Algorithm
The Cereal Guy's friend Serial Guy likes to watch soap operas. An episode is about to start, and he hasn't washed his plate yet. But he decided to at least put in under the tap to be filled with water. The plate can be represented by a parallelepiped k × n × m, that is, it has k layers (the first layer is the upper one), each of which is a rectangle n × m with empty squares ('.') and obstacles ('#'). The water can only be present in the empty squares. The tap is positioned above the square (x, y) of the first layer, it is guaranteed that this square is empty. Every minute a cubical unit of water falls into the plate. Find out in how many minutes the Serial Guy should unglue himself from the soap opera and turn the water off for it not to overfill the plate. That is, you should find the moment of time when the plate is absolutely full and is going to be overfilled in the next moment.
Note: the water fills all the area within reach (see sample 4). Water flows in each of the 6 directions, through faces of 1 × 1 × 1 cubes.
The first line contains three numbers k, n, m (1 ≤ k, n, m ≤ 10) which are the sizes of the plate. Then follow k rectangles consisting of n lines each containing m characters '.' or '#', which represents the "layers" of the plate in the order from the top to the bottom. The rectangles are separated by empty lines (see the samples). The last line contains x and y (1 ≤ x ≤ n, 1 ≤ y ≤ m) which are the tap's coordinates. x is the number of the line and y is the number of the column. Lines of each layer are numbered from left to right by the integers from 1 to n, columns of each layer are numbered from top to bottom by the integers from 1 to m.
The answer should contain a single number, showing in how many minutes the plate will be filled.
1 1 1
.
1 1
1
2 1 1
.
#
1 1
1
2 2 2
.#
##
..
..
1 1
5
3 2 2
#.
##
#.
.#
..
..
1 2
7
3 3 3
.#.
###
##.
.##
###
##.
...
...
...
1 1
13
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <iostream>
#include <vector>
void dfs(int h, int row, int col, std::vector<std::vector<std::vector<bool> > > &g, int &count){
if(h < 0 || h >= g.size() || row < 0 || row >= g[0].size() || col < 0 || col >= g[0][0].size()){return;}
if(!g[h][row][col]){return;}
g[h][row][col] = 0; ++count;
dfs(h - 1, row, col, g, count);
dfs(h + 1, row, col, g, count);
dfs(h, row - 1, col, g, count);
dfs(h, row + 1, col, g, count);
dfs(h, row, col - 1, g, count);
dfs(h, row, col + 1, g, count);
return;
}
int main(){
int k, n, m; scanf("%d %d %d", &k, &n, &m);
std::vector<std::vector<std::vector<bool> > > g(k, std::vector<std::vector<bool> >(n, std::vector<bool>(m, 0)));
for(int p = 0; p < k; p++){
for(int q = 0; q < n; q++){
std::string s; std::cin >> s;
for(int r = 0; r < m; r++){g[p][q][r] = (s[r] == '.');}
}
}
int x, y; scanf("%d %d", &x, &y);
--x; --y;
int count(0);
dfs(0, x, y, g, count);
printf("%d\n", count);
return 0;
}
Copy The Code &
Try With Live Editor
Input
.
1 1
Output
Demonstration
Codeforces Solution-B. Serial Time!-Solution in C, C++, Java, Python