来源:点击打开链接
水到炸的BFS,0为墙,1为路,2为起点,3为终点,4为炸弹重置(6秒钟内要么走出去要么重置),走一步需要一秒,求走出去的最短时间~
加一个简单的炸弹时间判断即可,开始wa1,因为visit标记只有重置点不能重复运行,而其他的地方都可以,所以visited的范围要有所注意。
#include <iostream>
#include <cmath>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
char mat[10][10];
bool visited[10][10];
int dir[4][2]={1,0,-1,0,0,1,0,-1};
int high,length;
class node
{
public:
int x;
int y;
int boomst;
int step;
};
int bfs(node st,node ed)
{
memset(visited,0,sizeof(visited));
queue<node> p;
node first,next;
first.x=st.x;
first.y=st.y;
first.boomst=6;
first.step=0;
visited[st.x][st.y]=1;
p.push(first);
while(!p.empty())
{
first=p.front();
p.pop();
for(int i=0;i<4;i++)
{
next.x=first.x+dir[i][0];
next.y=first.y+dir[i][1];
next.step=first.step+1;
next.boomst=first.boomst-1;
if(next.x>=0 && next.x<length && next.y>=0 && next.y<high && !visited[next.x][next.y] && next.boomst>=1 && mat[next.x][next.y]!='0')
{
if(next.x==ed.x && next.y==ed.y)
{
return next.step;
}
else if(mat[next.x][next.y]=='4')
{
next.boomst=6;
visited[next.x][next.y]=1;
}
else if(mat[next.x][next.y]=='1')
{
next.boomst=first.boomst-1;
}
p.push(next);
}
}
}
return -1;
}
int main()
{
int testcase;
node stt,edd;
cin>>testcase;
while(testcase--)
{
memset(mat,0,sizeof(mat));
cin>>length>>high;
for(int i=0;i<length;i++)
{
for(int j=0;j<high;j++)
{
cin>>mat[i][j];
if(mat[i][j]=='2')
{
stt.x=i;
stt.y=j;
}
else if(mat[i][j]=='3')
{
edd.x=i;
edd.y=j;
}
}
}
cout<<bfs(stt,edd)<<endl;
}
return 0;
}
可是1728却死活改不对了,sad。。。。