Accept: 199 Submit: 1546
Time Limit: 10000 mSec
Problem Description
Input
The input file may contain several mazes to solve. Each maze description starts with a single line containing two integers x and y (1 ≤ x ≤ 6,1 ≤ y ≤ 4) which is the start position in the maze. Next follows four lines with six integers each. These integers p (0 ≤ p ≤ 15) describe each square in the maze in the following way: p is the sum of 1 (if there is a wall west of the square), 2 (north), 4 (east) and 8 (south). Each inner wall will thus be mentioned twice. Each opening in the boundary is considered an exit. The input ends with a maze with starting coordinates 0, 0 and should not be processed.
Output
Output a single line for each maze with the description of a path with minimum length that leads to any of exits. Use the letters ‘N’, ‘S’, ‘E’ and ‘W’ to denote north, south, east and west, respectively. If there are several solutions with minimum length, display any one of them.
Sample Input
10 2 10 10 2 6
3 12 11 14 9 4
13 15 3 6 15 13
14 11 12 9 14 11
0 0
Sample Output
NESESEENNWNWWWWW
题解:这个题最大的提示在Input里,直接告诉你最佳存图方式,最短路径,因此采用IDA*,之后就是水题了。
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 const int maxn = 5, maxm = 7; 6 const char direction[] = "WNES"; 7 const int INF = 0x3f3f3f3f; 8 const int dx[] = { 0,-1,0,1 }; 9 const int dy[] = { -1,0,1,0 }; 10 const int dir[] = { 1,2,4,8 }; 11 12 int sx, sy, maxd; 13 int ans[maxn*maxm]; 14 int gra[maxn][maxm]; 15 bool vis[maxn][maxm]; 16 17 vector< pair<int, int> > Exit; 18 19 int ok(int x,int y) { 20 if (x == 1 && !(gra[x][y] & 2)) return 1; 21 else if (x == 4 && !(gra[x][y] & 8)) return 3; 22 if (y == 1 && !(gra[x][y] & 1)) return 0; 23 else if (y == 6 && !(gra[x][y] & 4)) return 2; 24 return -1; 25 } 26 27 bool Judge(int x, int y) { 28 if (x < 1 || y < 1 || x > 4 || y > 6) return true; 29 return false; 30 } 31 32 bool dfs(int d, int x, int y) { 33 if (d == maxd) return false; 34 int tmp = ok(x, y); 35 if (tmp != -1) { 36 ans[d] = tmp; 37 return true; 38 } 39 40 int h = INF; 41 for (vector< pair<int, int> >::iterator it = Exit.begin(); it != Exit.end(); it++) { 42 h = min(h, abs(it->first - x) + (it->second - y)); 43 } 44 if (d + h > maxd) return false; 45 46 for (int i = 0; i < 4; i++) { 47 int xx = x + dx[i], yy = y + dy[i]; 48 if (Judge(xx, yy) || vis[xx][yy]) continue; 49 if (!(gra[x][y] & dir[i])) { 50 vis[xx][yy] = true; 51 ans[d] = i; 52 if (dfs(d + 1, xx, yy)) return true; 53 vis[xx][yy] = false; 54 } 55 else if (!(gra[xx][yy] & dir[i])) { 56 gra[xx][yy] += dir[i]; 57 gra[x][y] -= dir[i]; 58 if (!Judge(xx + dx[i], yy + dy[i])) { 59 gra[xx + dx[i]][yy + dy[i]] += dir[(i + 2) % 4]; 60 } 61 ans[d] = i; 62 vis[xx][yy] = true; 63 if (dfs(d + 1, xx, yy)) return true; 64 vis[xx][yy] = false; 65 if (!Judge(xx + dx[i], yy + dy[i])) { 66 gra[xx + dx[i]][yy + dy[i]] -= dir[(i + 2) % 4]; 67 } 68 gra[xx][yy] -= dir[i]; 69 gra[x][y] += dir[i]; 70 } 71 } 72 return false; 73 } 74 75 int main() 76 { 77 //freopen("input.txt", "r", stdin); 78 while (~scanf("%d%d", &sx, &sy) && (sx || sy)) { 79 for (int i = 1; i <= 4; i++) { 80 for (int j = 1; j <= 6; j++) { 81 scanf("%d", &gra[i][j]); 82 83 if (j == 1) { 84 if (gra[i][j] & 1) Exit.push_back(make_pair(i, j)); 85 } 86 else if (j == 6) { 87 if (gra[i][j] & 1 << 2) Exit.push_back(make_pair(i, j)); 88 } 89 } 90 if (i == 1) { 91 for (int j = 1; j <= 6; j++) { 92 if (gra[i][j] & (1 << 1)) Exit.push_back(make_pair(i, j)); 93 } 94 } 95 else if (i == 4) { 96 for (int j = 1; j <= 6; j++) { 97 if (gra[i][j] & (1 << 3)) Exit.push_back(make_pair(i, j)); 98 } 99 } 100 } 101 102 for (maxd = 0;; maxd++) { 103 memset(vis, false, sizeof(vis)); 104 vis[sy][sx] = true; 105 if (dfs(0, sy, sx)) break; 106 } 107 108 for (int i = 0; i < maxd; ++i) { 109 printf("%c", direction[ans[i]]); 110 } 111 printf(" "); 112 } 113 }