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

input output
5
.....
...##
..#..
..###
.....
198

dfs稍微注意一下,两个入口不一定想通,所以需要从两个入口分别进行dfs

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
char map[maxn][maxn];
bool vis[maxn][maxn];
int nxt[][]={,,,,,-,-,};
int sum;
int n;
int dfs(int x,int y){
vis[x][y]=true;
for(int i=;i<=;i++){
int xx=x+nxt[i][];
int yy=y+nxt[i][]; if(map[xx][yy]=='#'){
sum++;
// printf("-------->%d\n",sum);
// printf("---->%d %d\n",xx,yy); }
}
for(int i=;i<=;i++){
int tx=x+nxt[i][];
int ty=y+nxt[i][];
if(tx<||tx>n||ty<||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,,sizeof(map));
memset(vis,false,sizeof(vis));
getchar();
for(int i=;i<=n;i++){
scanf("%s",map[i]+);
getchar();
} for(int i=;i<=n+;i++){
map[][i]='#';
map[i][]='#';
}
for(int i=;i<=n-;i++){
map[n+][i]='#';
map[i][n+]='#';
}
map[][]='.';
map[][]='.';
map[][]='.';
map[n+][n+]='.';
map[n+][n]='.';
map[n][n+]='.'; vis[][]=true;
vis[][]=true;
vis[][]=true;
vis[n+][n+]=true;
vis[n+][n]=true;
vis[n][n+]=true;
int ans=;
sum=;
ans+=dfs(,); sum=;
if(!vis[n][n])
ans+=dfs(n,n); printf("%d\n", ans*);
}
return ;
}

URAL 1033 Labyrinth的更多相关文章

  1. URAL.1033 Labyrinth (DFS)

    URAL.1033 Labyrinth (DFS) 题意分析 WA了好几发,其实是个简单地DFS.意外发现这个俄国OJ,然后发现ACRUSH把这个OJ刷穿了. 代码总览 #include <io ...

  2. timus 1033 Labyrinth(BFS)

    Labyrinth Time limit: 1.0 secondMemory limit: 64 MB Administration of the labyrinth has decided to s ...

  3. 1033. Labyrinth(dfs)

    1033 简单dfs 有一点小小的坑 就是图可能不连通 所以要从左上和右下都搜一下 加起来 从讨论里看到的 讨论里看到一句好无奈的回复 “可不可以用中文呀...” #include <iostr ...

  4. URAL题解一

    URAL题解一 URAL 1002 题目描述:一种记住手机号的方法就是将字母与数字对应,如图.这样就可以只记住一些单词,而不用记住数字.给出一个数字串和n个单词,用最少的单词数来代替数字串,输出对应的 ...

  5. URAL 1145—— Rope in the Labyrinth——————【求树的直径】

    Rope in the Labyrinth Time Limit:500MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64 ...

  6. ural 1145. Rope in the Labyrinth

    1145. Rope in the Labyrinth Time limit: 0.5 secondMemory limit: 64 MB A labyrinth with rectangular f ...

  7. ural 1152. False Mirrors

    1152. False Mirrors Time limit: 2.0 secondMemory limit: 64 MB Background We wandered in the labyrint ...

  8. 【hihoCoder】1033: 交错和

    初探数位dp 介绍了数位类统计的基础知识.以下列出其中的基础点: 基本问题 统计在区间[l, r]中满足条件的数的个数 思路 1. [l, r] 将问题转换为 在[0, r]中满足条件的个数 - 在[ ...

  9. 2014百度之星资格赛 1004:Labyrinth(DP)

    Labyrinth Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

随机推荐

  1. 2013年省市区/县数据SQL Server(SQL语句)

    SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOSET ANSI_PADDING ONGOCREATE TABLE [dbo].[tbl_Region]( [ ...

  2. SPOJ SORTBIT Sorted bit squence (数位DP,入门)

    题意: 给出一个范围[m,n],按照二进制表示中的1的个数从小到大排序,若1的个数相同,则按照十进制大小排序.求排序后的第k个数.注意:m*n>=0. 思路: 也是看论文的.一开始也能想到是这种 ...

  3. 51nod 1431 快乐排队

    题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题  收藏  关注 有一群人在排队,如果某个人想排到前面去,可以花一元钱给直接站在他前 ...

  4. 感觉单链表是实现BCL ICollection 的最佳方式,所有操作都能以最小的时间复杂度完成

    public interface ICollection<T> : IEnumerable<T>, IEnumerable {     int Count { get; }// ...

  5. noip模拟赛#14

    #14: T1:f[x]=x-1(x&1)||x/2(x&1=0) 求[n,m]有多少个数可以通过变换得到k.(1e9). =>好像cf上看过类似的题,用二进制的方式来写.不过我 ...

  6. flask 快速入门链接

    http://docs.jinkan.org/docs/flask/quickstart.html

  7. 【转】LDA-linear discriminant analysis

    分类问题也可以用降维来理解,比如一个D维的数据点x,我们可以采用下面的映射进行线性的降维, y=θTx 在计算出y后,就可以选择一个阈值h,来进行分类.正如我们在前面的PCA模型中看到的,降维会有信息 ...

  8. pandas处理大文本数据

    当数据文件是百万级数据时,设置chunksize来分批次处理数据 案例:美国总统竞选时的数据分析 读取数据 import numpy as np import pandas as pdfrom pan ...

  9. release判断系统

    #!/bin/bash # Name: Atomic Archive configuration script # Copyright Atomicorp, 2002-2018 # License: ...

  10. VS自学日记整理

    vs渣渣自学之旅 一.vs实用插件 二.制作简历之旅 1.一堆错误示范示范 2.标签的使用 3.文件的文本的样式的保存 二.美化博客园之旅 1.第一天 学python有点多这个慢慢消化