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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2216

    Game III

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


    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
     
    Recommend
    lcy
     
    这里状态需要用四维数组标记,搜索每个过程Zjt可能走的位置,然后Sara走相反的位置,如果无法走,则保持原位,注意边界问题,不然可能会RE。
    之前用dfs提交RE了,不知道哪里溢出了,唉,有时间的话再找下问题所在吧

    贴下AC代码:

     By LFENG
     
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<queue>
    #include<cstdlib>
    #include<queue>
    using namespace std;
    struct node
    {
        
    int sx;
        
    int sy;
        
    int zx;
        
    int zy;
        
    int time;
    };
    char g[30][30];
    int dir[4][2] = {{10}, {01}, { -10}, {0, -1}};
    int fdir[4][2] = {{ -10}, {0, -1}, {10}, {01}};
    int vis[30][30][30][30];
    int n, m, x, y, x2, y2, ok;
    void getdata()
    {
        
    int i, j;
        
    char s[30];
        
    for(i = 0; i < n; i++)
        {
            scanf(
    "%s", s);
            
    for(j = 0; j < m; j++)
            {
                
    if(s[j] == 'S')
                {
                    x = i;
                    y = j;
                }
                
    else if(s[j] == 'Z')
                {
                    x2 = i;
                    y2 = j;
                }
                
    if(s[j] == 'Z' || s[j] == 'S')g[i][j] = '.';
                
    else g[i][j] = s[j];
            }
        }
    }
    void bfs()
    {
        node p, s;
        
    int i;
        queue<node>q;
        p.sx = x;
        p.sy = y;
        p.zx = x2;
        p.zy = y2;
        p.time = 
    0;
        vis[p.sx][p.sy][p.zx][p.zy] = 
    1;
        q.push(p);
        
    while(!q.empty())
        {
            p = q.front();
            q.pop();
            
    if(abs(p.sx - p.zx) + abs(p.sy - p.zy) <= 1)
            {
                printf(
    "%d\n", p.time);
                
    return ;
            }
            
    for(i = 0; i < 4; i++)
            {
                s.zx = p.zx + dir[i][
    0];
                s.zy = p.zy + dir[i][
    1];
                
    if(s.zx < 0 || s.zx >= n || s.zy < 0 || s.zy >= m || g[s.zx][s.zy] == 'X')continue;
                s.sx = p.sx + fdir[i][
    0];
                s.sy = p.sy + fdir[i][
    1];
                
    if(s.sx < 0 || s.sx >= n || s.sy < 0 || s.sy >= m || g[s.sx][s.sy] == 'X')
                {
                    s.sx = p.sx;
                    s.sy = p.sy;
                }
                
    if(vis[s.sx][s.sy][s.zx][s.zy])continue;
                vis[s.sx][s.sy][s.zx][s.zy] = 
    1;
                s.time = p.time + 
    1;
                q.push(s);
            }
        }
        printf(
    "Bad Luck!\n");
    }
    int main()
    {
        node p;
        
    while(scanf("%d %d", &n, &m) != EOF)
        {
            getchar();
            getdata();
            memset(vis, 
    0sizeof(vis));
            bfs();
        }
        
    return 0;
    }
  • 相关阅读:
    qt调用simsimi api实现小黄鸡
    [机器学习系列] k-近邻算法(K–nearest neighbors)
    Ubuntu上安装flashplayer
    关于ubuntu下qt编译显示Cannot connect creator comm socket /tmp/qt_temp.xxx/stub-socket的解决办法
    Linux下添加源的几种方法
    Ubuntu字符界面输入密码始终提示错误 login incorrect 解决办法
    boost::algorithm(字符串算法库)
    boost::assign(标准容器填充库)
    boost::format(字符串格式化库)
    C/C++内存对齐 ZZ
  • 原文地址:https://www.cnblogs.com/lfeng/p/3080389.html
Copyright © 2011-2022 走看看