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. WCF-IIS-PDA

    PDA调用WCF 一 IIS托管WCF 项目从开始是用IIS托管的WCF,但一直出错,到最后也没有搞定,希望哪位大神知道的话可以指点. 错误如下: There was no endpoint list ...

  2. linux 搭建lamp环境

    sudo apt-get install apache2 mysql-server mysql-client php5 php5-gd php5-mysql sudo chmod 777 /var/w ...

  3. 调用Android系统设置中的Intent

    开发Android软件时,常常需要打开系统设置或信息界面,来设置相关系统项或查看系统的相关信息,这时我们就可以使用以下语句来实现:(如打开“无线和网络设置”界面) Intent intent = ne ...

  4. 超实用--删除MYSQL中指定的数据的全部表

    作过的人都知道,重复测试数据库的苦恼. 用法:# Usage: ./script user password dbnane mysql.nixcraft.in ~~~~~~~~~~~~~ #!/bin ...

  5. 解决Qt中文乱码以及汉字编码的问题(UTF-8/GBK)——ubuntu环境设置默认是utf-8,文件编码可使用Encodersoft批量转换

    一.Qt环境设置 文件从window上传到Ubuntu后会显示乱码,原因是因为ubuntu环境设置默认是utf-8,Windows默认都是GBK.Windows环境下,Qt Creator,菜单-&g ...

  6. Linux企业级项目实践之网络爬虫(4)——主程序流程

    当我们设计好程序框架之后就要开始实现它了.第一步当然是要实现主程序的流程框架.之后我们逐渐填充每个流程的细节和其需要调用的模块. 主程序的流程如下: 1.  解析命令行参数,并根据参数跳转到相应的处理 ...

  7. cf437D The Child and Zoo

    D. The Child and Zoo time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  8. 【剑指offer】面试题39扩展:平衡二叉树

    题目: 输入一棵二叉树,判断该二叉树是否是平衡二叉树. 思路: 直观的思路是,判断根结点的左子树.右子树高度差是否小于1. 为避免多次访问同一结点,应该用后序遍历的方式访问. 注意:加号优先级高于条件 ...

  9. 按每k个结点反转链表

    //按每k个结点反转链表 Node* turn_k(Node* head,int n,int k) { Node* p=head; ;t<k;t++) p=p->next; //为了获取最 ...

  10. 深入了解Json转变为map的思想,附源代码2

    最近在做一个投票情况的用例,返回的结果打算放到JSON中 数据库的结果集如上图所示:optionkey代表选项,optionval代表其值 第一次做的时候考虑应该键值对应的关系,所以前台接到的json ...