hdoj 2216 Game III (BFS)

手感来了挡也挡不住,,一遍AC,,不会做的先把Double Maze做了。

Game III

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1289    Accepted Submission(s): 365


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 4 XXXX .Z.. .XS. XXXX 4 4 XXXX .Z.. .X.S XXXX 4 4 XXXX .ZX. .XS. XXXX
 

Sample Output
  
  
1 1 Bad Luck!
 

Author
zjt
 

#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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值