UVA 225 - Golygons(dfs回溯)

Golygons 

Imagine a country whose cities have all their streets laid out in a regular grid. Now suppose that a tourist with an obsession for geometry is planning expeditions to several such cities.

Starting each expedition from the central cross-roads of a city, the intersection labelled (0,0), our mathematical visitor wants to set off north, south, east or west, travel one block, and view the sights at the intersection (0,1) after going north, (0,-1) after going south, (1,0) after going east or (-1,0) after going west. Feeling ever more enthused by the regularity of the city, our mathematician would like to walk a longer segment before stopping next, going two blocks.

What's more, our visitor doesn't want to carry on in the same direction as before, nor visit the same point twice, nor wishes to double back, so will make a 90 tex2html_wrap_inline32 turn either left or right. The next segment should be three blocks, again followed by a right-angle turn, then four, five, and so on with ever-increasing lengths until finally, at the end of the day, our weary traveller returns to the starting point, (0,0).

The possibly self-intersecting figure described by these geometrical travels is called a golygon.

Unfortunately, our traveller will making these visits in the height of summer when road works will disrupt the stark regularity of the cities' grids. At some intersections there will be impassable obstructions. Luckily, however, the country's limited budget means there will never be more than 50 road works blocking the streets of any particular city. In an attempt to gain accountability to its citizens, the city publishes the plans of road works in advance. Our mathematician has obtained a copy of these plans and will ensure that no golygonal trips get mired in molten tar.

Write a program that constructs all possible golygons for a city.

Input

Since our tourist wants to visit several cities, the input file will begin with a line containing an integer specifying the number of cities to be visited.

For each city there will follow a line containing a positive integer not greater than 20 indicating the length of the longest edge of the golygon. That will be the length of the last edge which returns the traveler to (0,0). Following this on a new line will be an integer from 0 to 50 inclusive which indicates how many intersections are blocked. Then there will be this many pairs of integers, one pair per line, each pair indicating the x and y coordinates of one blockage.

Output

For each city in the input, construct all possible golygons. Each golygon must be represented by a sequence of characters from the set {n,s,e,w} on a line of its own, and they should be output in lexicographics order. Following the list of golygons should be a line indicating how many solutions were found. This line should be formatted as shown in the example output. A blank line should appear following the output for each city.

Sample Input

2
8
2
-2 0
6 -2
8
2
2 1
-2 0

Sample Output

wsenenws
Found 1 golygon(s).

Found 0 golygon(s).

Diagram of the 1st City


题意:一个探险队要去参观城市,走n次,每次走1,2,3,4,5,...n步,参观停留的城市,每次方向转90度。路途中有一些障碍物是不能走的。求所有走法,按字典序输出。注意,题目没说每个城市只能参观一次,但是实际上每个城市就是只能参观一次不然会WA。。坑

思路:深搜回溯,可以加一个剪枝,如果走到一个地方剩下的步数回不到原点了,直接回溯。

代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

const int N = 222;
const char ANS[5] = {"ensw"};
const int d[4][2] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
const int O = 105;

int t, n, m, g[N][N], ans[25], vis[N][N], sum[25], ansn;

void init() {
	ansn = 0;
	memset(g, 0, sizeof(g));
	memset(vis, 0, sizeof(vis));
	vis[O][O] = 1;
	scanf("%d%d", &n, &m);
	int x, y;
	while (m--) {
		scanf("%d%d", &x, &y);
		if (abs(x) > O || abs(y) > O) continue;
		g[y + O][x + O] = 1;
	}
}

bool judge(int x, int y, int xx, int yy, int k, int i) {
	if (abs(xx - O) + abs(yy - O) > sum[n] - sum[k + 1]) return false;	
	if ((vis[xx][yy] || g[xx][yy]) && !(xx == O && yy == O && k + 1 == n)) return false;
	int xxx = x, yyy = y;
	while (xxx != xx - d[i][0]) {
		xxx += d[i][0];
		if (g[xxx][y]) return false;
	}
	while (yyy != yy - d[i][1]) {
		yyy += d[i][1];
		if (g[x][yyy]) return false;
	}
	return true;
}

void dfs(int x, int y, int k, int v) {
	if (g[x][y]) return;
	if (k == n) {
		if (x == O && y == O) {
			ansn ++;
			for (int j = 0; j < k; j ++)
				printf("%c", ANS[ans[j]]);
			printf("\n");
		}
		return;
	}
	for (int i = 0; i < 4; i++) {
		if (i == v || i + v == 3) continue;
		int xx = x + d[i][0] * (k + 1);
		int yy = y + d[i][1] * (k + 1);
		if (!judge(x, y, xx, yy, k, i)) continue;
		ans[k] = i;
		vis[xx][yy]++;
		dfs(xx, yy, k + 1, i);
		vis[xx][yy]--;
	}
}

void solve() {
	dfs(O, O, 0, -1);
	printf("Found %d golygon(s).\n\n", ansn);
}

void sum_table() {
	sum[0] = 0;
	for (int i = 1; i <= 20; i++)
		sum[i] = sum[i - 1] + i;
}

int main() {
	sum_table();
	scanf("%d", &t);
	while (t--) {
		init();
		solve();
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值