1033. Labyrinth

1033. Labyrinth

Time limit: 1.0 second Memory limit: 64 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.
Problem illustration
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

inputoutput
5
.....
...##
..#..
..###
.....
198

Problem Author: Vladimir Pinaev  Problem Source: Ural Collegiate Programming Contest '99 
*********************************************************************
广搜
**********************************************************************
 1 #include<iostream>
 2 #include<string>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<cctype>
 6 #include<cstdio>
 7 #include<stack>
 8 #include<queue>
 9 #include<vector>
10 using namespace std;
11 char s[100][100];
12 int a[100][100];
13 int i,j,n;
14 void find(int x,int y)//广搜
15  {
16      if(x==0||y==0||x>n||y>n)
17       return;
18      if(a[x][y]==0)return;
19      if(s[x][y]=='#')return;
20      a[x][y]=0;
21      find(x+1,y);
22      find(x-1,y);
23      find(x,y-1);
24      find(x,y+1);
25  }
26  int main()
27  {
28      cin>>n;
29      getchar();
30      for(i=1;i<=n;i++)
31       {
32           for(j=1;j<=n;j++)
33            cin>>s[i][j];
34           getchar();
35       }
36       memset(a,255,sizeof(a));
37       find(1,1);
38       find(n,n);
39       int ans=0;
40       for(i=1;i<=n;i++)
41        for(j=1;j<=n;j++)
42         {
43             if(a[i][j]==0)//看四周
44              {
45                  if(a[i-1][j]==-1)ans++;
46                  if(a[i][j-1]==-1)ans++;
47                  if(a[i+1][j]==-1)ans++;
48                  if(a[i][j+1]==-1)ans++;
49              }
50 
51         }
52         ans-=4;
53         ans*=9;
54         cout<<ans<<endl;
55         return 0;
56  }
View Code
原文地址:https://www.cnblogs.com/sdau--codeants/p/3242925.html