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.
 

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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 人被传染了耳螨怎么办 人被传染耳螨怎么办 头发干枯毛躁怎么办做什么发型 学校不给转学籍怎么办 结婚证号码错了怎么办 继教学分1类不够怎么办 猫咪耳螨传染人怎么办 ph值低了怎么办呀 水的ph值高怎么办 宝宝起热痱子了怎么办 宝宝热的起痱子怎么办 吃了有农药的菜怎么办 水银吃到肚子里怎么办 牙齿有牙结石怎么办可以去除 刷牙牙结石掉了怎么办 痘痘留下的黑印怎么办 脸上用了激素药怎么办 激素药膏用多了怎么办 8岁儿童牙齿不齐怎么办 两个牙齿之间有缝隙怎么办 蛀牙就剩牙根了怎么办 牙龈发白像烂了怎么办 3岁宝宝牙龈肿痛怎么办 1岁宝宝牙龈肿痛怎么办 牙疼引起的发烧怎么办 2岁小儿牙龈红肿怎么办 宝宝出牙牙龈红肿怎么办 3岁宝宝牙龈红肿怎么办 1岁宝宝牙龈红肿怎么办 4岁宝宝牙龈红肿怎么办 儿童牙黑了怎么办啊 牙黑了掉了一块怎么办 1岁幼儿牙齿腐蚀怎么办 我的大牙变黑了怎么办 牙齿里面黑了疼怎么办 最里面的牙黑了怎么办 牙龈的肉裂开了怎么办 有蛀牙怎么办可以变好么 拔智齿没拔干净怎么办 蛀牙只剩牙根了怎么办 牙掉的就剩牙根怎么办