求助大佬。全WA
  • 板块P1902 刺杀大使
  • 楼主Kalso42
  • 当前回复0
  • 已保存回复0
  • 发布时间2023/11/21 09:53
  • 上次更新2023/11/21 15:31:43
查看原帖
求助大佬。全WA
1153763
Kalso42楼主2023/11/21 09:53

用BFS+二分的常规做法,检查了巨久,代码写得思路也很清晰,不知道为什么全WA

#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
int n, m, ans;
const int N = 1e3+10;
int a[N][N];
bool vis[N][N];
bool bfs(int x)	//搜索是否可行
{
	queue<pair<int, int>> q;
	q.push({1, 1});
	vis[1][1] = true;
	while (!q.empty())
	{
		auto pair = q.front();
		q.pop();
		int xx = pair.first;
		int yy = pair.second;		//x为行,y为列
		if (xx == n)
		{
			memset(vis, 0, sizeof vis);
			return true;
		}
		else
		{
			if (a[xx+1][yy] <= x && xx+1 <= n && !vis[xx+1][yy])
			{
				q.push({xx+1, yy});
				vis[xx+1][yy] = true;
			}
			if (a[xx-1][yy] <= x && xx-1 >= 1 &&!vis[xx-1][yy])
			{
				q.push({xx-1, yy});
				vis[xx-1][yy] = true;
			}
			if (a[xx][yy+1] <= x && yy+1 <= m && !vis[xx][yy+1])
			{
				q.push({xx, yy+1});
				vis[xx][yy+1] = true;
			}
			if (a[xx][yy-1] <= x && yy-1 >= 1&&!vis[xx][yy-1])
			{
				q.push({xx, yy-1});
				vis[xx][yy-1] = true;
			}
		}
	}
	return false;
}
int main()
{
	int mx = 0;
	cin >> n >> m;
	for (int i=1; i<=n; i++)
		for (int j=1; j<=m; j++)
		{
			cin >> a[i][j];
			mx = max(mx, a[i][j]);
		}
	//二分查答案
	int l=0, r=mx, mid;
	while (l<=r)
	{
		mid = (l+r)/2;
		if (bfs(mid))
		{
			ans = mid;
			r = mid-1;
		}
		else
			l = mid+1;
	}
	cout << ans << endl;
}

给大佬磕头了 orz

2023/11/21 09:53
加载中...