UVA 225 - Golygons(dfs回溯)
2014-01-03 23:39
399 查看
Golygons |
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
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 lexicographicsorder. 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
, ans[25], vis
, 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
- 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;
}
相关文章推荐
- Uva225 Golygons 【dfs回溯】【习题7-2】
- UVa 225 - Golygons(DFS+回溯)
- UVA 225 - Golygons(dfs回溯)
- UVa 225 - Golygons ( DFS, 回溯, 剪枝 )
- UVA225 Golygons 黄金图形(dfs+回溯)
- UVA 225 Golygons (DFS)
- 【uva】225 - Golygons(dfs有坑)
- UVa 225 - Golygons (回溯 + 剪枝)
- UVA 225 Golygons(DFS + sort)
- UVa 225:Golygons(DFS)
- 紫书搜索 习题7-2 UVA - 225 Golygons 搜索dfs
- UVa 225 – Golygons [DFS+剪枝]
- UVA225 Golygons 【搜索, dfs】
- UVa 225 – Golygons [DFS+剪枝]
- 【习题 7-2 UVA-225】Golygons
- UVA1600 Dfs回溯完全遍历+记忆化搜索(用最小距离剪枝)+细节处理 (值得复习)(水)
- uva 225(dfs)
- UVA 331 Mapping the Swaps (回溯+dfs)
- UVa 225 Golygons
- UVA Graph Coloring(DFS+回溯)