hdu

http://acm.hdu.edu.cn/showproblem.php?pid=1072

遇到Bomb-Reset-Equipment的时候除了时间恢复之外,必须把这个点做标记不能再走,不然可能造成死循环,也得不到最优解。

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
struct point
{
    int x,y,time,step;
};
point s;
int n,m;
int maze[15][15];
int dir[4][2]={{-1,0},{1,0},{0,1},{0,-1}};

void bfs()
{
   queue<point>que;
    que.push(s);
    maze[s.x][s.y]=0;
    while(!que.empty())
    {
        point t=que.front(); que.pop();
       // printf("%d %d %d %d
",t.x,t.y,t.step,t.time);
        if(maze[t.x][t.y]==3&&t.time>0) {printf("%d
",t.step);return;}
        if(t.time==1) continue;
        for(int i=0;i<4;i++)
        {
            s.x=t.x+dir[i][0],s.y=t.y+dir[i][1];
            if(s.x>=0&&s.x<n&&s.y>=0&&s.y<m&&maze[s.x][s.y]!=0)
            {
                if(maze[s.x][s.y]==4)
                {
                    s.time=6;
                    maze[s.x][s.y]=0;
                }
                else s.time=t.time-1;
                s.step=t.step+1;
                que.push(s);
            }
        }
    }
    printf("-1
");
}
int main()
{
   //freopen("a.txt","r",stdin);
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
            {
                scanf("%d",&maze[i][j]);
                if(maze[i][j]==2)
                {
                    s.x=i;
                    s.y=j;
                    s.step=0;s.time=6;
                }
            }
        bfs();
    }
    return 0;
}
原文地址:https://www.cnblogs.com/nowandforever/p/4536765.html