URAL 1033 Labyrinth(DFS)

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 output
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;
}
时间: 2024-10-04 21:03:28

URAL 1033 Labyrinth(DFS)的相关文章

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

URAL 1298 knight dfs搜索

1298. Knight Time limit: 2.0 second Memory limit: 64 MB Even paratroopers have vacations. The flight to Sirius in the depths of "The Admiral Brisco" Leo Hao whiled away with chessboard. No, he did not like usual chess game, and in addition, he d

Educational Codeforces Round 5 - C. The Labyrinth (dfs联通块操作)

题目链接:http://codeforces.com/contest/616/problem/C 题意就是 给你一个n行m列的图,让你求’*‘这个元素上下左右相连的连续的’.‘有多少(本身也算一个),每个’*‘的结果取模10.要是为’*‘输出结果,否则输出’.‘. 这个题目就是让你求连续的'.'联通块元素个数,求完一个联通块就把这个联通块标个记号(我设了ok[][]二维数组 表示这个位置的元素的联通块的标号,相连的则为同一个标号),之后这个联通块的每个元素的ans都为f(f为联通块元素个数),然

ural False Mirrors(dfs)

http://acm.timus.ru/problem.aspx?space=1&num=1152 有n个阳台围城一圈,每个阳台都有若干个怪兽,一次可以打三个相邻的阳台上的怪兽,它们就会全部死去,但攻击者会受到没有死去怪兽的攻击,每个怪兽的攻击是1unit,问最后攻击者受到的最小伤害. n <= 20,可以直接dfs过去. 1次WA,1次TLE. WA是没看透题意,我判断的递归终止的条件是怪兽数目小于等于3,这是不对的,就算怪兽数目小于等于3,也不一定能一次打完,因为它只能打连续的怪兽,若两

timus 1033 Labyrinth(BFS)

Labyrinth Time limit: 1.0 secondMemory 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 surface area of the walls inside the labyrinth. This job

URAL 1137Bus Routes (dfs)

Z - Bus Routes Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice URAL 1137 Description Several bus routes were in the city of Fishburg. None of the routes shared the same section of road, though commo

记忆化搜索(DFS+DP) URAL 1501 Sense of Beauty

题目传送门 1 /* 2 题意:给了两堆牌,每次从首部取出一张牌,按颜色分配到两个新堆,分配过程两新堆的总数差不大于1 3 记忆化搜索(DFS+DP):我们思考如果我们将连续的两个操作看成一个集体操作,那么这个操作必然是1红1黑 4 考虑三种情况:a[]连续两个颜色相同,输出11:b[]连续两个相同,输出22: 5 a[x] != b[y], 输出12:否则Impossible 6 详细解释:http://blog.csdn.net/jsun_moon/article/details/10254

记忆化搜索+DFS URAL 1183 Brackets Sequence

题目传送门 1 /* 2 记忆化搜索+DFS:dp[i][j] 表示第i到第j个字符,最少要加多少个括号 3 dp[x][x] = 1 一定要加一个括号:dp[x][y] = 0, x > y; 4 当s[x] 与 s[y] 匹配,则搜索 (x+1, y-1); 否则在x~y-1枚举找到相匹配的括号,更新最小值 5 */ 6 #include <cstdio> 7 #include <algorithm> 8 #include <cmath> 9 #include

ural 1145 Rope in the Labyrinth 图中 bfs求树的直径

1145. Rope in the Labyrinth Time limit: 0.5 second Memory limit: 64 MB A labyrinth with rectangular form and size m × n is divided into square cells with sides' length 1 by lines that are parallel with the labyrinth's sides. Each cell of the grid is