루미 알고리즘

#1593골드Map of Sweden

PO 2024 온라인 예선코딩출제 Joshua Andersson영어 원문이 문제가 어렵다면 — 배우는 순서 보기

영어 원문입니다. 원문의 수식·표를 글자로 옮겨 모양이 다를 수 있습니다. 정확한 내용은 원문 페이지에서 확인하세요. 원문 페이지 열기 (새 창)

Problem

Vidar wants to know how large Sweden is. After a lot of effort, he has managed to launch a satellite into the atmosphere that can take several low-resolution images of Sweden for him. Unfortunately, the satellite's camera is very poor. Each square in the image will either consist of “#”, which means the square contains land, or “.” if it contains water.

Vidar is a huge scaredy-cat when it comes to swimming, so in his opinion, Sweden's landmass is only the number of land squares that can be reached from Stockholm without ever swimming. More precisely, it is the number of squares that can be reached from Stockholm by moving upward, rightward, downward, or leftward without ever walking over a square that contains water. Note that the square containing Stockholm, S, also counts as land.

Vidar now wants your help to write a program that calculates Sweden's landmass given a satellite image where he has marked the location of Stockholm with an S. Then, he will wait for the satellite to take pictures from new angles. By looking at the new images, he sometimes realizes that a square that used to be water is actually land. When he enters this change into your program, you should immediately print new landmass.

Input

The first line of the input contains an integer R (1 ≤ R ≤ 500), the number of rows in Vidar's image.

The next line contains an integer C (1 ≤ C ≤ 500), the number of columns in Vidar's image.

The next line contains an integer U (0 ≤ U ≤ 100,000), the number of times Vidar changes a square from being water to land.

Then follow R lines, each containing C letters, describing the satellite image Vidar is working with. Each letter is either “#”, “.” or “S”. Exactly one square will contain the letter S.

If U is not 0, U lines will follow. Each one will contain two integers i, j (1 ≤ i ≤ R, 1 ≤ j ≤ C), which means the square in the i-th row and j-th column is changed from water to land.

Output

First, print an integer: Sweden's total landmass. Then print U integers: Sweden's total landmass after each time Vidar changes the image.

Points

Your solution will be tested on several test case groups. To get the points for a group, it must pass all the test cases in the group.

GroupPoint valueConstraints
120U=0 and the subtask consist of a single testcase, the one on our poster (https://www.progolymp.se/2024/affisch.pdf).
213R=1, U=0
39R=1
426U=0
513R,C ≤ 10
619No additional constraints.

Explanation of sample 1

At first, it's possible to reach 8 land squares from Stockholm. Since Stockholm is also counted as land, Sweden's landmass is 9. After the first update, the grid looks like this:

  .#...#
  .###..
  .#S##.
  ..##..
  ......

Since you can now reach a new piece of land, the answer becomes 10. After the next two updates, you can't reach the new land, so the answer does not change. After the final update, you can reach the newly created land, and the map looks like this:

  .#...#
  .###..
  .#S##.
  ..###.
  ....##

예제

예제 1

입력
5
6
4
.....#
.###..
.#S##.
..##..
......
1 2
5 6
5 5
4 5
출력
9
10
10
10
13

예제 2

입력
18
20
0
....................
...........######...
.........########...
........##########..
.......###########..
......##########....
.....#########......
.....#########......
...########.........
...#######..........
...######...........
...######...........
...#########..#.....
..######S#..........
..#######....#......
...#####.#..#.......
....##..............
....................
출력
121

예제 3

입력
1
5
3
#S...
1 3
1 5
1 4
출력
2
3
3
5

출처 표기

출처: Programmeringsolympiaden 2024년 온라인 예선 · 원문 페이지 · 이용 조건 CC BY-SA · LaTeX 원문을 화면용 글로 옮겼습니다(수식·표의 모양이 다를 수 있습니다). 내용은 고치지 않았습니다.

크리에이티브 커먼즈 저작자표시-동일조건변경허락.

문제 출처: 한국정보올림피아드(KOI) (CC BY-NC-SA 4.0) · International Olympiad in Informatics (IOI) (CC BY) · Centre for Education in Mathematics and Computing (CEMC), University of Waterloo (CC BY-NC 4.0) · Programmeringsolympiaden (CC BY-SA) · CSES Problem Set (Antti Laaksonen) (CC BY-NC-SA 4.0). 루미 알고리즘은 위 단체들과 관계없는 별도의 서비스이며, 자료는 원문 그대로 싣습니다. 문제마다 출처와 이용 조건을 따로 표기합니다.