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 ;
}

最新文章

  1. Objective -C学习笔记之字典
  2. ucenter 4站同步
  3. springMvc源码学习之:spirngMVC获取请求参数的方法2
  4. Working with LOBs in Oracle and PHP
  5. codechef January Challenge 2014 Sereja and Graph
  6. Django配置静态文件(CSS\js)及Django调用JS、CSS、图片等静态文件
  7. 深入js的面向对象学习篇(继承篇)——温故知新(三)
  8. C语言结构体占用空间内存大小解析
  9. 那些年搞不懂的&quot;协变&quot;和&quot;逆变&quot;
  10. ajaxFileUpload+struts2实现多文件上传
  11. java学习笔记 --- 方法
  12. UNIX环境高级编程——进程基本概述
  13. js 中 (function($){...})(jQuery) 含义
  14. luogu P2553 [AHOI2001]多项式乘法
  15. 转)nodejs后台启动方式PM2
  16. springboot之定时任务
  17. android.support不统一的问题
  18. arduino使用oled显示时间MQ_2温湿度
  19. &lt;!-- str.startsWith(&#39;胡&#39;) 检查一个 字符串中是否有某字符 返回true false --&gt;&amp; vh 属性
  20. Linux基础命令之grep

热门文章

  1. python+selenium之中类/函数/模块的简单介绍和方法调用
  2. MVC的验证码
  3. [web开发] Vue + spring boot + echart 微博爬虫展示平台
  4. 2018.4.15 Mac系统下如何使用StartUml画好需求分析的类图 (同样适用于windows)
  5. CentOS更改时区
  6. angular设置反向代理
  7. javaweb基础(3)_tomcat下部署项目
  8. C++ 学习笔记(一) cout 与printf 的不同之处
  9. 在Linux系统中重现黑客帝国经典画面
  10. CentOS7的systemctl使用