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代码:
问题描述
很久以前,中国和印度之间并没有喜马拉雅山相隔,两国的文化交流很频繁。随着喜马拉雅山海拔逐渐增加,两个地区的交流也越来越少,最终没有了来往。
假设当时的地形和我画的一样,蓝色部分代表海洋,而且当时人们还没有发明轮船。黄色部分代表沙漠,而且沙漠上经常有野鬼散步,所以人们不敢到沙漠中行走。黑色的格子表示山峰,这些山峰都无比高大,所以人无法穿过。白色格子代表平原,人可以在平原上自由行走。人每次可以向相邻的四个格子走动。
此外,我们的考古学家发现还有一些山峰会逐渐形成,通过研究发现,位置在 (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; }
相关文章推荐
- memcache介绍
- 30 继承(二)
- php高级变量
- Centos7 如何安装中文输入法
- XML(2)
- BugTags进行bug的管理
- 提升进入界面的速度
- 小学生之SpringMVC
- poj 2777 Count Color
- The Balance(母函数)
- hdoj 5651 xiaoxin juju needs help 【组合数学】
- 使用google guava做内存缓存
- Delphi多线程Timer
- 汇编语言学习笔记之通用寄存器
- 监控基础工作之JConsole远程连接JBossAs7.1.1
- android修改系统源码之实现蓝牙自动配对以及取消开机锁屏
- 安装 haproxy
- 新鲜出炉springmvc
- iOS完整学习路线图-对知识的回顾/整理
- Verify Preorder Serialization of a Binary Tree