7 8 #.#####. #.a#..r. #..#x... ..#..#.# #...##.. .#...... ........
Sample Output
13
给你r为起点,a为终点,#为墙壁,.为路径,遇到x则step+1,作优先队列自定义,优先step短的,然后广搜,每一次访问下一个结点取最小的step返回。
#include
#include
#include
#include
#define N 205
using namespace std;
int vist[N][N];
char map[N][N];
int dir[4][2]= {{0,1},{0,-1},{1,0},{-1,0}};
int n,m,k,sum,flag,sx,sy,ex,ey;
struct node
{
int x,y;
int step;
friend bool operator<(node a1,node a2)
{
return a2.step=n||b<0||b>=m||map[a][b]=='#'||!vist[a][b])
return 1;
return 0;
}
int bfs()
{
int i;
priority_queueQ;
node st,ed;
st.x=sx;
st.y=sy;
st.step=0;
vist[st.x][st.y]=1;
Q.push(st);
while(!Q.empty())
{
st=Q.top();
Q.pop();
if(st.x==ex&&st.y==ey)
return st.step;
for(i=0; i<4; i++)
{
ed=st;
ed.x=st.x+dir[i][0];
ed.y=st.y+dir[i][1];
if(check(ed.x,ed.y))
continue;
ed.step++;
if(map[ed.x][ed.y]=='x')
ed.step++;
if(vist[ed.x][ed.y]>ed.step)
{
vist[ed.x][ed.y]=ed.step;//取最小时间
Q.push(ed);
}
}
}
return 0;
}
int main()
{
int i,j;
while(~scanf("%d%d",&n,&m))
{
for(i=0; i