timus 1033 Labyrinth(BFS)

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=35;
const int M=100005;
int n,m,k,ans=0,t,cnt;
int vis[N][N];
int d[4][2]={0,1,1,0,-1,0,0,-1};
char w[N][N];
struct man{
    int x,y;
};
void bfs(int x,int y)
{
    queue<man>q;
    vis[x][y]=1;
    man s;s.x=x;s.y=y;
    q.push(s);
    while(!q.empty()){
        man t=q.front();q.pop();
        for(int i=0;i<4;i++){
            int xx=t.x+d[i][0];
            int yy=t.y+d[i][1];
           if(xx<0||xx>=n||yy<0||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]=1;
           }
        }
    }
}
int main() {
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%s",w[i]);
    }
    bfs(0,0);
    if(!vis[n-1][n-1])bfs(n-1,n-1);
    printf("%d\n",(ans-4)*9);
    return 0;
}

时间: 2024-10-13 21:59:50

timus 1033 Labyrinth(BFS)的相关文章

URAL 1033 Labyrinth

E - Labyrinth Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice URAL 1033 Description Administration of the labyrinth has decided to start a new season with new wallpapers. For this purpose they need

URAL 1033 Labyrinth(DFS)

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 m

Codeforces Round #516 (Div. 2) (A~F)

目录 A.Make a triangle! B.Equations of Mathematical Magic C.Oh Those Palindromes D.Labyrinth(BFS) E.Dwarves,Hats and Extrasensory Abilities(交互 二分) D.Labyrinth E.Dwarves,Hats and Extrasensory Abilities 比赛链接 A.Make a triangle! 不放了. B.Equations of Mathema

URAL 1145. Rope in the Labyrinth(两次BFS啊 )

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1145 1145. Rope in the Labyrinth Time limit: 0.5 second Memory limit: 64 MB A labyrinth with rectangular form and size m × n is divided into square cells with sides' length 1 by lines that are paralle

ural 1145 Rope in the Labyrinth 图中 bfs求树的直径

1145. Rope in the Labyrinth Time limit: 0.5 second Memory limit: 64 MB A labyrinth with rectangular form and size m × n is divided into square cells with sides' length 1 by lines that are parallel with the labyrinth's sides. Each cell of the grid is

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

Labyrinth Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 4004   Accepted: 1504 Description The northern part of the Pyramid contains a very large and complicated labyrinth. The labyrinth is divided into square blocks, each of them eithe

【CF676D】Theseus and labyrinth(BFS,最短路)

题意:给定一张N*M的地图,每一格都是一个房间,房间之间有门.每个房间可能有四个门,例如>代表右边只有一个门在右边即只能向右走,L代表左边没有门只能除了左其他都可以走等等.现在给出起点和终点,每次你可以把全部房间旋转90度或者移动到相邻的房间,但前提是两个房间之间都有有门,现在要你求起点出发到终点的最少时间. «+» means this block has 4 doors (one door to each neighbouring block); «-» means this block h

POJ3967Ideal Path[反向bfs 层次图]

Ideal Path Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 1754   Accepted: 240 Description New labyrinth attraction is open in New Lostland amusement park. The labyrinth consists of n rooms connected by m passages. Each passage is colo

hdu 1026 Ignatius and the Princess I (bfs打印路径)

Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has to rescue our pretty Princess. Now he gets into feng5166's castle. The castle is a large labyrinth. To make the problem simply, we assume the labyrint