URAL 1033 Labyrinth


E - Labyrinth Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit Status Practice URAL 1033

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 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.

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 Input

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

dfs稍微注意一下,两个入口不一定想通,所以需要从两个入口分别进行dfs

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=100;
char  map[maxn][maxn];
bool vis[maxn][maxn];
int nxt[4][2]={1,0,0,1,0,-1,-1,0};
int sum;
     int n;
int dfs(int x,int y){
    vis[x][y]=true;
    for(int i=0;i<=3;i++){
        int xx=x+nxt[i][0];
        int yy=y+nxt[i][1];

        if(map[xx][yy]=='#'){
              sum++;
             // printf("-------->%d\n",sum);
            //  printf("---->%d %d\n",xx,yy);

        }
    }
    for(int i=0;i<=3;i++){
        int tx=x+nxt[i][0];
        int ty=y+nxt[i][1];
        if(tx<1||tx>n||ty<1||ty>n||vis[tx][ty])
            continue;
        if(map[tx][ty]=='.'){
            vis[tx][ty]=true;
            dfs(tx,ty);
        }
    }
    return sum;
}

int main(){

     while(scanf("%d",&n)!=EOF){
         memset(map,0,sizeof(map));
         memset(vis,false,sizeof(vis));
         getchar();
         for(int i=1;i<=n;i++){
            scanf("%s",map[i]+1);
            getchar();
         }



          for(int i=2;i<=n+1;i++){
              map[0][i]='#';
              map[i][0]='#';
          }
         for(int i=0;i<=n-1;i++){
              map[n+1][i]='#';
              map[i][n+1]='#';
         }
           map[0][0]='.';
            map[0][1]='.';
             map[1][0]='.';
              map[n+1][n+1]='.';
               map[n+1][n]='.';
                map[n][n+1]='.';

         

         vis[0][0]=true;
        vis[0][1]=true;
       vis[1][0]=true;
           vis[n+1][n+1]=true;
           vis[n+1][n]=true;
           vis[n][n+1]=true;
        int ans=0;
         sum=0;
        ans+=dfs(1,1);
     
     
        sum=0;
        if(!vis[n][n])
        ans+=dfs(n,n);
        




        printf("%d\n", ans*9);
     }
     return 0;
}

优质内容筛选与推荐>>
1、IOS和Andriod手机浏览器内核
2、RTAX
3、numpy学习总结
4、在Sublime Text 3中配置编译和运行Java程序
5、window启动目录


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn