Ural 1033 Labyrinth (DFS)

Source: Internet
Author: User
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 wallinside 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 wallare constructed around the Labyrinth (vertex t for the upper left and lower right corners, which are used as entrances) and on the cells with a DIESIS character. no other Wils 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 wallinside the labyrinth. in other words, the area of the wall' 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 wallinside the labyrinth are drawn with bold lines. the height of all the Wils 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. 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. outputyour program shocould print to the output a single integer-the exact value of the area of the wallpaper needed. sample

Input Output
5........##..#....###.....
198
In the slot, wallpaper is pasted on the wall. MP ranges from 1 ~ N Save the graph, and the rest are assigned as # And then DFS .. The sum is reduced by 4, the exit and the entrance ..

You may not be able to find the exit, so you need to test =-= no more than once =-=

# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> using namespace STD; int Dr [4] [2] = {0, 1 }, {0,-1}, {-}, {}; char MP [35] [35]; int visit [35] [35]; int N, sum; void DFS (int x, int y) {for (INT I = 0; I <4; I ++) {int xx = x + Dr [I] [0]; int YY = Y + Dr [I] [1]; If (! Visit [XX] [YY] & amp; MP [XX] [YY] = '. ') {visit [XX] [YY] = 1; DFS (XX, YY);} else if (MP [XX] [YY] = '#') sum ++ ;}} int main () {char s [35]; while (~ Scanf ("% d", & N) {sum = 0; memset (MP, '#', sizeof (MP); // can this be done? Forgive me for assigning only 0 and-1 =-= for (INT I = 1; I <= N; I ++) {scanf ("% s", S ); for (Int J = 1; j <= N; j ++) MP [I] [J] = s [J-1];} visit [1] [1] = 1; DFS (1, 1); If (! Visit [N] [N]) {visit [N] [N] = 1; DFS (n, n);} printf ("% d \ n ", (Sum-4) * 9);} return 0 ;}


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.