社区讨论

蒟蒻求助 90 WA

P2802回家参与者 3已保存回复 2

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
2 条
当前快照
1 份
快照标识符
@mi6xg2gk
此快照首次捕获于
2025/11/20 12:24
4 个月前
此快照最后确认于
2025/11/20 12:24
4 个月前
查看原帖
期望 8 读入 -1
代码如下
CPP
#include<algorithm>
#include<iostream>
#include<stdio.h>
using namespace std;
int n,m,i,j,x,y;
int a[1005][1005];
bool o[1005][1005];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
struct qwq
{
	int x,y;
	int hp;
	int t;
}f[100005];
int bfs(int x,int y)
{
	int head=0,tail=1;
	f[1].x=x;
	f[1].y=y;
	f[1].hp=6;
	f[1].t=0;
	o[x][y]=true;
	int i;
	while (head<=tail)
	{
		head++;
		if (a[f[head].x][f[head].y]==3) {return f[head].t;}
		if (f[head].hp>1)
			for (int i=0; i<4; i++)
			{
				int tempx,tempy;
				tempx=f[head].x+dx[i];
				tempy=f[head].y+dy[i];
				if (!o[tempx][tempy] && tempx>0 && tempx<=n+1 && tempy>0 && tempy<=m+1 && a[tempx][tempy]!=0)
				{
					tail++;
					f[tail].x=tempx;
					f[tail].y=tempy;
					if (a[tempx][tempy]==4) f[tail].hp=6; else f[tail].hp=f[head].hp-1;
					f[tail].t=f[head].t+1;
					o[tempx][tempy]=true;
				}
			}
	}
	return -1;
}
int main()
{
	std::ios::sync_with_stdio(false);
	cin>>n>>m;
	for (i=1; i<=n; i++)
		for (j=1; j<=m; j++) 
		{
			cin>>a[i][j];
			if (a[i][j]==2) {x=i; y=j;}
		}
	cout<<bfs(x,y)<<endl;
	return 0;
}

回复

2 条回复,欢迎继续交流。

正在加载回复...