URAL 1033. Labyrinth

1. 题目

http://acm.timus.ru/problem.aspx?space=1&num=1033

1033. 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.
Problem illustration
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
Problem Source: Ural Collegiate Programming Contest ’99

2. 思路

求迷宫中墙的面积,BFS,撞墙就把计数累加1,得到墙的数量,求得面积。

3.代码

#include <stdio.h>
#include <stdlib.h>

#define MAX_WIDTH 33
#define STAT_BLOCKED -1
#define STAT_VISITED 0
#define STAT_AVAILABLE 1


typedef char Labyrinth[MAX_WIDTH][MAX_WIDTH];

void solveE1c();
int inputLabyrinth(Labyrinth laby);
int getWalls(int startRow, int startCol, Labyrinth laby, int width);
int canGoNorth(int row, int col, Labyrinth laby, int width);
int canGoSouth(int row, int col, Labyrinth laby, int width);
int canGoWest(int row, int col, Labyrinth laby, int width);
int canGoEast(int row, int col, Labyrinth laby, int width);
void printLaby(Labyrinth laby, int width);

Labyrinth laby;

int main() {
    //freopen("test.txt", "r", stdin);
    solveE1c();
    system("pause");
    return 0;
}

void solveE1c() {
    int width = 0, walls = 0, wallpaperSize = 0;

    width = inputLabyrinth(laby);
    // printLaby(laby, width);
    walls = getWalls(0, 0, laby, width);
    // printLaby(laby, width);
    if (laby[width - 1][width - 1] != 'v') {
        walls += getWalls(width - 1, width - 1, laby, width);
        // printLaby(laby, width);
    }

    wallpaperSize = (walls - 4) * 9;
    printf("%d\n", wallpaperSize);
}

int inputLabyrinth(Labyrinth laby) {
    int width = 0, i = 0, j = 0;
    char c;

    scanf("%d\n", &width);
    for(i = 0; i < width; ++i) {
        for(j = 0; j < width;) {
            scanf("%c", &c);
            if (c == '\n')
                continue;
            else
                laby[i][j++] = c;
        }
    }

    return width;
}

char qRow[MAX_WIDTH * MAX_WIDTH];
char qCol[MAX_WIDTH * MAX_WIDTH];
int getWalls(int startRow, int startCol, Labyrinth laby, int width) {
    int front = 0, rear = 0, currRow = 0, currCol = 0, walls = 0, nextPosStatus = 0;

    qRow[rear] = startRow;
    qCol[rear] = startCol;
    ++rear;
    laby[startRow][startCol] = 'v';

    while (front < rear) {
        currRow = qRow[front];
        currCol = qCol[front];
        ++front;

        nextPosStatus = canGoNorth(currRow, currCol, laby, width);
        if (nextPosStatus == STAT_AVAILABLE) {
            qRow[rear] = currRow - 1;
            qCol[rear] = currCol;
            ++rear;
            laby[currRow - 1][currCol] = 'v';
        } else if (nextPosStatus == STAT_BLOCKED) {
            ++walls;
        }

        nextPosStatus = canGoSouth(currRow, currCol, laby, width);
        if (nextPosStatus == STAT_AVAILABLE) {
            qRow[rear] = currRow + 1;
            qCol[rear] = currCol;
            ++rear;
            laby[currRow + 1][currCol] = 'v';
        } else if (nextPosStatus == STAT_BLOCKED) {
            ++walls;
        }

        nextPosStatus = canGoWest(currRow, currCol, laby, width);
        if (nextPosStatus == STAT_AVAILABLE) {
            qRow[rear] = currRow;
            qCol[rear] = currCol - 1;
            ++rear;
            laby[currRow][currCol - 1] = 'v';
        } else if (nextPosStatus == STAT_BLOCKED) {
            ++walls;
        }

        nextPosStatus = canGoEast(currRow, currCol, laby, width);
        if (nextPosStatus == STAT_AVAILABLE) {
            qRow[rear] = currRow;
            qCol[rear] = currCol + 1;
            ++rear;
            laby[currRow][currCol + 1] = 'v';
        } else if (nextPosStatus == STAT_BLOCKED) {
            ++walls;
        }
    }

    return walls;
}

int canGo(int row, int col, Labyrinth laby, int width) {
    if (row < 0 || row >= width || col < 0 || col >= width) {
        return STAT_BLOCKED;
    } else if (laby[row][col] == '#') {
        return STAT_BLOCKED;
    } else if (laby[row][col] == 'v') {
        return STAT_VISITED;
    } else {
        return STAT_AVAILABLE;
    }
}

int canGoNorth(int row, int col, Labyrinth laby, int width) {
    int nextRow = row - 1, nextCol = col;
    return canGo(nextRow, nextCol, laby, width);
}

int canGoSouth(int row, int col, Labyrinth laby, int width) {
    int nextRow = row + 1, nextCol = col;
    return canGo(nextRow, nextCol, laby, width);
}

int canGoWest(int row, int col, Labyrinth laby, int width) {
    int nextRow = row, nextCol = col - 1;
    return canGo(nextRow, nextCol, laby, width);
}

int canGoEast(int row, int col, Labyrinth laby, int width) {
    int nextRow = row, nextCol = col + 1;
    return canGo(nextRow, nextCol, laby, width);
}

void printLaby(Labyrinth laby, int width) {
    int i, j;
    for (i = 0; i < width; ++i) {
        for (j = 0; j < width; ++j) {
            printf("%c ", laby[i][j]);
        }
        printf("\n");
    }
}