捞!(玄关一个)
  • 板块P2802 回家
  • 楼主AK_CSPS
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/5/3 15:04
  • 上次更新2024/5/3 17:17:49
查看原帖
捞!(玄关一个)
933895
AK_CSPS楼主2024/5/3 15:04
#include <bits/stdc++.h>
using namespace std;
struct node
{
	int x,y,hp,step;
}t,k;
int x,y,n,m,mapp[10][10],d[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
bool book[10][10];
queue <node> que;
void bfs(int x,int y)
{
	book[x][y]=1;
	t.x=x,t.y=y,t.hp=6,t.step=0;
	que.push(t);
	while(!que.empty())
	{
		t=que.front();
		que.pop();
		if(t.hp==0)
		{
			cout<<-1;
			return;
		}
		if(mapp[t.x][t.y]==4)
		{
			cout<<1;
			t.hp=6;
		} 
		if(mapp[t.x][t.y]==3) 
		{
			cout<<t.step;
			return;
		}
		for(int i=0;i<4;i++)
		{
			int nx=t.x+d[i][0],ny=t.y+d[i][1];
			if(nx<1||nx>n||ny<1||ny>m) continue;
			if(book[nx][ny]==0&&mapp[nx][ny]!=0)
			{
				book[nx][ny]=1;
				k.x=nx,k.y=ny,k.hp=t.hp-1,k.step=t.step+1;
				que.push(k);
			}
		}
	}
	cout<<-1;
}
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			cin>>mapp[i][j];
			if(mapp[i][j]==2) x=i,y=j;
		}
	}
	bfs(x,y);
	return 0;
}
```cpp
2024/5/3 15:04
加载中...