首页 > ACM题库 > HDU-杭电 > HDU 1245 Saving James Bond-BFS-[解题报告] C++
2013
12-04

HDU 1245 Saving James Bond-BFS-[解题报告] C++

Saving James Bond

问题描述 :

This time let us consider the situation in the movie "Live and Let Die" in which James Bond, the world’s most famous spy, was captured by a group of drug dealers. He was sent to a small piece of land at the center of a lake filled with crocodiles. There he performed the most daring action to escape — he jumped onto the head of the nearest crocodile! Before the animal realized what was happening, James jumped again onto the next big head… Finally he reached the bank before the last crocodile could bite him (actually the stunt man was caught by the big mouth and barely escaped with his extra thick boot).
Assume that the lake is a 100×100 square one. Assume that the center of the lake is at (0,0) and the northeast corner at (50,50). The central island is a disk centered at (0,0) with the diameter of 15. A number of crocodiles are in the lake at various positions. Given the coordinates of each crocodile and the distance that James could jump, you must tell him whether he could escape.If he could,tell him the shortest length he has to jump and the min-steps he has to jump for shortest length.

输入:

The input consists of several test cases. Each case starts with a line containing n <= 100, the number of crocodiles, and d > 0, the distance that James could jump. Then one line follows for each crocodile, containing the (x, y) location of the crocodile. Note that x and y are both integers, and no two crocodiles are staying at the same position.

输出:

For each test case, if James can escape, output in one line the shortest length he has to jump and the min-steps he has to jump for shortest length. If it is impossible for James to escape that way, simply ouput "can’t be saved".

样例输入:

4 10
17 0
27 0
37 0
45 0
1 10
20 30

样例输出:

42.50 5
can't be saved

纠结了一个上午,原来是运算符重载弄错了!我去。

该题用一个四维数组标记人和箱子的当前状态。在判断是否满足队列条件时,用continue巧妙的处理了,如果搬运工遇到箱子的问题。

#include<iostream>
 #include<cstring>
 #include<cstdio>
 #include<queue>
 using namespace std;
 struct node
 {
     int x,y;
     int bx,by;
     int step;
     bool friend operator<(node a,node b)
     {
        return a.step>b.step;//这里开始定义的小于  交了n次都不对
     };
 };
 #define N 10
 int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
 int map[N][N];
 int mark[N][N][N][N];
 int sx,sy,bx,by,ex,ey;
 int n,m;
 void BFS()
 {
     memset(mark,0,sizeof(mark));
     node cur,next;
     priority_queue<node>q;
     int x,y,xx,yy,i;
     cur.x=sx;cur.y=sy;cur.bx=bx;cur.by=by;cur.step=0;
     q.push(cur);
     mark[sx][sy][bx][by]=1;
     while(!q.empty())
     {
         cur=q.top();
         q.pop();
         if(cur.bx==ex&&cur.by==ey)
         {
             printf("%d\n",cur.step);
             return ;
         }
         for(i=0;i<4;i++)
         {
             next.x=x=cur.x+dir[i][0];
             next.y=y=cur.y+dir[i][1];
             next.bx=xx=cur.bx;
             next.by=yy=cur.by;
             next.step=cur.step;
             if(x<0||x>=n||y<0||y>=m)//第一次用for循环写判断符合条件的WA了,改用continue可以很好的处理如果搬运工前面是箱子的问题
                 continue;
             if(map[x][y]==1||mark[x][y][xx][yy]==1)
                 continue;
             if(x==xx&&y==yy)
             {
                 next.bx=xx=xx+dir[i][0];
                 next.by=yy=yy+dir[i][1];
                 next.step++;
                 if(xx<0||xx>=n||yy<0||yy>=m)
                 continue;
                 if(map[xx][yy]==1||mark[x][y][xx][yy]==1)
                     continue;
 
             }
             q.push(next);
             mark[x][y][xx][yy]=1;
 
         }
     }
     printf("-1\n");
     return ;
 }
 int main()
 {
    //freopen("input.txt","r",stdin);
   //  freopen("output.txt","w",stdout);
    int  t;
    scanf("%d",&t);
    while(t--)
    {
        memset(map,0,sizeof(map));
        scanf("%d%d",&n,&m);
        int i,j;
        for(i=0;i<n;i++)
        {
 
            for(j=0;j<m;j++)
            {
                scanf("%d",&map[i][j]);
            }
        }
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                if(map[i][j]==4)
                {
                   sx=i,sy=j;
                   
                }
                else if(map[i][j]==2)
                {
                    bx=i,by=j;
 
                }
                else if(map[i][j]==3)
                {
                    ex=i,ey=j;
                    
                }
            }
        }
        BFS();
    }
 
     return 0;
 }

 

 

 


,