标签:
题目链接:http://acm.swust.edu.cn/problem/409/
8 8 3
3 3
4 5
6 6
2 1
7 7
|
11
96
|
1 #include <iostream> 2 #include <queue> 3 #include <cstring> 4 #define maxn 101 5 using namespace std; 6 typedef long long LL; 7 struct node{ 8 int sx, sy; 9 node(int x, int y) :sx(x), sy(y){}; 10 node(){}; 11 }; 12 int n, m, k, mpt[maxn][maxn], vis[maxn][maxn], dis[4][2] = { 1, 0, -1, 0, 0, -1, 0, 1 }; 13 LL dp[maxn][maxn]; 14 //dp[i][j]代表路径条数,mpt[i][j]最短路程的值 15 int BFS(int sx, int sy, int ex, int ey){ 16 dp[sx][sy] = 1;//初始情况1条路径 17 vis[sx][sy] = 1; 18 node start(sx, sy); 19 queue<node>Q; 20 Q.push(start); 21 while (!Q.empty()){ 22 node now = Q.front(); 23 Q.pop(); 24 if (now.sx == ex && now.sy == ey)//找到终点,返回最短路径 25 return mpt[now.sx][now.sy]; 26 for (int i = 0; i < 4; i++){ 27 int x = now.sx + dis[i][0]; 28 int y = now.sy + dis[i][1]; 29 if (x >= 1 && x <= n && y >= 1 && y <= m && mpt[x][y] != -1){ 30 if (!vis[x][y]){ 31 vis[x][y] = 1; 32 mpt[x][y] = mpt[now.sx][now.sy] + 1;//最短路程加1 33 dp[x][y] = dp[now.sx][now.sy];//直接赋值 34 node next(x, y); 35 Q.push(next); 36 } 37 else 38 dp[x][y] += dp[now.sx][now.sy]; 39 } 40 } 41 } 42 return -1; 43 } 44 int main(){ 45 int x, y, sx, sy, ex, ey; 46 while (cin >> n >> m >> k){ 47 memset(mpt, 0, sizeof(mpt)); 48 memset(dp, 0, sizeof(dp)); 49 memset(vis, 0, sizeof(vis)); 50 for (int i = 0; i < k; i++){ 51 cin >> x >> y; 52 mpt[x][y] = -1;//标记为障碍物 53 } 54 cin >> sx >> sy >> ex >> ey; 55 int ans = BFS(sx, sy, ex, ey); 56 if (ans == -1) 57 cout << "No Solution!" << endl; 58 else 59 cout << mpt[ex][ey] << endl << dp[ex][ey] << endl; 60 } 61 return 0; 62 }
[Swust OJ 409]--小鼠迷宫问题(BFS+记忆化搜索)
标签:
原文地址:http://www.cnblogs.com/zyxStar/p/4608225.html