HDU 1072

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

在n×m的地图上,0表示墙,1表示空地,2表示人
3表示目的地,4表示有炸弹重启器。
炸弹的时间是6,人走一步所需要的时间是1
求人从2走到3的最短时间
第一次的代码删了 这次的代码写的才舒服 贴上小庆祝一下
代码:

#include<iostream>
#include<queue>
#include<cstdio>
using namespace std;
int row,col,bx,by,ex,ey;
int map[10][10],XX[4][2]={{-1,0},{1,0},{0,1},{0,-1}};
struct node {int x,y,step,retime;};
void bfs()
{
int k;
queue<node>qu;
node t;
t.x=bx,t.y=by,t.step=0,t.retime=6;
qu.push(t);
map[bx][by]=0;
while(!qu.empty())
{
t=qu.front();
qu.pop();
for(k=0;k<4;k++)
{
node tt=t;
tt.x+=XX[k][0],tt.y+=XX[k][1];
if(tt.x<0||tt.x>=row||tt.y<0||tt.y>=col||map[tt.x][tt.y]==0)continue;
if(map[tt.x][tt.y]==3&&tt.retime>1)
{ tt.step++;
printf("%d\n",tt.step);
return;
}
if(map[tt.x][tt.y]==1)
{
tt.retime--;
if(tt.retime>=1)
{
tt.step++;
qu.push(tt);

}

}

if(map[tt.x][tt.y]==4)
{
tt.retime--;
if(tt.retime>=1)
{
tt.step++;
tt.retime=6;
qu.push(tt);

}
map[tt.x][tt.y]=0;
}

}
}
printf("-1\n");
}
int main()
{
int CASE,j,i;
scanf("%d",&CASE);
while(CASE--)
{
scanf("%d%d",&row,&col);
for(i=0;i<row;i++)
for(j=0;j<col;j++)
{
scanf("%d",&map[i][j]);
if(map[i][j]==2)
bx=i, by=j;
}
bfs();
}
return 0;
}


 
 
posted @ 2011-11-24 12:00  快乐.  阅读(148)  评论(0编辑  收藏  举报