Summary Files Commits Branches @commit @diff @stats
path: root / Year 2 / Design and analysis of algorithms / Practicum / Exam2 / 1-Office-SecondTry.cpp
blob: cc9a73e9bedadf4a3833b67d77ff63b24e0497a7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <iostream>
#include <string>
#include <queue>
#include <vector>
#include <algorithm>
#include <utility>
using namespace std;

/*
 * .....# 6 + 7 + 8 + 9 + 10 = 21 + 19 = 40
 * .###.# 5
 * ...#.# 4 + 5 + 6 = 15
 * .##### 3
 * ..^..# 2 + 1 + 0 + 1 + 2 = 6
 * ######
 *
 *
3 3 1
.##
...
##.
1 1


10 10 10
+.####...#
####.#.##.
...#....##
.###......
###.....#.
..#.......
##.......#
..#.##.###
...###.##.
.#...#...#
1 1
1 7
2 10
3 1
6 1
8 1
9 10
10 8
5 2
1 3


 */

typedef pair<long long, long long> loc;
loc operator+(const loc& l1, const loc& l2) {
	return { l1.first + l2.first, l1.second + l2.second };
}

long long N, M, K;
loc dir[] = { {1, 0}, {-1, 0}, {0, 1}, {0, -1}};


int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	cin >> N >> M >> K;
	string s;
	vector<string> matrix;
	for (long long i = 0; i < N; i++) {
		cin >> s;
		matrix.push_back(s);
	}
	for (long long i = 0; i < K; i++) {
		long long ri, ci;
		cin >> ri >> ci;
		ri--; ci--;
		if (matrix[ri][ci] != '#') matrix[ri][ci] = '+';
	}

	long long sum = 0;
	for (long long i = 0; i < N; i++) {
		for (long long j = 0; j < M; j++) {
			if (matrix[i][j] != '.') continue;

			vector<vector<bool>> visited(N, vector<bool>(M, false));
			queue<loc> bfs;
			bfs.push({ i, j });
			bfs.push({-1, -1});
			long long level = 1;
			bool stop = false;
			while (!bfs.empty() && !stop) {
				if (bfs.front() == loc({-1,-1})) {
					level++;
					bfs.pop();
					continue;
				}
				loc c = bfs.front();
				bfs.pop();
				visited[c.first][c.second] = true;
				bool added = false;
				for (int i = 0; i < 4; i++) {
					loc ne = c + dir[i];
					if (0 > ne.first || ne.first >= N || 0 > ne.second || ne.second >= M || visited[ne.first][ne.second] || matrix[ne.first][ne.second] == '#') continue;
					if (matrix[ne.first][ne.second] == '+') {
						stop = true;
						break;
					}
					bfs.push(ne);
					added = true;
				}
				if (added) bfs.push({ -1, -1 });
			}
			sum += level;
		}
	}
	cout << sum << endl;
}