Administration of the labyrinth has decided to start a new season with new wallpapers. For this purpose they need a program to calculate the surface area 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 area of visible part of the walls inside the labyrinth. In other words, the area 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.
The first line of the input contains the single number
N. The next
N lines contain
Ncharacters 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 terminated with a new line character. There will be no spaces in the input.
Your program should print to the output a single integer — the exact value of the area of the wallpaper needed.
input | output |
---|---|
5 ..... ...## ..#.. ..### ..... | 198 |
题解:
因为出口和入口可能不连通,只要从出口用bfs搜一遍再入口搜一遍,然后看已经遍历了的块周围的#或者边界的数目,因为出入口的4块不算,得到的结果-4再乘上每一块的面积9就是答案
代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<stdlib.h>
#include<cmath>
#include<string>
#include<algorithm>
#include<iostream>
#include<stdio.h>
using namespace std;
#define ll long long
#define maxn 100008
char s[35][35];
int vis[35][35];
struct node
{
int x,y;
};
int dirx[4]={0,0,-1,1};
int diry[4]={1,-1,0,0};
queue<node>q;
int main()
{
int n,i,j,k,d,xx,yy;
node now,next;
while(scanf("%d",&n)!=EOF)
{
while(!q.empty())q.pop();
memset(vis,0,sizeof(vis));
for(i=0;i<n;i++)
{
scanf("%s",s[i]);
}
now.x=0,now.y=0;
q.push(now);
vis[0][0]=1;
while(!q.empty())
{
now=q.front();
q.pop();
for(i=0;i<4;i++)
{
next.x=now.x+dirx[i];
next.y=now.y+diry[i];
if(next.x>=0&&next.x<n&&next.y>=0&&next.y<n&&s[next.x][next.y]=='.'&&!vis[next.x][next.y])
{
vis[next.x][next.y]=1;
q.push(next);
}
}
}
if(!vis[n-1][n-1])
{
now.x=n-1,now.y=n-1;
vis[n-1][n-1]=1;
q.push(now);
while(!q.empty())
{
now=q.front();
q.pop();
for(i=0;i<4;i++)
{
next.x=now.x+dirx[i];
next.y=now.y+diry[i];
if(next.x>=0&&next.x<n&&next.y>=0&&next.y<n&&s[next.x][next.y]=='.'&&!vis[next.x][next.y])
{
vis[next.x][next.y]=1;
q.push(next);
}
}
}
}
int ans=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(!vis[i][j])
continue;
for(k=0;k<4;k++)
{
xx=i+dirx[k];
yy=j+diry[k];
if(xx<0||yy<0||xx>=n||yy>=n||s[xx][yy]=='#')
ans++;
}
}
}
ans-=4;
printf("%d\n",ans*9);
}
return 0;
}