URAL 1033 Labyrinth(DFS)

来源:互联网 发布:iphone7无法删除软件 编辑:程序博客网 时间:2024/06/05 19:36
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.
Problem illustration
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.

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 terminated 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 area of the wallpaper needed.

Sample

inputoutput

5........##..#....###.....
198
卧槽,就是在墙上贴壁纸来着,mp从1~n存图,其余的赋为#,再DFS。。得到的sum得减4,出口和入口吗。。

还有可能搜不到出口,所以要检验=-=不行再从出口搜一次=-=

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int dr[4][2]={{0,1},{0,-1},{-1,0},{1,0}};char mp[35][35];int visit[35][35];int n,sum;void dfs(int x,int y){   for(int i=0;i<4;i++)   {       int xx=x+dr[i][0];       int yy=y+dr[i][1];       if(!visit[xx][yy]&&mp[xx][yy]=='.')       {          visit[xx][yy]=1;          dfs(xx,yy);       }       else if(mp[xx][yy]=='#')          sum++;   }}int main(){    char s[35];    while(~scanf("%d",&n))    {        sum=0;        memset(mp,'#',sizeof(mp));//还可以这样?原谅我只会赋0和-1=-=        for(int i=1;i<=n;i++)        {            scanf("%s",s);            for(int j=1;j<=n;j++)                mp[i][j]=s[j-1];        }        visit[1][1]=1;        dfs(1,1);        if(!visit[n][n])        {            visit[n][n]=1;            dfs(n,n);        }        printf("%d\n",(sum-4)*9);    }    return 0;}


1 0
原创粉丝点击