luogu P1902 刺杀大使

一道水题

主要是较久没有系统的做过题了,从简单的开始吧;
二分答案 + BFS
因为我们可以弄一万个人来搞
所以我们只需要路径上的最大值就可以了
一般这种最值可能会用到二分答案
所依旧是二分答案 + 搜索
没什么好说的
就是第一次忘记清空数组和队列然后gg了
之后就好了

#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;
const int maxn = 1e3 + 5;
int n, m, p[maxn][maxn], map[maxn][maxn], l, r, mid;
int fx[4] = {0, 0, 1, -1};
int fy[4] = {1, -1, 0, 0};
struct node{
	int x, y, val;
};
queue <node> q;
bool BFS(){
	node start, tmp;
	start.x = start.y = 1; start.val = 0;
	memset(map, 0, sizeof(map));
	while (q.size()) q.pop();
	map[1][1] = 1;
	q.push(start);
	while (q.size()){
		start = q.front(); q.pop();
		if (start.x == n) return true;
		for (int i = 0; i < 4; i++){
			int x = start.x + fx[i], y = start.y + fy[i];
			if (!map[x][y] && p[x][y] <= mid && x <= n && x >= 1 && y >=1 && y <= m){
				map[x][y] = 1;
				tmp.x = x; tmp.y = y; tmp.val = max(start.val, p[x][y]);
				q.push(tmp);
			}
		}
	}
	return false;
}
int main(){
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++){
			scanf("%d", &p[i][j]);
			if (p[i][j] > r) r = p[i][j];
		}
	while (l < r){
		mid = (l + r) / 2;
		if (BFS()) r = mid;
		else l = mid + 1;
	}
	printf("%d", l);
	return 0;
}

做的时候弄了好多纸张的错误Orz
以后争取做一个题写shui一篇blog???

原文地址:https://www.cnblogs.com/sxy2004/p/14280418.html