玄关两个
Code:
#include<bits/stdc++.h>
using namespace std;
struct pos
{
int x;
int y;
int step;
int health;
};
int a[11][11];
int n,m;
bool flag[11][11]={false};
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
queue<pos>q;
int main()
{
int i,j;
cin>>n>>m;
int startx,starty,endx,endy;
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
cin>>a[i][j];
if(a[i][j]==2)
{
startx=i;
starty=j;
}
else if(a[i][j]==3)
{
endx=i;
endy=j;
}
}
}
q.push({startx,starty,1,6});
flag[startx][starty]=true;
while(!q.empty())
{
int hl=q.front().health-1;
if(hl==0)
{
cout<<-1;
return 0;
}
if(a[q.front().x][q.front().y]==3)
{
cout<<q.front().step-1;
return 0;
}
for(i=0;i<4;i++)
{
int xx=q.front().x+dx[i];
int yy=q.front().y+dy[i];
int st=q.front().step+1;
if((a[xx][yy]==1 || a[xx][yy]==4)&&((xx>=1&&xx<=n)&&(yy>=1&&yy<=m))&&flag[xx][yy]==false)
{
if(a[xx][yy]==4)
{
q.push({xx,yy,st,6});
flag[xx][yy]=true;
}
else
{
q.push({xx,yy,st,hl});
flag[xx][yy]=true;
}
}
}
q.pop();
}
}
/*
3 3
2 1 1
1 1 0
1 1 3
*/