您的位置:首页 > 其它

hdu5652 India and China Origins 深搜+二分

2016-03-28 16:59 393 查看


India and China Origins

Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 777 Accepted Submission(s): 259



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


Source

BestCoder Round #77 (div.2)

二分加BFS,就是有种情况有点含糊

input:

1

1 3

101

1

0 1

output:

0

以为输出1呢,结果刚开始就走不通,原来刚开始必须走一步,不然算不行

#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>

using namespace std;

struct node {
int x, y;
node() {}
node(int x, int y) : x(x), y(y) {}
};

int N, M, Q;
char mapp[505][505];
int op[2][505 * 505];
bool vis[505][505];

int dir[4][2] = { 0, 1, 1, 0, -1, 0, 0, -1 };

bool check(int x, int y) {
if (x < 0 || x >= N || y < 0 || y >= M || vis[x][y])
return false;
return true;
}

bool bfs(int pos) {
memset(vis, false, sizeof(vis));
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (mapp[i][j] == '1') {
vis[i][j] = true;
}
}
}
for (int i = 1; i <= pos; i++) {
vis[op[0][i]][op[1][i]] = true;
}
queue<node> q;
for (int i = 0; i < M; i++) {
if (!vis[0][i]) {
node fr = node(0, i);
vis[0][i] = true;
q.push(fr);

while (!q.empty()) {
node tn = q.front(); q.pop();
for (int j = 0; j < 4; j++) {
int nx = tn.x + dir[j][0];
int ny = tn.y + dir[j][1];
if (check(nx, ny)) {
//下一步找到直接返回true
if (nx == N - 1) return true;
vis[nx][ny] = true;
q.push(node(nx, ny));
}
}
}
}
}
//所有情况都试过了都不行,返回false
return false;
}

int main()
{
int T;
scanf("%d", &T);
while (T--) {
scanf("%d%d", &N, &M);
for (int i = 0; i < N; i++) {
scanf("%s", mapp[i]);
}
scanf("%d", &Q);
for (int i = 1; i <= Q; i++) {
scanf("%d%d", &op[0][i], &op[1][i]);
}

//刚开始就不行,直接打印0
if (!bfs(0)) {
puts("0");
continue;
}
int l = 1, r = Q;
//二分新生成的山峰时间的区间
while (r - 1 > l) {
int m = (l + r) / 2;
if (bfs(m)) {
l = m;
}
else {
r = m;
}
}
//没有必要判l,因为二分完了以后l肯定是可以联通的一年
//而r才有可能是不连通的一年
if (!bfs(r)) {
printf("%d\n", r);
}
else {
puts("-1");
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: