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. RFID电子标签加工的倒装工艺

    倒装对于半导体封装领域的人员而言,是再熟悉不过的了.一般我们看到的集成电路多数以塑封为主,半导体芯片和外界进行信息沟通的通道,靠的就是集成电路的管脚.如果把集成电路外面的封装去掉,会发现每个集成电路内 ...

  2. Window Ghosting(仍可拖动失去响应的窗口,因为我们真正的窗口已经让系统用Ghosting窗口替代了。使用IsHungAppWindow 探测)

    最近工作中遇到Window Ghosting这个问题, 感觉挺有意思,这里简单记录下.     在XP时代我们的程序没有响应后只能通过任务管理器强制杀掉,但是Vista之后情况变了, 我们仍然可以拖动 ...

  3. Python 坑爹之 代码缩进

    建议:统一使用空格!!!!!!!!!不要Tab Python代码缩进   这两天python-cn邮件列表有一条thread发展的特别长,题目是<python的代码缩进真是坑爹>(地址), ...

  4. smarty 内置函数if 等判断

    {if},{elseif},{else} Smarty的{if}条件判断和PHP的if 非常相似,只是增加了一些特性. 每个{if}必须有一个配对的{/if}. 也可以使用{else} 和 {else ...

  5. Linux企业级项目实践之网络爬虫(14)——使用正则表达式抽取HTML正文和URL

    正则表达式,又称正规表示法.常规表示法(英语:Regular Expression,在代码中常简写为regex.regexp或RE),计算机科学的一个概念.正则表达式使用单个字符串来描述.匹配一系列符 ...

  6. NOI2012 魔幻棋盘

    http://www.lydsy.com/JudgeOnline/problem.php?id=2877 二维线段树. 好恶...... B类数据: 棋盘是一维的. 我们有一个结论: $gcd(a_{ ...

  7. java实现矩阵连乘的动态规划

    package com.cjs.algorithm; public class DynamicPlan { /** * 此方法用来求解矩阵连乘的最小数乘次数 * * @param p * 传入的要连乘 ...

  8. pyqt时间

    # -*- coding: utf-8 -*-__author__ = 'Administrator'from PyQt4 import QtCore, QtGui class Help(QtGui. ...

  9. Netty实例-简单的服务端-client实现,凝视具体

           书籍推荐:                                       实例代码 :http://download.csdn.net/detail/jiangtao_st ...

  10. Makefile学习(三)执行make

    9 执行make 一般方法:make. 某些情况:1.可能需要使用make更新一部分过时文件而不是全部 2.需要使用另外的编译器或者重新定义编译选项 3.只需要查看哪些文件被修改,不需要重新编译 所以 ...