1033. Labyrinth
Time Limit: 1.0 second
Memory Limit: 16 MB
Memory Limit: 16 MB
Administration of the labyrinth has decided to start a new season with new wallpapers. For this purpose they need a program to calculate the square of the walls inside the labyrinth. This job is just for you!
The labyrinth is represented by a matrix
N×
N (3 ≤
N ≤ 33, you see, ‘3’ is a magic digit!). Some matrix cells contain a dot character (‘.’) that denotes an empty square. Other cells contain a diesis character (‘#’) that denotes a square filled by monolith block of stone wall. All squares are of the same size 3×3 meters.
The walls are constructed around the labyrinth (except for the upper left and lower right corners, which are used as entrances) and on the cells with a diesis character. No other walls are constructed. There always will be a dot character at the upper left and lower right corner cells of the input matrix.
Your task is to calculate the square of visible part of the walls inside the labyrinth. In other words, the square of the walls' surface visible to a visitor of the labyrinth. Note that there's no holes to look or to move through between any two adjacent blocks of the wall. The blocks are considered to be adjacent if they touch each other in any corner. See picture for an example: visible walls inside the labyrinth are drawn with bold lines. The height of all the walls is 3 meters.
Input
The first line of the input contains the single number
N. The next
N lines contain
N characters each. Each line describes one row of the labyrinth matrix. In each line only dot and diesis characters will be used and each line will be finished with a new line character. There will be no spaces in the input.
Output
Your program should print to the output a single integer — the exact value of the square of the wallpaper needed.
Sample
input | output |
---|---|
5 ..... ...## ..#.. ..### ..... | 198 |
必须,左上角和右下角同时搜索。因为中间有可能会有一堵墙把上下2边隔开!
#include <iostream>
using namespace std;
int main()
{
int i, j, unknow, know, n, ans, x, y;
int dx[4] = {-1, 0, 0, 1},
dy[4] = {0, 1, -1, 0};
int ax[33*33], ay[33*33];
char s[35][35];
bool pass[35][35];
memset(pass, 0, 35*35*sizeof(bool));
cin>>n;
for (i=1; i<=n; i++)
{
s[0][i] = '#';
s[n+1][i] = '#';
s[i][0] = '#';
s[i][n+1] = '#';
}
for (i=1; i<=n; i++)
for (j=1; j<=n; j++)
cin>>s[i][j];
unknow = 2;
know = 0;
ans = 0;
ax[1] = ay[1] = 1;
ax[2] = ay[2] = n;
pass[1][1] = 1;
pass[n][n] = 1;
while (know < unknow)
{
know++;
for (i=0; i<4; i++)
{
x = ax[know] + dx[i];
y = ay[know] + dy[i];
if (s[x][y] - '#' == 0)
ans++;
else if (pass[x][y] == 0)
{
pass[x][y] = 1;
unknow++;
ax[unknow] = x;
ay[unknow] = y;
}
}
}
cout<<(ans - 4) * 9<<endl;
}