zoukankan      html  css  js  c++  java
  • 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
    ",sum);
                //  printf("---->%d %d
    ",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
    ", ans*9);
         }
         return 0;
    }
  • 相关阅读:
    for xml path(''),root('')
    [小明带你玩儿Photon]4.一起来看日志
    [小明带你玩儿Photon]3.Hello World i
    [小明带你玩儿Photon]2.从零开始一个程序
    [小明带你玩儿Photon]1.Photon介绍
    杂记
    FluentNHibernate初探[1]
    [Navigation]Navigation初探[2]
    [Navigation]Navigation初探[1]
    动画系统的animator
  • 原文地址:https://www.cnblogs.com/13224ACMer/p/4836378.html
Copyright © 2011-2022 走看看