hdu 5402 Travelling Salesman Problem(构造)

582 篇文章 0 订阅
33 篇文章 0 订阅

题目链接:hdu 5402 Travelling Salesman Problem


当n和m均为偶数,需要舍掉一个偶数位的格子。


#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn = 105;
const int dir[2][4][2] = { {{0, -1}, {-1, 0}, {1, 0}, {0, 1}}, {{-1, 0}, {0, 1}, {0, -1}, {1, 0}} };
const char sig[2][5] = {"LUDR", "URLD"};

int N, M, A[maxn][maxn], V[maxn][maxn];
char str[maxn * maxn];

int build (const int d[4][2], const char* t) {
	int x = 1, y = 1, n = 0; V[1][1] = 1;

	while (true) {
		bool flag = true;
		for (int i = 0; i < 4 && flag; i++) {
			int p = x + d[i][0];
			int q = y + d[i][1];
			if (p <= 0 || p > N || q <= 0 || q > M || V[p][q])
				continue;

			str[n++] = t[i];  V[p][q] = 1;
			x = p, y = q, flag = false;
		}
		if (flag) break;
	}
	str[n] = '\0';
	if (x == N && y == M)
		return n;
	return 0;
}

void build2 (const int d[4][2], const char* t, int& x, int &y, int& n, int l, int r) {
	V[x][y] = 1;

	while (true) {
//		printf("%d %d\n", x, y);
		bool flag = true;
		for (int i = 0; i < 4 && flag; i++) {
			int p = x + d[i][0];
			int q = y + d[i][1];
			if (p <= 0 || p > N || q <= l || q > r || V[p][q])
				continue;

			str[n++] = t[i]; V[p][q] = 1;
			x = p, y = q, flag = false;
		}
		if (flag) break;;
	}
//	printf("end:%d %d\n", x, y);
}

int main () {
	while (scanf("%d%d", &N, &M) == 2) {
		memset(V, 0, sizeof(V));

		int k = 1e4 + 5, rx, ry, s = 0;
		for (int i = 1; i <= N; i++) {
			for (int j = 1; j <= M; j++) {
				scanf("%d", &A[i][j]);
				s += A[i][j];
				if (A[i][j] < k && ((i+j)&1)) {
					k = A[i][j];
					rx = i, ry = j;
				}
			}
		}

		int end = N * M - 1;
		if ((N&1) == 0 && (M&1) == 0) {
			s -= k;
			int x = 1, y = 1, n = 0;
			memset(V, 0, sizeof(V));
			V[rx][ry] = 1;
			for (int i = 1; i <= M; i += 2) {
				if (i == ry || i + 1 == ry)
					build2(dir[1], sig[1], x, y, n, y - 1, y + 1);
				else
					build2(dir[0], sig[0], x, y, n, y - 1, y + 1);
				y++;
				str[n++] = 'R';
			}
			str[n-1] = '\0';
		} else {
			memset(V, 0, sizeof(V));
			int n = build(dir[0], sig[0]);
			if (n != end) {
				memset(V, 0, sizeof(V));
				int n = build(dir[1], sig[1]);
			}
		}

		printf("%d\n%s\n", s, str);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值