【Ural 1033】 Labyrinth.(迷宫)

来源:互联网 发布:淘宝账号下线 编辑:程序博客网 时间:2024/05/29 10:46

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 containN 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.
Example
inputoutput
5........##..#....###.....

 【题解】

 就是dfs找不能通过的边,因为数据给的可能有不能从左上角到右下角的情况,所以要从两边同时搜。


【AC代码】

#include <iostream>#include <cstring>#include <string>char wall[100][100];using namespace std;int count=0;void check(int a,int b){if(wall[a][b]=='.'){if(wall[a-1][b]=='#') count++;if(wall[a+1][b]=='#') count++;if(wall[a][b-1]=='#') count++;if(wall[a][b+1]=='#') count++;wall[a][b]='$';check(a-1,b);check(a+1,b);check(a,b-1);check(a,b+1);}}//中间可能不是连接在一起的,所以需要从两个入口开始int main(){memset(wall,'#',sizeof(wall));int n;cin >> n;for(int i=1; i <= n; i++){for(int k=1; k <= n; k++)cin >> wall[i][k];}check(1,1);//从左上角的入口开始check(n,n);//右下角的入口开始cout << (count-4)*9 << endl;}



阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 逼几能塞进三根手指 吉他有几根弦 人有几根肋骨 一根玉米相当几碗米饭 小提琴有几根弦 古筝几根弦 肋骨有几根 琵琶几根弦 1克虫草有几根 虫草一天吃几根为合适 虫草泡水一次几根 女生那里能放几根手指 正常女人能放几根手指 苦丁茶一次泡几根 男生一抓头发就掉几根 17岁女孩可以放几根手指 虫草泡酒一斤酒放几根 长庚顾昀h在第几章 哈佛h6开几年会后悔吗 鱼竿几h什么意思 鱼竿28调是几h 9005灯泡是h几 川崎h2r中国有几辆 几梦hh吧 成都龙之梦大酒店是几星级 几只夫狼太倾城 一梦倾城 超神宗师 一梦几千秋 几比几的比例怎么算法 在家自制糖盐水几比几 灰兔 兔小白和兔小灰 小白兔小灰兔 小灰兔和小白兔故事 兔小白和兔小灰故事全集 兔小灰和兔小白的故事 小白兔小灰兔的故事 兔小白和兔小灰的故事 小白兔与小灰兔的故事 小白兔和小灰兔的故事 几点钟方向怎么判断 欧洲现在几点钟