zoukankan      html  css  js  c++  java
  • Jury Jeopardy (这是一道单纯的模拟题)

    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[300][300];
    char ch[999999];
    int main()
    {
        int n;
    
        scanf("%d",&n);
        
        printf("%d
    ",n);
        while(n--)
        {
            memset(book,0,sizeof(book));
            getchar();
            scanf("%s",ch);
            int i=0;
            int nowx=0+PY;
            int nowy=0+PY;
            int miny=PY,maxy=PY,maxx=PY,minx=PY;
            int t=1;//
            book[nowx][nowy]=1;
            
            while(ch[i]!='')
            {
                if(ch[i]=='F')
                {
                    if(t==1)
                    {
                        nowy++;
    
                        book[nowx][nowy]=1;
                        //printf("%d %d
    ",nowx,nowy);
                    }
                    if(t==2)
                    {
                        nowx++;
    
                        book[nowx][nowy]=1;
                        //printf("%d %d
    ",nowx,nowy);
                    }
                    if(t==3)
                    {
                        nowy--;
    
                        book[nowx][nowy]=1;
                        //printf("%d %d
    ",nowx,nowy);
                    }
                    if(t==4)
                    {
                        nowx--;
    
                        book[nowx][nowy]=1;
                       // printf("%d %d
    ",nowx,nowy);
                    }
    
                }
                if(ch[i]=='B')
                {
                    if(t==1)
                    {
                        t=3;
                        nowy--;
    
                        book[nowx][nowy]=1;
                        //printf("%d %d
    ",nowx,nowy);
                    }
                   else  if(t==2)
                    {
                        t=4;
                        nowx--;
    
                        book[nowx][nowy]=1;
                        //printf("%d %d
    ",nowx,nowy);
    
                    }
                    else if(t==3)
                    {
                        t=1;
                        nowy++;
    
                        book[nowx][nowy]=1;
                        //printf("%d %d
    ",nowx,nowy);
                    }
                   else  if(t==4)
                    {
                        t=2;
                        nowx++;
    
                        book[nowx][nowy]=1;
                        //printf("%d %d
    ",nowx,nowy);
                    }
                }
                if(ch[i]=='R')
                {
                    if(t==1)
                    {
                        t=2;//
                        nowx++;
    
                        book[nowx][nowy]=1;
                        //printf("%d %d
    ",nowx,nowy);
                    }
                   else   if(t==2)
                    {
                        t=3;//西
                        nowy--;
    
                        book[nowx][nowy]=1;
                        //printf("%d %d
    ",nowx,nowy);
                    }
                   else if(t==3)
                    {
                        t=4;//
                        nowx--;
    
                        book[nowx][nowy]=1;
                        //printf("%d %d
    ",nowx,nowy);
                    }
                 else   if(t==4)
                    {
                        t=1;
                        nowy++;
    
                        book[nowx][nowy]=1;
                        //printf("%d %d
    ",nowx,nowy);
                    }
                }
                if(ch[i]=='L')
                {
                    if(t==1)
                    {
                        t=4;
                        nowx--;
    
                        book[nowx][nowy]=1;
                        //printf("%d %d
    ",nowx,nowy);
                    }
                  else   if(t==2)
                    {
                        t=1;
                        nowy++;
    
                        book[nowx][nowy]=1;
                       // printf("%d %d
    ",nowx,nowy);
                    }
                  else   if(t==3)
                    {
                        t=2;
                        nowx++;
    
                        book[nowx][nowy]=1;
                       // printf("%d %d
    ",nowx,nowy);
                    }
                  else  if(t==4)
                    {
                        t=3;
                        nowy--;
    
                        book[nowx][nowy]=1;
                        //printf("%d %d
    ",nowx,nowy);
                    }
                }
                i++;
                minx=min(minx,nowx);
                maxx=max(maxx,nowx);
                miny=min(miny,nowy);
                maxy=max(maxy,nowy);
            }
        int m=maxy-miny+1+1;int n=maxx-minx+1+2;
    
           printf("%d %d
    ",n,m);
          for(int i=minx-1 ; i<=maxx+1 ;i++)
          {
              for(int j=miny ;j<=maxy+1 ; j++)
                if(book[i][j]==1)
                 putchar('.');
                 else
                    putchar('#');
                 puts("");
          }
    
    
        }
        return 0;
    }
    View Code

    这是大佬的代码:

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

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

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

  • 相关阅读:
    NOI2018:屠龙勇士
    Hello world!
    bzoj5月月赛订正
    codeforces906 D
    bzoj2728 [HNOI2012]与非
    bzoj3884上帝与集合的正确用法
    bzoj2817[ZJOI2012]波浪
    2017多校联合赛1[题解]
    论如何优雅的用bitset来求四维偏序
    bzoj1488[HNOI2009]图的同构
  • 原文地址:https://www.cnblogs.com/shuaihui520/p/8975625.html
Copyright © 2011-2022 走看看