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 143 144 145 146 147 148 149 150 151 152 153 154 155
| #include<iostream> #include<vector> #include<list> #include<queue> #include<cstring> using namespace std; #define SIZE 31 bool map[SIZE][SIZE]; int SizeX, SizeY; int EmptyX, EmptyY; int StartX, StartY; int TargX, TargY; int gamePlays; struct Graph { static const int MAXNODE = 10000; struct Edge { int dest, weight; Edge(int d, int w) : dest(d), weight(w) {} }; list<Edge>map[MAXNODE]; }; struct SPFA :private Graph { int dis[MAXNODE]; bool visited[MAXNODE]; queue<int>joblist; void clear() { memset(dis, 1, sizeof dis); memset(visited, 0, sizeof visited); while (!joblist.empty())joblist.pop(); } void work() { while (!joblist.empty()) { int current = joblist.front(); joblist.pop(); visited[current] = 0; list<Edge>::iterator i = map[current].begin(); for (; i != map[current].end(); i++) { if (dis[i->dest] > dis[current] + i->weight) { dis[i->dest] = dis[current] + i->weight; if (!visited[i->dest]) { joblist.push(i->dest); visited[i->dest] = 1; } } } } } void connect(int a, int b, int w) { map[a].push_front(Edge(b, w)); } };
struct point { int x, y, step; point(int x, int y, int s) : x(x), y(y), step(s) {} }; bool visited[SIZE][SIZE]; int dx[4] = { 1,-1,0,0 }; int dy[4] = { 0,0,1,-1 }; int priceToMoveTo(int StartX, int StartY, int EndX, int EndY, int BlankX, int BlankY) { queue<point>joblist; if (StartX == EndX&&StartY == EndY)return 0; joblist.push(point(StartX, StartY, 0)); memset(visited, 0, sizeof visited); visited[StartX][StartY] = 1; while (!joblist.empty()) { point current = joblist.front(); joblist.pop(); if (current.x == EndX&¤t.y == EndY)return current.step; for (int i = 0; i < 4; i++) { if (visited[current.x + dx[i]][current.y + dy[i]]) continue; if (!map[current.x + dx[i]][current.y + dy[i]]) continue; if (current.x + dx[i] == BlankX && current.y + dy[i] == BlankY) continue; visited[current.x + dx[i]][current.y + dy[i]] = 1; joblist.push(point(current.x + dx[i], current.y + dy[i], current.step + 1)); } } return 16843009; }
int id[SIZE][SIZE][4]; void RenewID() { int temp = 1; for (int i = 1; i <= SizeY; i++) { for (int j = 1; j <= SizeX; j++) for (int k = 0; k < 4; k++) if (map[i][j] && map[i + dx[k]][j + dy[k]]) id[i][j][k] = temp++; } } void readMap() { cin >> SizeY >> SizeX >> gamePlays; for (int i = 1; i <= SizeY; i++) for (int j = 1; j <= SizeX; j++) cin >> map[i][j]; } int main() { readMap(); SPFA instG; RenewID(); for (int i = 1; i <= SizeY; i++) for (int j = 1; j <= SizeX; j++) for (int k = 0; k < 4; k++) if (id[i][j][k]) instG.connect( id[i][j][k], id[i + dx[k]][j + dy[k]][k ^ 1], 1); for (int a = 1; a <= SizeY; a++) for (int b = 1; b <= SizeX; b++) for (int i = 0; i < 4; i++) for (int j = 0; j < 4; j++) { if (i == j)continue; if (!id[a][b][i] || !id[a][b][j])continue; instG.connect( id[a][b][i], id[a][b][j], priceToMoveTo(a + dx[i], b + dy[i], a + dx[j], b + dy[j], a, b) ); } while (gamePlays--) { cin >> EmptyX >> EmptyY >> StartX >> StartY >> TargX >> TargY; if (StartX == TargX&&StartY == TargY) { cout << 0 << endl; continue; } instG.clear(); for (int i = 0; i < 4; i++) { if (id[StartX][StartY][i]) { instG.joblist.push(id[StartX][StartY][i]); instG.visited[id[StartX][StartY][i]] = 1; instG.dis[id[StartX][StartY][i]] = priceToMoveTo(EmptyX, EmptyY, StartX + dx[i], StartY + dy[i], StartX, StartY); } } instG.work(); int Min = 16843009; for (int i = 0; i < 4; i++) { if (id[TargX][TargY][i] && instG.dis[id[TargX][TargY][i]] < Min) Min = instG.dis[id[TargX][TargY][i]]; } cout << (Min == 16843009 ? -1 : Min) << endl; } }
|