Description

What would a programming contest be without a problem featuring an ASCII-maze? Do not despair: one of the judges has designed such a problem.

The problem is about a maze that has exactly one entrance/exit, contains no cycles and has no empty space that is completely enclosed by walls. A robot is sent in to explore the entire maze. The robot always faces the direction it travels in. At every step, the robot will try to turn right. If there is a wall there, it will attempt to go forward instead. If that is not possible, it will try to turn left. If all three directions are unavailable, it will turn back.

The challenge for the contestants is to write a program that describes the path of the robot, starting from the entrance/exit square until it finally comes back to it. The movements are described by a single letter: 'F' means forward, 'L' is left, 'R' is right and 'B' stands for backward. Each of 'L', 'R' and 'B' does not only describe the change in orientation of the robot, but also the advancement of one square in that direction. The robot’s initial direction is East. In addition, the path of the robot always ends at the entrance/exit square.

The judge responsible for the problem had completed all the samples and testdata, when disaster struck: the input file got deleted and there is no way to recover it! Fortunately the output and the samples are still there. Can you reconstruct the input from the output? For your convenience, he has manually added the number of test cases to both the sample output and the testdata output.

Input

On the first line one positive number: the number of test cases. After that per test case:

one line with a single string: the movements of the robot through the maze.

Output

On the first line one positive number: the number of test cases, at most 100. After that per test case:

  • one line with two space-separated integers h and w (3 ≤ hw ≤ 100): the height and width of the maze, respectively.

  • h lines, each with w characters, describing the maze: a '#' indicates a wall and a '.' represents an empty square.

The entire contour of the maze consists of walls, with the exception of one square on the left: this is the entrance. The maze contains no cycles (i.e. paths that would lead the robot back to a square it had left in another direction) and no empty squares that cannot be reached from the entrance. Every row or column --- with the exception of the top row, bottom row and right column --- contains at least one empty square.

Sample Input

3
FFRBLF
FFRFRBRFBFRBRFLF
FRLFFFLBRFFFRFFFRFRFBRFLBRFRLFLFFR

Sample Output

