Jury Jeopardy 图的四方向的变换

Jury Jeopardy

  • Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others)
  • Total Submission(s): 52     Accepted Submission(s): 16
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
#######
#...#.#
#.#...#
#.#.###
..###.#
#.....#
#######


规定东,南,西,北分别为0,1,2,3F, R, L, B0,1,2,3

例如:此时面对的方向是向东的,我们要右转,右转是1,那么坐标变化就是此时的

x+=inext[0][1](inext[now][R]) 行坐标0表示方向东,列坐标1表示右转,而

y也是一样,y+=jnext[0][1](jnext[now][R]);那么紧接着方向变为now=dic[0][1]([now][R])。


#include<cstdio>///规定东,南,西,北分别为0,1,2,3,F,R,L,B为0,1,2,3
#include<algorithm>
#include<cstring>
using namespace std;
int main()
{
    int maps[310][310],inext[4][4]={0,1,-1,0,1,0,0,-1,-1,0,0,1,0,-1,1,0};///坐标x的变换
    int jnext[4][4]={1,0,0,-1,0,-1,1,0,0,1,-1,0,-1,0,0,1};///坐标y的变化
    int dic[4][4]={0,1,2,3,1,3,0,2,2,0,3,1,3,2,1,0};///紧接着下次的方向
    int now,ncase,x,y,shu;
    char op;
    scanf("%d ",&ncase);
    shu=ncase;
    for(int t=1;t<=ncase;t++)
    {
        memset(maps,0,sizeof(maps));///以0代表#
        x=150,y=150;///为了避免越界,在此处开始
        now=0;///刚开始是向东的。所以此时的方向为0
        maps[x][y]=1;
        int xmin=150,ymin=150,xmax=150,ymax=150;///初始化xmin表示此时左上角行坐标最小的坐标
                       ///ymin表示左上角纵坐标,xmax表示此时右下角行坐标最大的坐标,ymax表示右下角最大的纵坐标
        while((op=getchar())&&op!='\n')
        {
            shu++;
            if(op=='F'){
                x+=inext[now][0]; ///更新x坐标
                y+=jnext[now][0];///更新y坐标
                maps[x][y]=1;
                now=dic[now][0];/// 更新方向
            }
           else if(op=='R'){
                x+=inext[now][1];
                y+=jnext[now][1];
                maps[x][y]=1;
                now=dic[now][1];
            }
           else if(op=='L'){
                x+=inext[now][2];
                y+=jnext[now][2];
                maps[x][y]=1;
                now=dic[now][2];
            }
           else if(op=='B'){
                x+=inext[now][3];
                y+=jnext[now][3];
                maps[x][y]=1;
                now=dic[now][3];
            }
           if(x<xmin) ///有比已知左上角行坐标更小的,则更新
           xmin=x;
           else if(y<ymin)///有比已知左上角纵坐标更小的,则更新
            ymin=y;
           else if(x>xmax)///有比已知右下角行坐标更小的,则更新
            xmax=x;
           else if(y>ymax)///有比已知右下角纵坐标更小的,则更新
            ymax=y;
        }
//        printf("shu=%d\n",shu);
       if(t==1) printf("%d\n",ncase);
        printf("%d %d\n",xmax-xmin+1+2,ymax-ymin+1+1);///这里按题目围住输出
        for(int i=xmin-1;i<=xmax+1;i++)
        {
            for(int j=ymin;j<=ymax+1;j++){
                if(maps[i][j]==0) printf("#");
                else printf(".");
            }
            putchar('\n');
        }
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值