ACM Jury Jeopardy

题目:

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 Format

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 Format

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

  • one line with two space-separated integers hh and ww (3 \le h, w \le 100)(3h,w100): the height and width of the maze, respectively.
  • hh lines, each with ww 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.

样例输入
3
FFRBLF
FFRFRBRFBFRBRFLF
FRLFFFLBRFFFRFFFRFRFBRFLBRFRLFLFFR
样例输出
3
4 4
####
...#
##.#
####
7 5
#####
...##
##.##
#...#
##.##
##.##
#####
7 7
#######
#...#.#
#.#...#
#.#.###
..###.#
#.....#
#######

题意:

FRBL分别代表前右后左,给出每次不同的步骤,求出一个地图,这个地图的外围除了入口处全都是墙壁‘#’,道路用‘.’表示

分析;

难点就是每次的方向都是相对于当下的,也就是说不同的点出的next数组会不一样;我令FRBL分别为1234,next={{0,1},{1,0},{0,-1},{-1,0}},发现只要是顺时针转,每转90度,next数组的第一行变成最后一行,其余三行一次向上递增,则需要每次变换next数组;

代码:

#include<stdio.h>
#include<map>
#include<string.h>
#include<math.h>
using namespace std;
#define inf 99999999;
int k;
char mapp[210][105],s[10005];
map<char,int>mm;
int main()
{
    int i,j,l,n,m,tx,ty,mayy,maxx,mixx;
    scanf("%d",&k);
    mm['F']=1;
    mm['R']=2;
    mm['B']=3;
    mm['L']=4;
    printf("%d\n",k);
    while(k--)
    {
        int next[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
        maxx=-1;
        mayy=-1;
        mixx=inf;
        for(i=0;i<210;i++)
            for(j=0;j<105;j++)
                {
                    mapp[i][j]='#';
                }
        scanf("%s",s);
        n=strlen(s);
        mapp[105][0]='.';
        tx=105;
        ty=0;
        for(i=0;i<n;i++)
        {
            tx=tx+next[mm[s[i]]-1][0];
            ty=ty+next[mm[s[i]]-1][1];
            maxx=max(maxx,tx);//记录下地图的最上方最下方以及最右方;
            mayy=max(mayy,ty);
            mixx=min(mixx,tx);
            mapp[tx][ty]='.';
            m=mm[s[i]]-1;
            for(j=0;j<m;j++)//m表示 转过几个90度;
            {
                int a,b;
                a=next[0][0];
                b=next[0][1];
                for(l=0;l<3;l++)
                {
                    next[l][0]=next[l+1][0];
                    next[l][1]=next[l+1][1];
                }
                next[3][0]=a;
                next[3][1]=b;
            }
        }
        printf("%d %d\n",maxx-mixx+3,mayy+2);
        for(i=mixx-1;i<=maxx+1;i++)
            {
                for(j=0;j<=mayy+1;j++)
                    {
                        printf("%c",mapp[i][j]);
                    }
                    printf("\n");
            }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值