HDU 5652 (二分 bfs)
2016-03-26 23:29
423 查看
India and China Origins
Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 237 Accepted Submission(s): 73
Problem Description
A long time ago there are no himalayas between India and China, the both cultures are frequently exchanged and are kept in sync at that time, but eventually himalayas rise up. With that at first the communation started to reduce and eventually died.
Let's assume from my crude drawing that the only way to reaching from India to China or viceversa is through that grid, blue portion is the ocean and people haven't yet invented the ship. and the yellow portion is desert and has ghosts roaming around so people
can't travel that way. and the black portions are the location which have mountains and white portions are plateau which are suitable for travelling. moutains are very big to get to the top, height of these mountains is infinite. So if there is mountain between
two white portions you can't travel by climbing the mountain.
And at each step people can go to 4 adjacent positions.
Our archeologists have taken sample of each mountain and estimated at which point they rise up at that place. So given the times at which each mountains rised up you have to tell at which time the communication between India and China got completely cut off.
Input
There are multi test cases. the first line is a sinle integer T which
represents the number of test cases.
For each test case, the first line contains two space seperated integers N,M.
next N lines
consists of strings composed of 0,1 characters. 1 denoting
that there's already a mountain at that place, 0 denoting
the plateau. on N+2 line
there will be an integer Q denoting
the number of mountains that rised up in the order of times. Next Q lines
contain 2 space
seperated integers X,Y denoting
that at ith year a mountain rised up at location X,Y.
T≤10
1≤N≤500
1≤M≤500
1≤Q≤N∗M
0≤X<N
0≤Y<M
Output
Single line at which year the communication got cut off.
print -1 if these two countries still connected in the end.
Hint:
From the picture above, we can see that China and India have no communication since 4th year.
Sample Input
1
4 6
011010
000010
100001
001000
7
0 3
1 5
1 3
0 0
1 2
2 4
2 1
Sample Output
4
题意:给出一个地图1表示不连通0表示联通,q个操作,每次给一个格子设为1,问最早使得地图上面到下面不连通的操作.
无脑二分操作,每次从第一排bfs看是否能到达最后一排.
坑:注意输出0和-1的情况.
#include <cstdio> #include <iostream> #include <cstring> #include <cmath> #include <algorithm> #include <queue> using namespace std; #define maxn 511 #define move Move char mp[maxn][maxn]; int op[maxn*maxn][2]; int n, m, q; const int move[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; bool vis[maxn][maxn]; bool legal (int x, int y) { if (x < 0 || y < 0 || x >= n || y >= m) return 0; return 1; } struct node { int x, y; }; node gg[maxn*maxn]; int tou, wei; bool ok (int pos) {//是否联通 memset (vis, 0, sizeof vis); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (mp[i][j] == '1') {//cout << ".." << endl; vis[i][j] = 1; } } } for (int i = 1; i <= pos; i++) { vis[op[i][0]][op[i][1]] = 1; } for (int i = 0; i < m; i++) { if (!vis[0][i]) { tou = wei = 0; gg[wei++] = (node) {0, i}; vis[0][i] = 1; while (tou < wei) { node now = gg[tou++]; //cout << now.x << " " << now.y << endl; for (int i = 0; i < 4; i++) { node next; next.x = now.x+move[i][0]; next.y = now.y+move[i][1]; if (!legal (next.x, next.y) || vis[next.x][next.y]) continue; if (next.x == n-1) return 1; gg[wei++] = next; vis[next.x][next.y] = 1; } } } } return 0; } int main () { //freopen ("in.txt", "r", stdin); int t; scanf ("%d", &t); while (t--) { scanf ("%d%d", &n, &m); for (int i = 0; i < n; i++) scanf ("%s", mp[i]); scanf ("%d", &q); //cout << q << endl; for (int i = 1; i <= q; i++) { scanf ("%d%d", &op[i][0], &op[i][1]); } if (!ok (0)) { printf ("0\n"); continue; } int l = 1, r = q; while (r-l > 1) { int mid = (l+r)>>1; if (ok (mid)) l = mid; else r = mid; } if (!ok (l)) printf ("%d\n", l); else if (!ok (r)) printf ("%d\n", r); else printf ("-1\n"); } return 0; }
相关文章推荐
- java学习笔记16.3.26
- Linux内核协议栈-主要函数调用栈
- MyEclipse------PreparedStatement使用方法
- HDU5653 Bomber Man wants to bomb an Array 简单DP
- Linux 内核源代码情景分析 chap 2 存储管理(一)
- Android——GridView
- C++中的数组
- linux下运行java项目
- linux yum命令详解
- js中的this
- github的简单学习之旅
- Retrofit学习一:文档介绍
- HDU 5651 (组合 水~)
- Android扩大图片的点击区域
- 行内元素与块级元素的区别
- sql视图实例
- 关于Integer大小比较的问题
- <div+css页面布局课堂笔记>6---与盒子相关的CSS属性第三部分
- 2279: 字符串分段(串)
- 记