您的位置:首页 > 其它

hdoj 5652 India and China Origins 【二分 + BFS】

2016-03-29 15:24 351 查看
题目链接:hdoj 5652 India and China Origins

问题描述

很久以前,中国和印度之间并没有喜马拉雅山相隔,两国的文化交流很频繁。随着喜马拉雅山海拔逐渐增加,两个地区的交流也越来越少,最终没有了来往。



假设当时的地形和我画的一样,蓝色部分代表海洋,而且当时人们还没有发明轮船。黄色部分代表沙漠,而且沙漠上经常有野鬼散步,所以人们不敢到沙漠中行走。黑色的格子表示山峰,这些山峰都无比高大,所以人无法穿过。白色格子代表平原,人可以在平原上自由行走。人每次可以向相邻的四个格子走动。

此外,我们的考古学家发现还有一些山峰会逐渐形成,通过研究发现,位置在 (x, y)(x,y) (保证该位置之前没有山峰)的地方在 ii 年后出现了山峰。现在给你若干个位置出现山峰的时间,你可以计算出中国和印度之间的联系最早被彻底切断的时间吗?

并查集估计十有八九菜鸡也不会写,直接用二分 + BFS搞了。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#define PI acos(-1.0)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define fi first
#define se second
#define ll o<<1
#define rr o<<1|1
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 3*1e5+10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
void add(LL &x, LL y) { x += y; x %= MOD; }
bool vis[510][510];
int fp[510][510];
int N, M;
struct Node {
int x, y;
};
char Map[600][600];
int Move[4][2] = {1,0, 0,-1, 0,1, -1,0};
bool Check(int x, int y, int time) {
if(fp[x][y] && fp[x][y] <= time) return false;
return x >= 0 && x < N && y >= 0 && y < M && vis[x][y] == false && Map[x][y] != '1';
}
bool judge(int time) {
queue<Node> Q; CLR(vis, false);
Node now, next;
for(int i = 0; i < M; i++) {
//cout << fp[pii(0, i)] << endl;
if(fp[0][i] && fp[0][i] <= time) continue;
else if(Map[0][i] != '1') {
vis[0][i] = true;
now.x = 0; now.y = i;
Q.push(now);
}
}
while(!Q.empty()) {
now = Q.front(); Q.pop();
if(now.x == N-1) {
return false;
}
for(int i = 0; i < 4; i++) {
next.x = now.x + Move[i][0];
next.y = now.y + Move[i][1];
if(Check(next.x, next.y, time)) {
Q.push(next);
vis[next.x][next.y] = true;
}
}
}
return true;
}
int main()
{
int t; scanf("%d", &t);
while(t--) {
scanf("%d%d", &N, &M);
for(int i = 0; i < N; i++) {
scanf("%s", Map[i]);
}
int Q; scanf("%d", &Q);
CLR(fp, 0);
for(int i = 1; i <= Q; i++) {
int x, y;
scanf("%d%d", &x, &y);
fp[x][y] = i;
}
int l = 0, r = Q; int ans = -1;
while(r >= l) {
int mid = (l + r) >> 1;
if(judge(mid)) {
ans = mid;
r = mid - 1;
}
else {
l = mid + 1;
}
}
printf("%d\n", ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: