hdu1072

来源:互联网 发布:php json unicode 编辑:程序博客网 时间:2024/05/17 08:40
#include<iostream>#include<queue>#include<string>using namespace std;typedef struct point{int x,y;int step;int time;}point;int n,m;int mins;int map[10][10];int t[10][10];int sx,sy,ex,ey;int dir[4][2]={0,1,1,0,0,-1,-1,0};void bfs(){mins=0;queue<point> Q;point start;start.x=sx;start.y=sy;start.step=0;start.time=6;t[sx][sy]=6;Q.push(start);while(!Q.empty()){point now=Q.front();Q.pop();if(now.x==ex&&now.y==ey){if(now.time>0){mins=now.step;return;}else continue;}if(map[now.x][now.y]==4){now.time=6;t[now.x][now.y]=6;}int i;for(i=0;i<4;i++){point temp;temp.x=now.x+dir[i][0];temp.y=now.y+dir[i][1];temp.step=now.step+1;temp.time=now.time-1;if(temp.x>=0&&temp.x<n&&temp.y>=0&&temp.y<m&&now.time>0&&temp.time>t[temp.x][temp.y]&&map[temp.x][temp.y]!=0){t[temp.x][temp.y]=temp.time;Q.push(temp);}}}}int main(){int k;cin>>k;while(k--){cin>>n>>m;int i,j;for(i=0;i<n;i++){for(j=0;j<m;j++){t[i][j]=0;cin>>map[i][j];if(map[i][j]==2){sx=i;sy=j;}else if(map[i][j]==3){ex=i;ey=j;}}}bfs();if(mins==0)cout<<-1<<endl;else cout<<mins<<endl;}return 0;}

原创粉丝点击