1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
| const int R = 4, C = 6; const string CH = "WNES";
const int dx[] = {0, -1, 0, 1}; const int dy[] = {-1, 0, 1, 0}; const int rev[] = {2, 3, 0, 1};
int sx = 0, sy = 0;
int vis[R][C]; int grid[R][C];
vector<char> paths;
void init() { Set(grid, 0); }
void initVis() { paths.clear(); Set(vis, 0); }
void setbit(int& x, int b, bool flag) { if(flag) x |= (1 << b); else x &= ~(1 << b); } bool getbit(const int x, const int b) { return (x & (1 << b)) > 0; }
bool valid(int x, int y) { return 0 <= x && x < R && 0 <= y && y < C; }
bool isExit(const int x, const int y, vector<char>& paths) { int p = grid[x][y]; if(x == 0 && !getbit(p, 1)) { paths.push_back(CH[1]); return true; } if(x == R - 1 && !getbit(p, 3)) { paths.push_back(CH[3]); return true; } if(y == 0 && !getbit(p, 0)) { paths.push_back(CH[0]); return true; } if(y == C - 1 && !getbit(p, 2)) { paths.push_back(CH[2]); return true; } return false; }
bool dfs(int x, int y, vector<char>& paths, int d, const int maxd) { if(isExit(x, y, paths)) return true; if(d >= maxd) return false; int& p = grid[x][y]; _for(dir, 0, 4) { int nx = x + dx[dir], ny = y + dy[dir]; if(!valid(nx, ny) || vis[nx][ny]) continue; int& np = grid[nx][ny]; paths.push_back(CH[dir]); vis[nx][ny] = 1; // dfs next position if(!getbit(p, dir)) { if(dfs(nx, ny, paths, d + 1, maxd)) return true; } else if(!getbit(np, dir)) { // push the wall setbit(p, dir, 0); setbit(np, dir, 1); setbit(np, rev[dir], 0); // next next position will change because of the pushment int nnx = nx + dx[dir], nny = ny + dy[dir]; if(valid(nnx, nny)) setbit(grid[nnx][nny], rev[dir], 1); if(dfs(nx, ny, paths, d + 1, maxd)) return true; if(valid(nnx, nny)) setbit(grid[nnx][nny], rev[dir], 0); setbit(p, dir, 1); setbit(np, dir, 0); setbit(np, rev[dir], 1); } paths.pop_back(); vis[nx][ny] = 0; } return false; }
int main() { freopen("input.txt", "r", stdin); while (scanf("%d%d", &sy, &sx) == 2 && (sx || sy)) { init(); _for(i, 0, R) _for(j, 0, C) scanf("%d", &grid[i][j]); sx--; sy--; int maxd = 1; for(maxd = 1; ; maxd++) { initVis(); vis[sx][sy] = 1; if(dfs(sx, sy, paths, 0, maxd)) break; vis[sx][sy] = 0; } _for(i, 0, paths.size()) cout << paths[i]; cout << endl; } }
|