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
| #include<cstring> #include<cstdio> #include<algorithm> #include<iostream> #include<unordered_map> #include<queue>
using namespace std;
const int N = 1000;
typedef pair<int, int> PII;
int n, m, k, ax, ay, bx, by,ans,t,step; int map[N][N]; bool st[N][N]; queue<PII> q; int dist[N][N]; int dx[4] = { 1,-1,0,0 }, dy[4] = { 0,0,1,-1 };
int bfs() { q.push({ ax,ay }); dist[ax][ay] = 0; st[ax][ay] = 0;
while (!q.empty()) { PII t = q.front(); q.pop();
int x = t.first, y = t.second;
if (x == bx && y == by) return dist[x][y];
for (int i = 0; i <= 3; i++) { int xx = x + dx[i], yy = y + dy[i]; if (xx >= 1 && xx <= n && yy >= 1 && yy <= n && !st[xx][yy]&&!map[xx][yy]) { q.push({ xx,yy }); dist[xx][yy] = dist[x][y] + 1; } } }
return -1; }
void dfs(int x,int y,int s) { if (s > t)return; if (abs(bx - x) + abs(by - y) + s > t)return; if (x == bx && y == by && s==t) { step+=1; return; }
for (int i = 0; i <= 3; i++) { int xx = x + dx[i], yy = y + dy[i]; if (xx >= 1 && xx <= n && yy >= 1 && yy <= n && !st[xx][yy] && !map[xx][yy]) { st[xx][yy] = true; dfs(xx, yy, s + 1); st[xx][yy] = false; } } }
int main() { while (cin >> n >> m >> k) { ans = 0x3f3f3f3f; step = 0; memset(map, 0, sizeof map); memset(st, false, sizeof st); while (k--) { int i, j; cin >> i >> j; map[i][j] = 1; } cin >> ax >> ay >> bx >> by;
t = bfs(); if (t == -1) { cout << "No Solution!" << endl<<"0"<<endl; } else { memset(st, false, sizeof st); dfs(ax, ay, 0); cout << t << endl<<step<<endl; } } }
|