POJ 2357 Labyrinth

来源:互联网 发布:淘宝热搜关键词软件 编辑:程序博客网 时间:2024/06/01 10:53


又是一道困扰了我N天的题。


Labyrinth
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 572 Accepted: 293

Description

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. 
 
Figure 1. The labyrinth from the input sample.

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 file a single integer — the exact value of the square of the wallpaper needed.

Sample Input

5........##..#....###.....

Sample Output

198


题目大意:有一个迷宫需要装修,给出迷宫的内部构造,问需要刷多少面积的墙(包括边界,单位格子长宽高均为3)


思路:首先肯定要确认哪些格子是能到的,果断选择BFS。注意到题干中就有一些墙是不可见的,所以需要先跑一遍哪里可以走到,在走到的地方搜索墙面,附代码。


#include<cstdio>#include<cstring>#include<string>#include<algorithm>#include<iostream>#include<cmath>#include<queue>using namespace std;const int MAXN=40;const int dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};int map[MAXN][MAXN];bool go[MAXN][MAXN];int n,ans;queue <int> quex,quey;void BFS(){int x,y;x=quex.front();y=quey.front();quex.pop();quey.pop();for (int i=0;i<4;i++){int nx=x+dx[i],ny=y+dy[i];if (nx<=0 || ny<=0 || nx>n || ny>n || go[nx][ny] || map[nx][ny]!=1){continue;}quex.push(nx);quey.push(ny);go[nx][ny]=true;}for (int i=0;i<4;i++){int nx=x+dx[i],ny=y+dy[i];if (map[nx][ny]==0){ans++;}}}int main(){char p;ans=0;scanf("%d",&n);getchar();memset(map,-1,sizeof(map));memset(go,false,sizeof(go));for (int i=1;i<=n;i++){for (int j=1;j<=n;j++){scanf("%c",&p);if (p=='.'){map[i][j]=1;}else{map[i][j]=0;}}getchar();}for (int i=0;i<=n;i++){map[0][i]=0;map[n+1][i]=0;map[i][0]=0;map[i][n+1]=0;}map[0][0]=-1;map[0][1]=-1;map[1][0]=-1;map[n+1][n]=-1;map[n][n+1]=-1;map[0][n+1]=-1;map[n+1][0]=-1;quex.push(1);quey.push(1);go[1][1]=true;while (!quex.empty()){BFS();}if (!go[n][n]){quex.push(n);quey.push(n);go[n][n]=true;while (!quex.empty()){BFS();}}printf("%d",ans*9);    return 0;}

很笨但是很有效的方法:预处理处迷宫四周的墙面是可见的,同时注意到[1,1]和[n,n]四周的墙面是不可见的。然后跑一遍BFS,同时还要发现迷宫的入口和出口不保证连通。。不得不贡献一次分母在此处。其实反向一遍BFS就好。


总算还了欠债。

0 0
原创粉丝点击