UVA 784 (13.08.09)

 Maze Exploration 

A maze of rectangular rooms is represented on a twodimensional grid as illustrated in figure 1a. Each point of thegrid is represented by a character. The points of room walls aremarked by the same character which can be any printable characterdifferent than `*', `_' and space. In figure 1 this character is`X'. All the other points of the grid are markedby spaces.

               XXXXXXXXXXXXXXXXXXXXX             XXXXXXXXXXXXXXXXXXXXX
               X   X   X   X   X   X             X###X###X###X   X   X
               X           X   X   X             X###########X   X   X
               X   X   X   X   X   X             X###X###X###X   X   X
               XXXXXX XXX XXXXXXXXXX             XXXXXX#XXX#XXXXXXXXXX
               X   X   X   X   X   X             X   X###X###X###X###X
               X   X     *         X             X   X###############X
               X   X   X   X   X   X             X   X###X###X###X###X
               XXXXXXXXXXXXXXXXXXXXX             XXXXXXXXXXXXXXXXXXXXX
a) Initial maze                    b) Painted maze

Figure 1. Mazes of rectangular rooms

All rooms of the maze are equal sized with all walls 3points wide and 1 point thick as illustrated in figure 2. Inaddition, a wall is shared on its full length by the separatedrooms. The rooms can communicate through doors, which arepositioned in the middle of walls. There are no outdoor doors.

                     door
                       |
                     XX XX
                     X . X   measured from within the room
               door - ...--  walls are 3 points wide
                     X . X__
                     XXXXX  |
                       |___  walls are one point thick
Figure 2. A room with 3 doors

Your problem is to paint all rooms of a maze which can bevisited starting from a given room, called the `start room' whichis marked by a star (`*') positioned in the middle of theroom. A room can be visited from another room if there is a dooron the wall which separates the rooms. By convention, a room ispainted if its entire surface, including the doors, is markedby the character `#' as shown in figure 1b.

Input 

The program input is a text file structured as follows:

1.
The first line contains a positive integer which shows thenumber of mazes to be painted.
2.
The rest of the file contains the mazes.

The lines of the input file can be of different length. Thetext which represents a maze is terminated by a separation linefull of underscores (`_'). There are at most 30 lines andat most 80 characters in a line for each maze

The program reads the mazes from the input file, paints themand writes the painted mazes on the standard output.

Output 

The output text of a painted maze has the same format as thatwhich has been read for that maze, including the separationlines. The example below illustrates a simple input whichcontains a single maze and the corresponding output.

Sample Input 

2
XXXXXXXXX
X   X   X
X *     X
X   X   X
XXXXXXXXX
X   X
X   X
X   X
XXXXX
_____
XXXXX
X   X
X * X
X   X
XXXXX
_____

Sample Output 

XXXXXXXXX
X###X###X
X#######X
X###X###X
XXXXXXXXX
X   X
X   X
X   X
XXXXX
_____
XXXXX
X###X
X###X
X###X
XXXXX
_____

这题水爆了, 找到人所在的位置, DFS的起点, 然后定义四个方向, 直接搜~ 遇到空格字符就换成#
AC代码如下 :

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

char maze[60][160];
int r;
int dir[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};

void BFS(int x, int y) {
    for(int i = 0; i < 4; i++) {
        int tx, ty;
        tx = x + dir[i][0];
        ty = y + dir[i][1];
        if(maze[tx][ty] == ' ') {
            maze[tx][ty] = '#';
            BFS(tx, ty);
        }
    }
}

void search() {
    for(int i = 0; i < r; i++) {
        for(int j = 0; maze[i][j] != '0'; j++) {
            if(maze[i][j] == '\0')
                break;
            if(maze[i][j] == '*') {
                maze[i][j] = '#';
                BFS(i, j);
            }
        }
    }
}

void print() {
    for(int i = 0; i < r; i++)
        puts(maze[i]);
}

void init() {
    r = 0;
    memset(maze, 0 ,sizeof(maze));
    while(gets(maze[r])) {
        r++;
        if(maze[r-1][0] == '_')
            return ;
    }
}

int main() {
    int cas;
    scanf("%d", &cas);
    getchar();
    while(cas--) {
        init();
        search();
        print();
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值