您的位置:首页 > 其它

HDU 1072——Nightmare

2014-07-29 08:24 281 查看
题意分析:

一个人在迷宫中,身上背着一个定时***,初始时间是6秒,每次他只能按上下左右四个方向移动,每移动一步,***的时间就减少一秒,地图中有***时间重置点,可以将***的时间重新变为6,求逃离迷宫的最小步数

<span style="font-size:18px;"><strong>#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define fx(i,xi,n) for(int i=xi;i<n;++i)
#define ms(s,i) memset(s,i,sizeof (s))

using namespace std;

int map[10][10];
int vis[10][10];
int dir[4][2]={0,1,0,-1,1,0,-1,0};
int n,m,flag,ans;
struct pp{int x,y,step,time;};
bool check(int x,int y,int t){if(x>=0&&x<n&&y>=0&&y<m&&map[x][y]!=0&&!vis[x][y]&&t>0)return true;return false;}
void bfs(int sx,int sy,int ex,int ey){
    queue<pp> q;
    pp a,b;
    a.x=sx;a.y=sy;a.step=0;a.time=6;ms(vis,0);vis[sx][sy]=1;q.push(a);
    while(!q.empty()){
        a=q.front();q.pop();
        fx(i,0,4){
            b.x=a.x+dir[i][0];b.y=a.y+dir[i][1];b.time=a.time-1;//cout<<"b.x: "<<b.x<<" b.y: "<<b.y<<endl;
            if(check(b.x,b.y,b.time)){
//                vis[b.x][b.y]=1;
                b.step=a.step+1;
                if(b.x==ex&&b.y==ey){flag=1;ans=b.step;return;}
                if(map[b.x][b.y]==4){
                    vis[b.x][b.y]=1;
//                    cout<<"zhongzhi: "<<b.x<< " "<<b.y<<endl;
                    b.time=6;
                }
                q.push(b);
            }
        }
    }
    return ;
}
int main()
{
    int t;
    cin>>t;
    while(t--){
        cin>>n>>m;
        ms(map,0);
        fx(i,0,n)fx(j,0,m){scanf("%d",&map[i][j]);}
        int sx,sy,ex,ey;
        fx(i,0,n)fx(j,0,m){
            if(map[i][j]==2){sx=i,sy=j;}
            if(map[i][j]==3){ex=i,ey=j;}
        }
//        cout<<sx<<" "<<sy<<" "<<ex<<" "<<ey;
        flag=ans=0;
        bfs(sx,sy,ex,ey);
        if(flag) cout<<ans<<endl;
        else cout<<"-1"<<endl;

    }
    return 0;
}</strong></span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: