HDOJ 2216 Game III (BFS)
来源:互联网 发布:韩国的淘宝网站 编辑:程序博客网 时间:2024/04/19 11:54
Game III
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1544 Accepted Submission(s): 436
Problem Description
Zjt and Sara will take part in a game, named Game III. Zjt and Sara will be in a maze, and Zjt must find Sara. There are some strang rules in this maze. If Zjt move a step, Sara will move a step in opposite direction.
Now give you the map , you shold find out the minimum steps, Zjt have to move. We say Zjt meet Sara, if they are in the same position or they are adjacent .
Zjt can only move to a empty position int four diraction (up, left, right, down). At the same time, Sara will move to a position in opposite direction, if there is empty. Otherwise , she will not move to any position.
The map is a N*M two-dimensional array. The position Zjt stays now is marked Z, and the position, where Sara stays, is marked E.
> . : empty position
> X: the wall
> Z: the position Zjt now stay
> S: the position Sara now stay
Your task is to find out the minimum steps they meet each other.
Now give you the map , you shold find out the minimum steps, Zjt have to move. We say Zjt meet Sara, if they are in the same position or they are adjacent .
Zjt can only move to a empty position int four diraction (up, left, right, down). At the same time, Sara will move to a position in opposite direction, if there is empty. Otherwise , she will not move to any position.
The map is a N*M two-dimensional array. The position Zjt stays now is marked Z, and the position, where Sara stays, is marked E.
> . : empty position
> X: the wall
> Z: the position Zjt now stay
> S: the position Sara now stay
Your task is to find out the minimum steps they meet each other.
Input
The input contains several test cases. Each test case starts with a line contains three number N ,M (2<= N <= 20, 2 <= M <= 20 ) indicate the size of the map. Then N lines follows, each line contains M character. A Z and a S will be in the map as the discription above.
Output
For each test case, you should print the minimum steps. “Bad Luck!” will be print, if they can't meet each other.
Sample Input
4 4XXXX.Z...XS.XXXX4 4XXXX.Z...X.SXXXX4 4XXXX.ZX..XS.XXXX
Sample Output
11Bad Luck!
#include<iostream>#include<cstdio>#include<cstring>#include<queue>#include<cmath>using namespace std;const int N = 25;struct Point{ int zx, zy; int sx, sy; int step;}start, fr, next;char mp[N][N];int vis[N][N][N][N];int dir[4][2] = {-1, 0, 0, 1, 1, 0, 0, -1};int n, m;bool judge(int x1, int y1, int x2, int y2){ if(x1 == x2 && (y1 == y2 || abs(y1 - y2) == 1)) return true; if(y1 == y2 && (x1 == x2 || abs(x1 - x2) == 1)) return true; return false;}void bfs(){ queue<Point> Q; Q.push(start); while(!Q.empty()){ fr = Q.front(); // printf("%d %d %d %d\n", fr.zx, fr.zy, fr.sx, fr.sy); Q.pop(); if(judge(fr.zx, fr.zy, fr.sx, fr.sy)){ printf("%d\n", fr.step); return; } for(int i = 0; i < 4; i++){ int xx = fr.zx + dir[i][0]; int yy = fr.zy + dir[i][1]; if(mp[xx][yy] == '\0' || mp[xx][yy] == 'X') continue; int xxx = fr.sx + dir[(i + 2) % 4][0]; int yyy = fr.sy + dir[(i + 2) % 4][1]; // printf("%d %d %d %d\n", xx, yy, xxx, yyy); if(mp[xxx][yyy] == '\0' || mp[xxx][yyy] == 'X'){ if(!vis[xx][yy][fr.sx][fr.sy]){ vis[xx][yy][fr.sx][fr.sy] = 1; next.zx = xx; next.zy = yy; next.sx = fr.sx; next.sy = fr.sy; next.step = fr.step + 1; Q.push(next); } } else{ if(!vis[xx][yy][xxx][yyy]){ vis[xx][yy][xxx][yyy] = 1; next.zx = xx; next.zy = yy; next.sx = xxx; next.sy = yyy; next.step = fr.step + 1; // printf("%d %d %d %d\n", next.zx, next.zy, next.sx, next.sy); Q.push(next); } } } } puts("Bad Luck!"); return;}int main(){ while(scanf("%d%d", &n, &m) != EOF){ memset(mp, '\0', sizeof(mp)); for(int i = 1; i <= n; i++){ scanf("%s", mp[i] + 1); for(int j = 1; j <= m; j++){ if(mp[i][j] == 'Z'){ start.zx = i; start.zy = j; } else if(mp[i][j] == 'S'){ start.sx = i; start.sy = j; } } } start.step = 0; memset(vis, 0, sizeof(vis)); vis[start.zx][start.zy][start.sx][start.sy] = 1; bfs(); } return 0;}
0 0
- HDOJ 2216 Game III (BFS)
- hdoj 2216 Game III (BFS)
- hdu 2216 Game III(BFS)
- hdu 2216 Game III(bfs)
- hdu2216 Game III--BFS(待解决)
- HDU2216:Game III(BFS)
- HDU2216:Game III(BFS)
- hdu 2216 Game III
- hdu 2216 Game III
- hdu 2216 Game III
- HDU 2216 Game III
- Fire Game(BFS)
- Fire Game (BFS)
- (BFS)Rescue--HDOJ
- hdu 杭电 2216 Game III
- FZU2150 Fire Game(BFS)
- poj1101-The Game(BFS)
- The Tag Game(bfs)
- [Android实例] Handler+ExecutorService(线程池)+MessageQueue模式+缓存模式
- HDOJ 1195 Open the Lock (BFS)
- node 阻止favicon.ico请求
- 【Java语法】synchronized
- C/C++异常处理阶段总结
- HDOJ 2216 Game III (BFS)
- 新增clob字段到数据库表中,按一定的样式插入储存。
- solution to 当前计算机未注册"microsoft.ACE.oledb.12.0"
- Objective-C中@property所有的属性详解
- 美剧资源-网盘链接
- kafka入门:简介、使用场景、设计原理、主要配置及集群搭建(转)
- rman全库备份,恢复过程
- HDOJ 1372 Knight Moves (BFS)
- 系统学习机器学习之非参数方法