【Ural 1033】 Labyrinth.(迷宫)

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.
Example
input output
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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值