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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
| #include <iostream> #include <algorithm> #include <stdio.h> #include <string> #include <cstring> #include <math.h> #include <sstream> #include <queue> #include <set> #include <map> using namespace std;
int x0,y0; struct node{ int x,y,dir; }; char dir[] = "NESW"; char run[] = "FLR";
int dir_id(char c) { return strchr(dir, c) - dir; }
int run_id(char c) { return strchr(run, c) - run; } int dir1[] = {-1,0,1,0}; int dir2[] = {0,1,0,-1};
node walk(node u, int turn) { int dir = u.dir; if(turn == 1) dir = (dir + 3) % 4; if(turn == 2) dir = (dir + 1) % 4; return node{u.x + dir1[dir], u.y + dir2[dir], dir}; }
node p[10][10][10]; int d[10][10][10]; string s; int hasedge[10][10][5][5]; void print(node u) { vector<node>V; while(1) { V.push_back(u); if(d[u.x][u.y][u.dir] == 0) break; u = p[u.x][u.y][u.dir]; } reverse(V.begin(), V.end()); int cnt = 0; int sz = V.size(); while(cnt != sz) { cout << ' '; for(int i = 0; i < min(10,sz); ++i) { printf(" (%d,%d)",V[cnt].x,V[cnt].y); cnt++; if(cnt == sz) break; } cout << '\n'; } }
void bfs(int x1,int y1,char c, int x2, int y2) { memset(d , -1, sizeof(d)); int num = dir_id(c); d[x0][y0][num] = 0; d[x1][y1][num] = 1; p[x1][y1][num] = node{x0,y0,num}; queue<node>Q; node a; a.x = x1, a.y = y1, a.dir = num; Q.push(a); while(!Q.empty()) { a = Q.front(); Q.pop(); if(a.x == x2 && a.y == y2) { print(a); return ;} for(int i = 0; i < 3; ++i) { if(hasedge[a.x][a.y][a.dir][i]) { node b = walk(a, i); if(d[b.x][b.y][b.dir] < 0) { d[b.x][b.y][b.dir] = d[a.x][a.y][a.dir] + 1; p[b.x][b.y][b.dir] = a; Q.push(b); } } } } cout << " No Solution Possible" << '\n'; }
int main() { string s1, s2; int a, b,x1,y1,x2,y2; char c; while(cin >> s) { if(s == "END") break; scanf("%d %d %c %d %d",&x1, &y1, &c, &x2, &y2); x0 = x1, y0 = y1; if(c == 'N') {x1--;} if(c == 'S') {x1++;} if(c == 'W') {y1--;} if(c == 'E') {y1++;} memset(hasedge, 0, sizeof(hasedge)); while(getline(cin ,s1)) { if(s1[0] == '0') break; int num1 = s1[0] - '0'; int num2 = s1[2] - '0'; stringstream ss(s1); while(ss >> s2) { if(isalpha(s2[0])) { int num3 = dir_id(s2[0]); for(int i = 1; i < s2.size(); ++i) { int num4 = run_id(s2[i]); hasedge[num1][num2][num3][num4] = 1; } } } } cout << s << '\n'; bfs(x1,y1,c,x2,y2); } }
|