Labyrinth

Time limit: 1.0 second
Memory limit: 64 MB
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 output
5
.....
...##
..#..
..###
.....
198
Problem Author: Vladimir Pinaev
【分析】简单BFS,先从起点出发,遇到#边ans就++;存在不连通情况,所以要从终点再找一遍,最后减去两个角的四条边。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define inf 0x3f3f3f3f
#define mod 10000
typedef long long ll;
using namespace std;
const int N=;
const int M=;
int n,m,k,ans=,t,cnt;
int vis[N][N];
int d[][]={,,,,-,,,-};
char w[N][N];
struct man{
int x,y;
};
void bfs(int x,int y)
{
queue<man>q;
vis[x][y]=;
man s;s.x=x;s.y=y;
q.push(s);
while(!q.empty()){
man t=q.front();q.pop();
for(int i=;i<;i++){
int xx=t.x+d[i][];
int yy=t.y+d[i][];
if(xx<||xx>=n||yy<||yy>=n)ans++;
else if(w[xx][yy]=='#')ans++;
else if(!vis[xx][yy]){
man k;k.x=xx;k.y=yy;
q.push(k);
vis[xx][yy]=;
}
}
}
}
int main() {
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%s",w[i]);
}
bfs(,);
if(!vis[n-][n-])bfs(n-,n-);
printf("%d\n",(ans-)*);
return ;
}

timus 1033 Labyrinth(BFS)的更多相关文章

  1. URAL.1033 Labyrinth (DFS)

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

  2. Codeforces Round #354 (Div. 2) D. Theseus and labyrinth bfs

    D. Theseus and labyrinth 题目连接: http://www.codeforces.com/contest/676/problem/D Description Theseus h ...

  3. URAL 1033 Labyrinth

    E - Labyrinth Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submi ...

  4. Codeforces Round #516 (Div. 2)D. Labyrinth(BFS)

    题目链接:http://codeforces.com/contest/1064/problem/D 题目大意:给你一个n*m的图,图中包含两种符号,'.'表示可以行走,'*'表示障碍物不能行走,规定最 ...

  5. cf1063B Labyrinth (bfs)

    可以证明,如果我搜索的话,一个点最多只有两个最优状态:向左剩余步数最大时和向右剩余步数最大时 然后判一判,bfs就好了 dfs会T惨... #include<bits/stdc++.h> ...

  6. 1033. Labyrinth(dfs)

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

  7. codeforces 676D Theseus and labyrinth BFS搜索

    分析:一个n*m的矩阵,每个格子有12个状态,每次按一次,每个格子转90度,所以整个矩阵只有4种状态,然后爆搜就好了 #include <cstdio> #include <iost ...

  8. Codeforces Round #516 (Div. 2) (A~E)

    目录 Codeforces 1064 A.Make a triangle! B.Equations of Mathematical Magic C.Oh Those Palindromes D.Lab ...

  9. poj 1383 Labyrinth【迷宫bfs+树的直径】

    Labyrinth Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 4004   Accepted: 1504 Descrip ...

随机推荐

  1. Redhat6.x下如何进行远程安装虚拟机

    远程主机IP:192.168.122.1 远程主机名:server1.example.com 本地主机IP:192.168.122.2 本地主机名:server2.example.com 1.登录到远 ...

  2. pl/sql乱码

    环境变量增加NLS_LANG=SIMPLIFIED CHINESE_CHINA.ZHS16GBK

  3. Linux摄像头驱动学习之:(一)V4L2_框架分析

    这段时间开始搞安卓camera底层驱动了,把以前的的Linux视频驱动回顾一下,本篇主要概述一下vfl2(video for linux 2). 一. V4L2框架: video for linux ...

  4. DetectEncoding

    private Encoding DetectEncoding(ref Stream stream) { if (_pageEncoding != null) { return _pageEncodi ...

  5. Apache虚拟主机(三)

    一.启用 httpd-vhosts.conf 在httpd.conf文件中启用 在文件中搜索:Virtual hosts #Virtual hosts虚拟主机 Include conf/extra/h ...

  6. TPLink 备份文件bin文件解析

    TPLink 路由器备份文件bin文件 测试路由器 WR885,备份文件加密方式DES,密钥:478DA50BF9E3D2CF linux端: openssl enc -d -des-ecb -nop ...

  7. 2016-1-7第一个完整APP 私人通讯录的实现 6:在联系人界面增加删除联系人的功能

    一:在viewDidLoad方法中代码添加一个UIBarButtonItem,并将其的类型设置成垃圾桶,代码如下: - (void)viewDidLoad { [super viewDidLoad]; ...

  8. 无法为表空间 ***中的段创建 INITIAL 区

    这是由于表空间不足引起的. 具体错误: 解决方案:扩展表空间

  9. IOS(SystemConfiguration)框架中关于测试连接网络状态相关方法

    1. 在SystemConfiguration.famework中提供和联网相关的function, 可用来检查网络连接状态. 2. SC(SystemConfiguration)框架中关于测试连接网 ...

  10. iOS开发之拖动图片

    步骤:1.首先创建一个single view application 2.然后添加一个新的cocoa touch class的类 3.添加的类遵守<UIGestureRecognizerDele ...