bfs20分求助,2玄关
  • 板块P2802 回家
  • 楼主laoqiu_tyxx
  • 当前回复4
  • 已保存回复4
  • 发布时间2024/7/14 18:26
  • 上次更新2024/7/14 20:04:57
查看原帖
bfs20分求助,2玄关
1301485
laoqiu_tyxx楼主2024/7/14 18:26

玄关两个
Code: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
*/
2024/7/14 18:26
加载中...