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. python中的嵌套类(内部类调用外部类中的方法函数)

    在为书中版本是3.X的,但2.X不太支持直接调用. 所以,在PYTHON2.X中,要在内部类中调用外部类的方法,就必须得实例化外部类,然后,传入实例进行调用. 花了我两个小时啊,资料没找到,自己一个一 ...

  2. UltraEdit-32 恢复到初始默认配置

    不小心把UltraEdit-32搞乱了,字体不管怎么设置,都回不去了..然后那些菜单也被弄得乱七糟八的....于是就想把这些设置都搞成默认的.重装UltraEdit-32.清理注册表.结果发现都没有用 ...

  3. Powershell 设置数值格式 1

    设置数值格式 1 6 6月, 2013  在 Powershell tagged 字符串 / 数字 / 文本 / 日期 / 格式化 by Mooser Lee 格式化操作符 -f 可以将数值插入到字符 ...

  4. 【转】 ubuntu12.04更新源

    原文网址:http://blog.chinaunix.net/uid-26404477-id-3382633.html 摘 要:本文列出ubuntu 12.04 LTS更新源列表,内容为网友整理,此处 ...

  5. 【开源】Hawk-数据抓取工具:简明教程

    1.软件介绍 HAWK是一种数据采集和清洗工具,依据GPL协议开源,能够灵活,有效地采集来自网页,数据库,文件, 并通过可视化地拖拽, 快速地进行生成,过滤,转换等操作.其功能最适合的领域,是爬虫和数 ...

  6. POJ2367 Genealogical tree (拓扑排序)

    裸拓扑排序. 拓扑排序 用一个队列实现,先把入度为0的点放入队列.然后考虑不断在图中删除队列中的点,每次删除一个点会产生一些新的入度为0的点.把这些点插入队列. 注意:有向无环图 g[] : g[i] ...

  7. java基础 二分查找算法

    /*   * 折半查找法:   * 思路:   * 定义三个变量记录查找范围中最大.最小和中间的索引值,每次都是使用中间索引值与要查找的目标进行对比,如果不符合,那么就不停缩小查找范围   * */  ...

  8. Android静态变量使用陷阱

    静态变量大家再熟悉不过了,本来没什么好重复的.事情起因是这样的,最近测试那边反应正在做的一个产品总是莫名其妙的显示不出某些数据,甚至闪退崩溃,仔细查了几遍发现没什么问题,最后百般周折发现在那部测试机上 ...

  9. Android设置定时执行执行一次任务

    private Handler handler = new Handler(){ public void handleMessage(Message msg) { super.handleMessag ...

  10. DB2数据库全系列版本安装介质下载地址

    网盘:http://pan.baidu.com/s/1qWE4D7A? ... qq-pf-to=pcqq.group官方:http://www-01.ibm.com/support/docview. ...