3
4 4
####
...#
##.#
####
7 5
#####
...##
##.##
#...#
##.##
##.##
#####
7 7
#######
#...#.#
#.#...#
#.#.###
..###.#
#.....#
#######

 
这道题就很妙,给出操作复原图,不过有几个注意的点,我们很容易可以想到偏移坐标,来求解,但千万得注意,一开始就要标记起点,为什么?这是入口啊兄弟,,,还要要输出测试数量,哎,没有看清题意,凉凉。
这是我的代码:
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
#define PY 100
#define INF 0x3f3f3f3f
bool book[][];
char ch[];
int main()
{
int n; scanf("%d",&n); printf("%d\n",n);
while(n--)
{
memset(book,,sizeof(book));
getchar();
scanf("%s",ch);
int i=;
int nowx=+PY;
int nowy=+PY;
int miny=PY,maxy=PY,maxx=PY,minx=PY;
int t=;//东
book[nowx][nowy]=; while(ch[i]!='\0')
{
if(ch[i]=='F')
{
if(t==)
{
nowy++; book[nowx][nowy]=;
//printf("%d %d\n",nowx,nowy);
}
if(t==)
{
nowx++; book[nowx][nowy]=;
//printf("%d %d\n",nowx,nowy);
}
if(t==)
{
nowy--; book[nowx][nowy]=;
//printf("%d %d\n",nowx,nowy);
}
if(t==)
{
nowx--; book[nowx][nowy]=;
// printf("%d %d\n",nowx,nowy);
} }
if(ch[i]=='B')
{
if(t==)
{
t=;
nowy--; book[nowx][nowy]=;
//printf("%d %d\n",nowx,nowy);
}
else if(t==)
{
t=;
nowx--; book[nowx][nowy]=;
//printf("%d %d\n",nowx,nowy); }
else if(t==)
{
t=;
nowy++; book[nowx][nowy]=;
//printf("%d %d\n",nowx,nowy);
}
else if(t==)
{
t=;
nowx++; book[nowx][nowy]=;
//printf("%d %d\n",nowx,nowy);
}
}
if(ch[i]=='R')
{
if(t==)
{
t=;//南
nowx++; book[nowx][nowy]=;
//printf("%d %d\n",nowx,nowy);
}
else if(t==)
{
t=;//西
nowy--; book[nowx][nowy]=;
//printf("%d %d\n",nowx,nowy);
}
else if(t==)
{
t=;//北
nowx--; book[nowx][nowy]=;
//printf("%d %d\n",nowx,nowy);
}
else if(t==)
{
t=;
nowy++; book[nowx][nowy]=;
//printf("%d %d\n",nowx,nowy);
}
}
if(ch[i]=='L')
{
if(t==)
{
t=;
nowx--; book[nowx][nowy]=;
//printf("%d %d\n",nowx,nowy);
}
else if(t==)
{
t=;
nowy++; book[nowx][nowy]=;
// printf("%d %d\n",nowx,nowy);
}
else if(t==)
{
t=;
nowx++; book[nowx][nowy]=;
// printf("%d %d\n",nowx,nowy);
}
else if(t==)
{
t=;
nowy--; book[nowx][nowy]=;
//printf("%d %d\n",nowx,nowy);
}
}
i++;
minx=min(minx,nowx);
maxx=max(maxx,nowx);
miny=min(miny,nowy);
maxy=max(maxy,nowy);
}
int m=maxy-miny++;int n=maxx-minx++; printf("%d %d\n",n,m);
for(int i=minx- ; i<=maxx+ ;i++)
{
for(int j=miny ;j<=maxy+ ; j++)
if(book[i][j]==)
putchar('.');
else
putchar('#');
puts("");
} }
return ;
}

这是大佬的代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int net[][]={{,},{,},{,-},{-,}};
bool book[][];
char ch[];
int main()
{
int t;
scanf("%d",&t);
printf("%d\n",t);
while(t--)
{
memset(book,,sizeof(book));
scanf("%s",ch);
int n=strlen(ch);
int r=;
int nowx,nowy,maxy,miny,maxx,minx;
nowx=nowy=maxy=miny=maxx=minx=;
book[nowx][nowy]=;
for(int i= ; i<n ; i++)
{
if(ch[i]=='F')
{
nowx+=net[r][];
nowy+=net[r][];
book[nowx][nowy]=; }
else if(ch[i]=='R')
{
r=(r+)%;
nowx+=net[r][];
nowy+=net[r][];
book[nowx][nowy]=;
}
else if(ch[i]=='B')
{
r=(r+)%;
nowx+=net[r][];
nowy+=net[r][];
book[nowx][nowy]=;
}
else if(ch[i]=='L')
{
r=(r+)%;
nowx+=net[r][];
nowy+=net[r][];
book[nowx][nowy]=;
}
maxx=max(nowx,maxx);
minx=min(nowx,minx);
maxy=max(nowy,maxy);
miny=min(nowy,miny);
}
printf("%d %d\n",maxx-minx+,maxy-miny+);
for(int i=minx- ; i<=maxx+ ; i++)
{
for(int j=miny ; j<=maxy+ ; j++)
if(book[i][j]==)
printf(".");
else
printf("#");
printf("\n");
} } return ;
}

看到了吗,同样的一道问题的区别,虽然我的代码快一点点,然并软。

来简单分析下大神与我的区别1:我是根据方向来确定下一点。大神是根据点来确定方向,这就是个本质的区别,还是要多学习人家好的代码

最新文章

  1. Servlet监听器笔记总结
  2. quartz CronExpression表达式
  3. 19.Java 注解
  4. 12款非常精致的免费 HTML5 &amp; CSS3 网站模板
  5. 搜索引擎LuceneNet
  6. safari浏览器添加书签的方法
  7. [安卓] 9、线程、VIEW、消息实现从TCP服务器获取数据动态加载显示
  8. Buffer cache 的调整与优化
  9. Fedora16 安装相关
  10. 【Android平台安全方案】の #00-请不要在外部存储(SD卡)加密存储的敏感信息
  11. Oracle数据库部分迁至闪存存储方案
  12. ORACLE窗口函数
  13. Spring源码追踪1——doGetBean(为什么org.springframework.data.redis.core.RedisTemplate的实例可以注入为ListOperations)
  14. Python中For循环
  15. python 爆破
  16. SNF开发平台WinForm-Grid表格控件大全
  17. 强大的JQuery表单验证插件 FormValidator使用介绍
  18. es分布式文档系统_bulk api的奇特json格式与底层性能优化关系
  19. Java基础 【Arrays 类的使用】
  20. Gravitational Teleport docker-compose简单运行

热门文章

  1. 获取字符串长度函数length()和hengthb()
  2. ORACLE体系结构一 (物理结构)- 数据文件、日志文件、控制文件和参数文件
  3. java多线程环境单例模式实现详解
  4. Windows版本Apache+php的Xhprof应用__[2]
  5. 关于jdk1.5之后的自定拆装箱
  6. UIScrollView现实循环滚动
  7. php的变量引用详解
  8. string为什么是final?源码分析
  9. 深、浅copy
  10. oracle connect by 递归,反递归,自动补全查询实现