Timus 1033 Labyrinth (BFS)

Source: Internet
Author: User

Labyrinthtime limit:1.0 Second
Memory limit:64 Mbadministration of the Labyrinth have 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. This job was just for you! The labyrinth is represented by a matrix NX N(3≤ N ≤33, you see, ' 3 ' is a magic digit!). Some matrix cells contain a dot character ('. ') that denotes a empty square. Other cells contain a diesis character (' # '), denotes a square filled by monolith block of stone wall. All squares is of the same size 3x3 meters. The walls is constructed around the labyrinth (except for the upper left and lower right corners, which is used as Entra NCES) and on the cells with a diesis character. No other walls is constructed. There always'll is a dot character at the upper left and lower right corner cells of the input matrix. Your task is to calculate the area of the visible part of the walls inside the labyrinth. In and words, the area of the walls ' surface is visible to a visitor of the labyrinth. Note that there's no holes to look or to move through between any and adjacent blocks of the wall. The blocks is considered to being adjacent if they touch each and any corner. See Example:visible walls inside the labyrinth is drawnwith bold lines. The height of the walls is 3 meters. Inputthe first line of the input contains the single number N. The next NLines contain NCharacters each. Each line describes one row of the Labyrinth matrix. All line only dot and diesis characters would be used and each line would be terminated with a new line character. There'll be is no spaces in the input. Outputyour 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 "Analysis" simple BFS, starting from the beginning, encountered the # side ans on + +; There is no connectivity, so you need to find it again from the end point, and then subtract two corners of four edges.
#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>#defineINF 0x3f3f3f3f#defineMoD 10000typedefLong Longll;using namespacestd;Const intn= *;Const intm=100005;intn,m,k,ans=0, t,cnt;intVis[n][n];intd[4][2]={0,1,1,0,-1,0,0,-1};CharW[n][n];structman{intx, y;};voidBFsintXinty) {Queue<man>Q; Vis[x][y]=1; Mans S;s.x=x;s.y=y;    Q.push (s);  while(!Q.empty ()) {Man T=Q.front (); Q.pop ();  for(intI=0;i<4; i++){            intxx=t.x+d[i][0]; intyy=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; }        }    }}intMain () {scanf ("%d",&N);  for(intI=0; i<n;i++) {scanf ("%s", W[i]); } BFS (0,0); if(!vis[n-1][n-1]) BFS (n1, N-1); printf ("%d\n", (ans-4)*9); return 0;}
View Code

Timus 1033 Labyrinth (BFS)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.