zoukankan      html  css  js  c++  java
  • HDU 2216 Game III(BFS)

    Game III

    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!
     
    Answer
    题解:这个BFS很有意思,跟典型的题目不同,它的目标点在动。当Z在移动的时候,S会往相反方向移动(如果能动)。就是这点,导致WA了两次,vis数组只开了二维来记录Z有没有走过,然后看了题解是要开四维数组,保存Z、S。然后第三组样例又一只跑不出来,发现要把判断是否访问语句放到判断S是否移动的后面才行。
     
    #include <cstdio>
    #include <iostream>
    #include <string>
    #include <sstream>
    #include <cstring>
    #include <stack>
    #include <queue>
    #include <algorithm>
    #include <cmath>
    #include <map>
    #define PI acos(-1.0)
    #define ms(a) memset(a,0,sizeof(a))
    #define msp memset(mp,0,sizeof(mp))
    #define msv memset(vis,0,sizeof(vis))
    using namespace std;
    //#define LOCAL
    int n,m;
    char mp[22][22];
    bool vis[22][22][22][22];
    int dir[4][2]={{1,0},{-1,0},{0,-1},{0,1}};
    struct Node
    {
        int zx,zy;
        int sx,sy;
        int step;
    }t,nn;
    int bfs()
    {
        queue<Node> q;
        while(!q.empty())q.pop();
        vis[t.zx][t.zy][t.sx][t.sy]=1;
        t.step=0;
        q.push(t);
        while(!q.empty())
        {
            t=q.front(),q.pop();
            if(t.sx==t.zx&&abs(t.sy-t.zy)==1)return t.step;
            if(t.sy==t.zy&&abs(t.sx-t.zx)==1)return t.step;
            if(t.sx==t.zx&&t.sy==t.zy)return t.step;
            for(int i=0;i<4;i++)
            {
                nn.zx=t.zx+dir[i][0];
                nn.zy=t.zy+dir[i][1];
                if(mp[nn.zx][nn.zy]=='X')continue;
                if(nn.zx<0||nn.zx>=n||nn.zy<0||nn.zy>=m)continue;
                nn.sx=t.sx-dir[i][0];
                nn.sy=t.sy-dir[i][1];
                if(nn.sx<0||nn.sx>=n||nn.sy<0||nn.sy>=m)nn.sx=t.sx,nn.sy=t.sy;
                if(mp[nn.sx][nn.sy]=='X')nn.sx=t.sx,nn.sy=t.sy;
                nn.step=t.step+1;
                if(vis[nn.zx][nn.zy][nn.sx][nn.sy])continue;
                vis[nn.zx][nn.zy][nn.sx][nn.sy]=1;
                q.push(nn);
            }
        }
        return -1;
    }
    int main()
    {
    #ifdef LOCAL
        freopen("in.txt", "r", stdin);
    #endif // LOCAL
        ios::sync_with_stdio(false);
        while(cin>>n>>m)
        {
            msv;
            for(int i=0;i<n;i++)cin>>mp[i];
            for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
            {
                if(mp[i][j]=='Z')t.zx=i,t.zy=j;
                else if(mp[i][j]=='S')t.sx=i,t.sy=j;
            }
            int ans=bfs();
            if(ans==-1)printf("Bad Luck!
    ");
            else printf("%d
    ",ans);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    diamond operator is not supported in -source 1.5
    ClassNotFoundException异常的解决方法
    serialVersionUID 的用途--转加自己的疑问
    java序列化---转
    junit类找不到的问题解决
    FastJson的基本用法----转
    sql字符串查找大小写敏感相关
    6.比较排序之快速排序
    有关ArrayList常用方法的源码解析
    5.比较排序之归并排序(非递归)
  • 原文地址:https://www.cnblogs.com/gpsx/p/5186548.html
Copyright © 2011-2022 走看看