URAL 1033 Labyrinth

E - Labyrinth
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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

 

转载于:https://www.cnblogs.com/13224ACMer/p/4836378.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值