Maze Exploration 

A maze of rectangular rooms is represented on a two dimensional grid as illustrated in figure 1a. Each point of the grid is represented by a character. The points of room walls are marked by the same character which can be any printable character different than `*', `_' and space. In figure 1 this character is `X'. All the other points of the grid are marked by 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 3 points wide and 1 point thick as illustrated in figure 2. In addition, a wall is shared on its full length by the separated rooms. The rooms can communicate through doors, which are positioned 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 be visited starting from a given room, called the `start room' which is marked by a star (`*') positioned in the middle of the room. A room can be visited from another room if there is a door on the wall which separates the rooms. By convention, a room is painted if its entire surface, including the doors, is marked by 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 the number 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. The text which represents a maze is terminated by a separation line full of underscores (`_'). There are at most 30 lines and at most 80 characters in a line for each maze

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

Output

The output text of a painted maze has the same format as that which has been read for that maze, including the separation lines. The example below illustrates a simple input which contains 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,所到达的地方用“#”覆盖即可。注意建图和数据规模。“There are at most 30 lines and at most 80 characters in a line for each maze”

代码:

 #include<stdio.h>
#include<string.h>
#include<stdbool.h>
#include<stdlib.h>
#include<ctype.h> #define rep(i,a,b) for(i=(a);i<=(b);i++)
#define red(i,a,b) for(i=(a);i>=(b);i--)
#define sqr(x) ((x)*(x))
#define clr(x,y) memset(x,y,sizeof(x))
#define LL long long const dx[]={,,,-};
const dy[]={,-,,}; int i,j,n,m,num,li,
b[]; char ch,a[][]; bool can[][]; void pre()
{
clr(can,);
clr(b,);
clr(a,'\0');
num=;li=;;
} int init()
{
int p;
while(true)
{
li++;p=;
while((ch=getchar())!='\n')
{
p++;
a[li][p]=ch;
} b[li]=p; if(a[li][]=='_') break;
} rep(i,,li)
rep(j,,b[i])
if(a[i][j]!='X') can[i][j]=; return ;
} void dfs(int xi,int yi)
{
int i,x,y; rep(i,,)
{
x=xi+dx[i];
y=yi+dy[i];
if(can[x][y])
{
a[x][y]='#';
can[x][y]=;
dfs(x,y);
}
}
} int work()
{
int i,j;
rep(i,,li)
rep(j,,b[i])
if(a[i][j]=='*')
{
a[i][j]='#';
can[i][j]=;
dfs(i,j);
} return ;
} int main()
{
scanf("%d\n",&n);
while(n--)
{
pre();
init();
work();
rep(i,,li)
{
rep(j,,b[i])
printf("%c",a[i][j]);
printf("\n");
}
}
return ;
}

[UVA] 784 - Maze Exploration的更多相关文章

  1. uva 784 Maze Exploration 染色 搜索水题 DFS

    染色问题,其实就是看看图上某一点能扩散多少. 用DFS解决,因为BFS不是很熟 =-=...以后要多练. 提交后32ms,优化了一下,在递归前进行判定,优化到22ms,不是优化的很好... 代码: # ...

  2. uva 784 Maze Exploration(简单dfs)

    这道题看上去非常麻烦,什么迷宫啊.门之类的,事实上挺简单的,就是让把与 * 连通的都置为 # 包含 * , 直接dfs就能够了,只是我wa了好多次...最后居然是多读了一个换行.忘了加getchar( ...

  3. 784 - Maze Exploration

    #include <stdio.h> #include <string.h> char maze[50][100]; void search(int i,int j) { if ...

  4. UVa784 Maze Exploration

    // 题意:输入一个迷宫,从*开始遍历,把可达点标记为字符# 注意迷宫边界不规则,要用strlen判断. #include<cstdio> #include<cstring> ...

  5. UVa 10377 - Maze Traversal

    題目:一個機器人在迷宮中行走,它的指令是方向控制(前進.左轉.右轉).給你初始位置和一些指令: 問最後停在那個位置. 分析:模擬.直接模擬就可以,注意一下細節. 假设,不能行走(邊界或者是墻壁)則停在 ...

  6. UVA 10531 Maze Statistics 迷宫统计 迷宫插头DP 四联通 概率

    题意: 有一个N*M的图,每个格子有独立概率p变成障碍物.你要从迷宫左上角走到迷宫右下角.求每个格子成为一个有解迷宫中的障碍物的概率.N <= 5,M <= 6 分析: 这真是一道好题,网 ...

  7. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

  8. Undirected Graphs

    无向图 Introduction 图是由边连接的点的集合,有着广泛的应用空间. 一些图的术语,点,边,路径,环(圈),连通分量(子图). 简单路径不重复经过点,简单环不含有重复点和边,简单图不含自环和 ...

  9. 【UVA 10307 Killing Aliens in Borg Maze】最小生成树, kruscal, bfs

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20846 POJ 3026是同样的题,但是内存要求比较严格,并是没有 ...

随机推荐

  1. cf D. Renting Bikes

    http://codeforces.com/contest/363/problem/D 先对b和p排序,用二分求出可以租的车子的最大辆数,其中用mid以后的个人钱数去租前mid的价钱的车子. #inc ...

  2. cf B. Vasya and Public Transport

    http://codeforces.com/contest/355/problem/B #include <cstdio> #include <cstring> #includ ...

  3. Codeforces 573B Bear and Blocks

    http://codeforces.com/problemset/problem/573/B  题目大意: 给出n个连续塔,每个塔有高度hi,每次取走最外层的块,问需要多少次操作能够拿光所有的块. 思 ...

  4. ActionBarActivity & FragmentActivity

    1 ActionBarActivity 是FragmentActivity的一个子类 2 ActionBarActivity 加入了对actionBar的操作, 比如getSupportActionB ...

  5. VC6.0 编译 gdlib 库

    环境  WinXP, MSVC6.0 1 从  https://bitbucket.org/libgd/gd-libgd/downloads 下载最新版本 libgd 2 可以用 nmake 编译 w ...

  6. hdu4641-K-string(后缀自动机)

    Problem Description Given a string S. K-string is the sub-string of S and it appear in the S at leas ...

  7. C++基础回顾1(数据类型, 控制语句, 数组)

    最近两天打开本科学校的C++教材,快速回顾了一下C++方面的内容.虽然书本内容比较基础,但是还是有些知识点值得自己强化记忆.分几篇文章,加上自己的理解记录如下. 先回顾面向过程的部分. C++数据类型 ...

  8. Decorator学习笔记

    初学者,自己的理解,请各位前辈不吝指正! Decorator,装饰模式,设计模式之一,谈谈我的理解,装饰这个词在我概念中就是给某个事物加上一些美丽的外表,把它变得更加完美.但是装饰是可以随时改变的,可 ...

  9. .NET 面试题(2)

    61.Application .Cookie和 Session 两种会话有什么不同? 1.Application 储存在服务端,没有时间限制,服务器关闭即销毁(前提是自己没写销毁方法) 2.Sessi ...

  10. IOS Layer的使用

    CALayer(层)是屏幕上的一个矩形区域,在每一个UIView中都包含一个根CALayer,在UIView上的所有视觉效果都是在这个Layer上进行的. CALayer外形特征主要包括: 1.层的大 ...