D - Robots Easy Gym - 102267D 搜索+路径记录

Alice was playing her favorite game, “Tales of the Abyss”, while playing she encountered the following puzzle that can be described as a 12×1212×12 grid:

在这里插入图片描述
A robot is standing somewhere in this grid, you can order the robot to move up, down, right or left. The robot can’t move to a blocked cell(fully black cell), but it can move to other cells(white and crossed cells). If you order the robot to move to a blocked cell or to move outside the grid then the robot won’t move.

The goal of the puzzle is to move the robot to a crossed cell in at most 1000 moves. You will be given the description of some levels of this puzzle that Alice has solved, can you solve them too?

Input
In the first line you will be given the integer LL(1≤L≤1341≤L≤134), the number of levels you need to solve.

Then you will be given LL lines describing the levels, each line will contain two integers r,cr,c(1≤r,c≤121≤r,c≤12), the row and column of the cell the robot is currently standing at, it’s guaranteed that this cell is not a blocked cell(not a fully black cell). The rows are numbered from top(1) to bottom(12), and columns are numbered from left(1) to right(12).

Output
For each level output exactly 2 lines, the first line containing a single integer nn(0≤n≤10000≤n≤1000), representing the number of moves you want to make, and the second line a string of length nn made of the letters ‘U’,‘D’,‘R’ and ‘L’(upper_case) meaning Up, Down, Right and Left describing the moves.

Please note that in order to get accepted you have to follow the above format exactly, printing the number of moves and the string on the same line, or printing lower_case letters instead of upper_case for example might give you a wrong answer verdict.

Example
Input
2
2 3
9 4
Output
4
UUDD
3
LDL
Note
In the first level, the robot moves as follows: (2,3)→(1,3)→(1,3)→(2,3)→(3,3)(2,3)→(1,3)→(1,3)→(2,3)→(3,3).

In the second level, the robot moves as follows: (9,4)→(9,4)→(10,4)→(10,3)(9,4)→(9,4)→(10,4)→(10,3).

路径输出有2种方式,一种是用STL提供的queue,但是必须在结构体中加step或pre,来标记他本身的操作或上一个操作,第二种是手动写队列,通过数组下标来记录,这种简单

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
using namespace std;
#define PI 3.14159265358
const int maxn = 1e6+100;
int a[22][22];
int vis[22][22];
int ans[maxn];
struct node
{
    int x;
    int y;
    int step;
    int pre;
    int p;
} q[maxn];
int k;
void bfs(int s,int d)
{
    int head = 0;
    int tail = 0;
    q[tail].x = s;
    q[tail].y = d;
    q[tail].step = 0;
    q[tail].pre = -1;
    tail++;
    while(head!=tail)
    {
        struct node tmp = q[head];
        if(vis[tmp.x][tmp.y]==1)
        {
            printf("%d\n",tmp.step);
            int len = 0;
            for(int i=head; i!=-1; i=q[i].pre)
            {
                ans[len++] = q[i].p;
            }
            for(int i=len-1; i>=0; i--)
            {
                if(ans[i]==1)
                    printf("U");
                else if(ans[i]==2)
                    printf("D");
                else if(ans[i]==3)
                    printf("L");
                else if(ans[i]==4)
                    printf("R");
            }
            printf("\n");
            return;
        }
        for(int i=0; i<4; i++)
        {
            if(i==0)
            {
                q[tail].x = tmp.x+1;
                q[tail].y = tmp.y;
                if(q[tail].x>12||vis[q[tail].x][q[tail].y]==-1)
                    q[tail].x = tmp.x;
                q[tail].pre = head;
                q[tail].p = 2;
                q[tail].step = tmp.step+1;
                tail++;
            }
            else if(i==1)
            {
                q[tail].x = tmp.x-1;
                q[tail].y = tmp.y;
                if(q[tail].x<1||vis[q[tail].x][q[tail].y]==-1)
                    q[tail].x = tmp.x;
                q[tail].pre = head;
                q[tail].step = tmp.step+1;
                q[tail].p = 1;
                tail++;
            }

            else if(i==2)
            {
                q[tail].y = tmp.y-1;
                q[tail].x = tmp.x;
                if(q[tail].y<1||vis[q[tail].x][q[tail].y]==-1)
                    q[tail].y = tmp.y;
                q[tail].pre = head;
                q[tail].p = 3;
                q[tail].step = tmp.step+1;
                tail++;
            }
            else if(i==3)
            {
                q[tail].y = tmp.y+1;
                q[tail].x = tmp.x;
                if(q[tail].y>12||vis[q[tail].x][q[tail].y]==-1)
                    q[tail].y = tmp.y;
                q[tail].pre = head;
                q[tail].p = 4;
                q[tail].step = tmp.step+1;
                tail++;
            }
        }
        head++;
    }
}

int main()
{
    ios::sync_with_stdio(false);
    int T;
    cin>>T;
    memset(vis,0,sizeof(vis));
    vis[6][6] = vis[6][7] = vis[7][6] = vis[7][7] = -1;
    vis[9][2] = vis[9][3] = vis[10][2] = -1;
    vis[9][10] = vis[9][11] = vis[10][11] = -1;
    vis[3][3] = vis[3][10] = vis[10][3] = vis[10][10] = 1;
    while(T--)
    {
        int r,c;
        cin>>r>>c;
        bfs(r,c);
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值