Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import static java.lang.Integer.parseInt;
- import java.util.ArrayDeque;
- import java.util.Queue;
- import java.util.StringTokenizer;
- public class D_Igor_In_the_Museum {
- static BufferedReader reader;
- static StringTokenizer tok;
- static int n, m, k, count, moveX[] = {-1, 1, 0, 0}, moveY[] = {0, 0, 1, -1};
- static char grid[][];
- static boolean visited[][];
- static Queue<pair> q, qK;
- static BufferedWriter writer;
- public static void main(String[] args) throws IOException {
- Input();
- writer = new BufferedWriter(new OutputStreamWriter(System.out));
- while (!qK.isEmpty()) {
- pair obj = qK.poll();
- procce(obj.x, obj.y);
- }
- writer.flush();
- }
- public static void Input() throws IOException {
- reader = new BufferedReader(new InputStreamReader(System.in));
- tok = new StringTokenizer(reader.readLine());
- n = parseInt(tok.nextToken()) + 1;
- m = parseInt(tok.nextToken()) + 1;
- k = parseInt(tok.nextToken());
- grid = new char[n][m];
- for (int i = 1; i < n; i++) {
- grid[i] = ('x' + reader.readLine()).toCharArray();
- }
- qK = new ArrayDeque<pair>();
- for (int tc = 0; tc < k; tc++) {
- tok = new StringTokenizer(reader.readLine());
- qK.add(new pair(parseInt(tok.nextToken()), parseInt(tok.nextToken())));
- }
- }
- public static void procce(int x, int y) throws IOException {
- q = new ArrayDeque<pair>();
- q.add(new pair(x, y));
- visited = new boolean[n][m];
- visited[x][y] = true;
- count = 0;
- while (!q.isEmpty()) {
- pair obj = q.poll();
- for (int i = 0; i < 4; i++) {
- int newX = obj.x + moveX[i], newY = obj.y + moveY[i];
- if (!(newX < 1 || newX >= n || newY < 1 || newY >= m || visited[newX][newY])) {
- switch (grid[newX][newY]) {
- case '.':
- visited[newX][newY] = true;
- q.add(new pair(newX, newY));
- break;
- default:
- count++;
- break;
- }
- }
- }
- }
- writer.write( count +"\n" );
- }
- static class pair {
- int x, y;
- public pair(int x, int y) {
- this.x = x;
- this.y = y;
- }
- }
- }
- /*
- 5 6 3
- ******
- *..*.*
- ******
- *....*
- ******
- 2 2
- 2 5
- 4 3
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement