HDU 1072——Nightmare
2014-07-29 08:24
281 查看
题意分析:
一个人在迷宫中,身上背着一个定时***,初始时间是6秒,每次他只能按上下左右四个方向移动,每移动一步,***的时间就减少一秒,地图中有***时间重置点,可以将***的时间重新变为6,求逃离迷宫的最小步数
一个人在迷宫中,身上背着一个定时***,初始时间是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>
相关文章推荐
- hdu 1072 Nightmare bfs
- HDU 1072 Nightmare
- hdu 1072 Nightmare(bfs)
- HDU1072-Nightmare【广度优先搜索】
- hdu 1072 Nightmare (bfs)
- hdu1072 nightmare
- hdu1072 Nightmare
- HDU 1072 Nightmare
- HDU 1072 Nightmare
- HDU 1728 逃离迷宫 + HDU 1072 Nightmare
- HDU 1072 Nightmare
- hdu 1072 Nightmare (bfs)
- HDU 1072 Nightmare(搜索)
- HDU1072-Nightmare
- HDU 1072 Nightmare (bfs)
- HDU_1072_Nightmare题解
- hdu 1072 Nightmare(BFS+回头路)
- hdu 1072 Nightmare
- HDU - 1072 Nightmare
- HDU 1072 Nightmare (BFS 状态的设定:参考了别人的代码)