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 ] = {{ 1 0 }, { 0 1 }, { - 1 0 }, { 0 , - 1 }};
int  fdir[ 4 ][ 2 ] = {{ - 1 0 }, { 0 , - 1 }, { 1 0 }, { 0 1 }};
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, 
0 sizeof (vis));
        bfs();
    }
    
return   0 ;
}

转载于:https://www.cnblogs.com/lfeng/archive/2013/05/15/3080389.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值