http://codeforces.com/contest/598/problem/D
http://www.chenguanghe.com/educational-codeforces-round-1-d-igor-in-the-museum/
Igor is in the museum and he wants to see as many pictures as possible.
Museum can be represented as a rectangular field of n × m cells. Each cell is either empty or impassable. Empty cells are marked with '.', impassable cells are marked with '*'. Every two adjacent cells of different types (one empty and one impassable) are divided by a wall containing one picture.
At the beginning Igor is in some empty cell. At every moment he can move to any empty cell that share a side with the current one.
For several starting positions you should calculate the maximum number of pictures that Igor can see. Igor is able to see the picture only if he is in the cell adjacent to the wall with this picture. Igor have a lot of time, so he will examine every picture he can see.
Input
First line of the input contains three integers n, m and k (3 ≤ n, m ≤ 1000, 1 ≤ k ≤ min(n·m, 100 000)) — the museum dimensions and the number of starting positions to process.
Each of the next n lines contains m symbols '.', '*' — the description of the museum. It is guaranteed that all border cells are impassable, so Igor can't go out from the museum.
Each of the last k lines contains two integers x and y (1 ≤ x ≤ n, 1 ≤ y ≤ m) — the row and the column of one of Igor's starting positions respectively. Rows are numbered from top to bottom, columns — from left to right. It is guaranteed that all starting positions are empty cells.
Output
Print k integers — the maximum number of pictures, that Igor can see if he starts in corresponding position.
Examples
input
5 6 3 ****** *..*.* ****** *....* ****** 2 2 2 5 4 3
output
6 4 10
input
4 4 1 **** *..* *.** **** 3 2
output
8
题目大意: 给一个图, 点(.) 是人可以待的位置, 星号(*) 是墙壁, 找出点和星号相邻的可能有多少个.
分析: 上来flood fill, 秒挂testcase 10, 加了visited[][] 秒挂testcase15, 后来发现里面重复请求非常多 真是!@#@!#!, 于是记录一下每次请求的结果. 原想是用uf记录, 后来想了想有现成的k, 就用k记录了…一个hashmap来的比uf方便的多.
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.readInt();
int m = in.readInt();
int k = in.readInt();
char[][] map = new char[n][m];
int[][] visited = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
map[i][j] = in.readCharacter();
}
}
Map<Integer,Integer> list = new HashMap<>(); //记录一下已经算过的
for (int i = 0; i < k; i++) {
int t1 = in.readInt() - 1;
int t2 = in.readInt() - 1;
if (visited[t1][t2] > 0) {//如果已经算过了, 找出第几次算的答案
out.printLine(list.get(visited[t1][t2]));
} else {//如果没算过
int ans = solve(map, t1, t2, visited, i + 1);//从1开始到k, 每次
out.printLine(ans);
list.put(i+1,ans);//记录一下算的结果
}
}
}
private int solve(char[][] map, int i, int j, int[][] visited, int id) {
Queue<Pair<Integer, Integer>> queue = new LinkedList<>();
queue.add(Pair.makePair(i, j));
int count = 0;
while (!queue.isEmpty()) {
Pair<Integer, Integer> cur = queue.poll();
if (visited[cur.first][cur.second] > 0) {
continue;
}
visited[cur.first][cur.second] = id;
if (map[cur.first + 1][cur.second] == '.') {
queue.add(Pair.makePair(cur.first + 1, cur.second));
} else {
count++;
}
if (map[cur.first - 1][cur.second] == '.') {
queue.add(Pair.makePair(cur.first - 1, cur.second));
} else {
count++;
}
if (map[cur.first][cur.second + 1] == '.') {
queue.add(Pair.makePair(cur.first, cur.second + 1));
} else {
count++;
}
if (map[cur.first][cur.second - 1] == '.') {
queue.add(Pair.makePair(cur.first, cur.second - 1));
} else {
count++;
}
}
return count;
}