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. 单列表变量与字符串拆分的对照(SqlServer)

    最近遇到一个问题,在SQLServer中,需要根据用户传入的一系列ID值更新对应的记录.有两种方法,一种是将这些ID值使用逗号分隔,拼接成字符串传入,一种是以表变量的方式传入.最开始,我想当然的认为传 ...

  2. UVA 12405 Scarecrow (基础DP)

    题意: 给出一个1*N的矩阵(就是一行的格子),其中部分格子可以有草,部分无草,现在要求放置一些稻草人在某些格子上,每个稻草人可以覆盖3个连续格子,为使得有草的格子都能被覆盖,问最少放置几个稻草人. ...

  3. (五)SpringMVC之使用Kaptcha实现验证码功能

    一.什么是Kaptcha Kaptcha是Google开发的用于自动生成验证码的插件. 二.怎么导入Kaptcha ① 如果没有用Maven管理工具的话就直接导入包(可以直接下载:pau8) http ...

  4. xwork-conversion.properties 目前没有解决方案

    它没法变成.xml 这意味着项目里就只能这样

  5. N17_判断树B是不是树A的子结构

    题目描述 输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构) package new_offer; /** * 输入两棵二叉树A,B,判断B是不是A的子结构.( ...

  6. 使用EventLog组件读写事件日志

    实现效果: 知识运用: Eventlog类的SourceExists方法 //确定指定的事件源是否已在本地计算机注册 public static bool  SourceExists(string s ...

  7. vector 下标操作

    比如:vector<int> ivec(3).. 当采用下标操作ivec[10]的时候,该操作是未定义的,在自己的机器上输出的值是零.建议使用迭代器进行操作.

  8. JDBC连接数据库报错:java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. ......

    问题:Java程序使用JDBC连接MySQL数据库时,控制台报错如下: java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' ...

  9. linux 下使用 curl 访问带多参数,GET掉参数解决方案

    url 为 http://mywebsite.com/index.php?a=1&b=2&c=3 web形式下访问url地址,使用 $_GET是可以获取到所有的参数 curl  -s  ...

  10. JS数据结构与算法--双向链表

    双向链表中链接是双向的:一个链向下一个元素,另一个链向上一个元素,如下图所示: 双向链表结构代码如下: class Node { constructor(element) { this.element ...