D - Robots Easy
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).
1.bfs
//Full of love and hope for life
#include <iostream>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <stdio.h>
#include <queue>
using namespace std;
struct node
{
int x,y,step;
};
struct pre
{
int x,y;
char c;
} p[15][15];
node n,m;
int nex[4][2]= {{1,0},{-1,0},{0,1},{0,-1}};
int flag[15][15];
int s[15][15];
void dfs(int x,int y)//记录走的方向
{
if(x==p[x][y].x&&y==p[x][y].y)
{
return ;
}
dfs(p[x][y].x,p[x][y].y);
cout << p[x][y].c;
}
void bfs(int x,int y)
{
queue<node>q;
n.x=x;
n.y=y;
n.step=0;
flag[n.x][n.y]=1;
q.push(n);
while(!q.empty())
{
m=q.front();
q.pop();
if(s[m.x][m.y]==1)
{
cout << m.step << endl;
dfs(m.x,m.y);
return ;
}
for(int i=0; i<4; i++)
{
n.x=m.x+nex[i][0];
n.y=m.y+nex[i][1];
if(flag[n.x][n.y]==1||s[n.x][n.y]==2||n.x<1||n.x>12||n.y<1||n.y>12)
{
continue;
}
p[n.x][n.y].x=m.x;
p[n.x][n.y].y=m.y;
if(i==0)
{
p[n.x][n.y].c='D';
}
if(i==1)
{
p[n.x][n.y].c='U';
}
if(i==2)
{
p[n.x][n.y].c='R';
}
if(i==3)
{
p[n.x][n.y].c='L';
}
flag[n.x][n.y]=1;
n.step=m.step+1;
q.push(n);
}
}
}
int main()
{
int a,b,c;
cin >> a;
s[3][3]=1;
s[3][10]=1;
s[10][3]=1;
s[10][10]=1;
s[6][6]=2;
s[6][7]=2;
s[7][6]=2;
s[7][7]=2;
s[9][2]=2;
s[9][3]=2;
s[9][10]=2;
s[9][11]=2;
s[10][2]=2;
s[10][11]=2;
while(a--)
{
for(int i=1; i<=12; i++)
{
for(int j=1; j<=12; j++)
{
p[i][j].x=i;
p[i][j].y=j;
p[i][j].c='0';
}
}//初始化
memset(flag,0,sizeof(flag));
cin >> b >> c;
bfs(b,c);
cout << endl;
}
return 0;
}
2.打表
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int a,b,c;
cin >> a;
while(a--)
{
cin >> b >> c;
if(b==1&&c==1)
{
cout << 4 << endl;
cout << "DDRR" << endl;
}
if(b==1&&c==2)
{
cout << 3 << endl;
cout << "DDR" << endl;
}
if(b==1&&c==3)
{
cout << 2 << endl;
cout << "DD" << endl;
}
if(b==1&&c==4)
{
cout << 3 << endl;
cout << "DDL" << endl;
}
if(b==1&&c==5)
{
cout << 4 << endl;
cout << "DDLL" << endl;
}
if(b==1&&c==6)
{
cout << 5 << endl;
cout << "DDLLL" << endl;
}
if(b==1&&c==7)
{
cout << 5 << endl;
cout << "DDRRR" << endl;
}
if(b==1&&c==8)
{
cout << 4 << endl;
cout << "DDRR" << endl;
}
if(b==1&&c==9)
{
cout << 3 << endl;
cout << "DDR" << endl;
}
if(b==1&&c==10)
{
cout << 2 << endl;
cout << "DD" << endl;
}
if(b==1&&c==11)
{
cout << 3 << endl;
cout << "DDL" << endl;
}
if(b==1&&c==12)
{
cout << 4 << endl;
cout << "DDLL" << endl;
}
if(b==2&&c==1)
{
cout << 3 << endl;
cout << "DRR" << endl;
}
if(b==2&&c==2)
{
cout << 2 << endl;
cout << "DR" << endl;
}
if(b==2&&c==3)
{
cout << 1 << endl;
cout << "D" << endl;
}
if(b==2&&c==4)
{
cout << 2 << endl;
cout << "DL" << endl;
}
if(b==2&&c==5)
{
cout << 3 << endl;
cout << "DLL" << endl;
}
if(b==2&&c==6)
{
cout << 4 << endl;
cout << "DLLL" << endl;
}
if(b==2&&c==7)
{
cout << 4 << endl;
cout << "DRRR" << endl;
}
if(b==2&&c==8)
{
cout << 3 << endl;
cout << "DRR" << endl;
}
if(b==2&&c==9)
{
cout << 2 << endl;
cout << "DR" << endl;
}
if(b==2&&c==10)
{
cout << 1 << endl;
cout << "D" << endl;
}
if(b==2&&c==11)
{
cout << 2 << endl;
cout << "DL" << endl;
}
if(b==2&&c==12)
{
cout << 3 << endl;
cout << "DLL" << endl;
}
if(b==3&&c==1)
{
cout << 2 << endl;
cout << "RR" << endl;
}
if(b==3&&c==2)
{
cout << 1 << endl;
cout << "R" << endl;
}
if(b==3&&c==3)
{
cout << 0 << endl;
}
if(b==3&&c==4)
{
cout << 1 << endl;
cout << "L" << endl;
}
if(b==3&&c==5)
{
cout << 2 << endl;
cout << "LL" << endl;
}
if(b==3&&c==6)
{
cout << 3 << endl;
cout << "LLL" << endl;
}
if(b==3&&c==7)
{
cout << 3 << endl;
cout << "RRR" << endl;
}
if(b==3&&c==8)
{
cout << 2 << endl;
cout << "RR" << endl;
}
if(b==3&&c==9)
{
cout << 1 << endl;
cout << "R" << endl;
}
if(b==3&&c==10)
{
cout << 0 << endl;
}
if(b==3&&c==11)
{
cout << 1 << endl;
cout << "L" << endl;
}
if(b==3&&c==12)
{
cout << 2 << endl;
cout << "LL" << endl;
}
if(b==4&&c==1)
{
cout << 3 << endl;
cout << "URR" << endl;
}
if(b==4&&c==2)
{
cout << 2 << endl;
cout << "UR" << endl;
}
if(b==4&&c==3)
{
cout << 1 << endl;
cout << "U" << endl;
}
if(b==4&&c==4)
{
cout << 2 << endl;
cout << "UL" << endl;
}
if(b==4&&c==5)
{
cout << 3 << endl;
cout << "ULL" << endl;
}
if(b==4&&c==6)
{
cout << 4 << endl;
cout << "ULLL" << endl;
}
if(b==4&&c==7)
{
cout << 4 << endl;
cout << "URRR" << endl;
}
if(b==4&&c==8)
{
cout << 3 << endl;
cout << "URR" << endl;
}
if(b==4&&c==9)
{
cout << 2 << endl;
cout << "UR" << endl;
}
if(b==4&&c==10)
{
cout << 1 << endl;
cout << "U" << endl;
}
if(b==4&&c==11)
{
cout << 2 << endl;
cout << "UL" << endl;
}
if(b==4&&c==12)
{
cout << 3 << endl;
cout << "ULL" << endl;
}
if(b==5&&c==1)
{
cout << 4 << endl;
cout << "UURR" << endl;
}
if(b==5&&c==2)
{
cout << 3 << endl;
cout << "UUR" << endl;
}
if(b==5&&c==3)
{
cout << 2 << endl;
cout << "UU" << endl;
}
if(b==5&&c==4)
{
cout << 3 << endl;
cout << "UUL" << endl;
}
if(b==5&&c==5)
{
cout << 4 << endl;
cout << "UULL" << endl;
}
if(b==5&&c==6)
{
cout << 5 << endl;
cout << "UULLL" << endl;
}
if(b==5&&c==7)
{
cout << 5 << endl;
cout << "UURRR" << endl;
}
if(b==5&&c==8)
{
cout << 4 << endl;
cout << "UURR" << endl;
}
if(b==5&&c==9)
{
cout << 3 << endl;
cout << "UUR" << endl;
}
if(b==5&&c==10)
{
cout << 2 << endl;
cout << "UU" << endl;
}
if(b==5&&c==11)
{
cout << 3 << endl;
cout << "UUL" << endl;
}
if(b==5&&c==12)
{
cout << 4 << endl;
cout << "UULL" << endl;
}
if(b==6&&c==1)
{
cout << 5 << endl;
cout << "UUURR" << endl;
}
if(b==6&&c==2)
{
cout << 4 << endl;
cout << "UUUR" << endl;
}
if(b==6&&c==3)
{
cout << 3 << endl;
cout << "UUU" << endl;
}
if(b==6&&c==4)
{
cout << 4 << endl;
cout << "UUUL" << endl;
}
if(b==6&&c==5)
{
cout << 5 << endl;
cout << "UUULL" << endl;
}
if(b==6&&c==8)
{
cout << 5 << endl;
cout << "UUURR" << endl;
}
if(b==6&&c==9)
{
cout << 4 << endl;
cout << "UUUR" << endl;
}
if(b==6&&c==10)
{
cout << 3 << endl;
cout << "UUU" << endl;
}
if(b==6&&c==11)
{
cout << 4 << endl;
cout << "UUUL" << endl;
}
if(b==6&&c==12)
{
cout << 5 << endl;
cout << "UUULL" << endl;
}
if(b==7&&c==1)
{
cout << 7 << endl;
cout << "RRRDDDL" << endl;
}
if(b==7&&c==2)
{
cout << 6 << endl;
cout << "RRDDDL" << endl;
}
if(b==7&&c==3)
{
cout << 5 << endl;
cout << "RDDDL" << endl;
}
if(b==7&&c==4)
{
cout << 4 << endl;
cout << "DDDL" << endl;
}
if(b==7&&c==5)
{
cout << 5 << endl;
cout << "DDDLL" << endl;
}
if(b==7&&c==8)
{
cout << 5 << endl;
cout << "DDDRR" << endl;
}
if(b==7&&c==9)
{
cout << 4 << endl;
cout << "DDDR" << endl;
}
if(b==7&&c==10)
{
cout << 5 << endl;
cout << " LDDDR" << endl;
}
if(b==7&&c==11)
{
cout << 6 << endl;
cout << "LLDDDR" << endl;
}
if(b==7&&c==12)
{
cout << 7 << endl;
cout << "LLLDDDR" << endl;
}
if(b==8&&c==1)
{
cout << 6 << endl;
cout << "RRRDDL" << endl;
}
if(b==8&&c==2)
{
cout << 5 << endl;
cout << "RRDDL" << endl;
}
if(b==8&&c==3)
{
cout << 4 << endl;
cout << "RDDL" << endl;
}
if(b==8&&c==4)
{
cout << 3 << endl;
cout << "DDL" << endl;
}
if(b==8&&c==5)
{
cout << 4 << endl;
cout << "DDLL" << endl;
}
if(b==8&&c==6)
{
cout << 5 << endl;
cout << "DDLLL" << endl;
}
if(b==8&&c==7)
{
cout << 5 << endl;
cout << "DDRRR" << endl;
}
if(b==8&&c==8)
{
cout << 4 << endl;
cout << "DDRR" << endl;
}
if(b==8&&c==9)
{
cout << 3 << endl;
cout << "DDR" << endl;
}
if(b==8&&c==10)
{
cout << 4 << endl;
cout << "LDDR" << endl;
}
if(b==8&&c==11)
{
cout << 5 << endl;
cout << "LLDDR" << endl;
}
if(b==8&&c==12)
{
cout << 6 << endl;
cout << "LLLDDR" << endl;
}
if(b==9&&c==1)
{
cout << 5 << endl;
cout << "DDRRU" << endl;
}
if(b==9&&c==4)
{
cout << 2 << endl;
cout << "DL" << endl;
}
if(b==9&&c==5)
{
cout << 3 << endl;
cout << "DLL" << endl;
}
if(b==9&&c==6)
{
cout << 4 << endl;
cout << "DLLL" << endl;
}
if(b==9&&c==7)
{
cout << 4 << endl;
cout << "DRRR" << endl;
}
if(b==9&&c==8)
{
cout << 3 << endl;
cout << "DRR" << endl;
}
if(b==9&&c==9)
{
cout << 2 << endl;
cout << "DR" << endl;
}
if(b==9&&c==12)
{
cout << 5 << endl;
cout << "DDLLU" << endl;
}
if(b==10&&c==1)
{
cout << 4 << endl;
cout << "DRRU" << endl;
}
if(b==10&&c==3)
{
cout << 0 << endl;
}
if(b==10&&c==4)
{
cout << 1 << endl;
cout << "L" << endl;
}
if(b==10&&c==5)
{
cout << 2 << endl;
cout << "LL" << endl;
}
if(b==10&&c==6)
{
cout << 3 << endl;
cout << "LLL" << endl;
}
if(b==10&&c==7)
{
cout << 3 << endl;
cout << "RRR" << endl;
}
if(b==10&&c==8)
{
cout << 2 << endl;
cout << "RR" << endl;
}
if(b==10&&c==9)
{
cout << 1 << endl;
cout << "R" << endl;
}
if(b==10&&c==10)
{
cout << 0 << endl;
}
if(b==10&&c==12)
{
cout << 4 << endl;
cout << "DLLU" << endl;
}
if(b==11&&c==1)
{
cout << 3 << endl;
cout << "RRU" << endl;
}
if(b==11&&c==2)
{
cout << 2 << endl;
cout << "RU" << endl;
}
if(b==11&&c==3)
{
cout << 1 << endl;
cout << "U" << endl;
}
if(b==11&&c==4)
{
cout << 2 << endl;
cout << "LU" << endl;
}
if(b==11&&c==5)
{
cout << 3 << endl;
cout << "LLU" << endl;
}
if(b==11&&c==6)
{
cout << 4 << endl;
cout << "LLLU" << endl;
}
if(b==11&&c==7)
{
cout << 4 << endl;
cout << "RRRU" << endl;
}
if(b==11&&c==8)
{
cout << 3 << endl;
cout << "RRU" << endl;
}
if(b==11&&c==9)
{
cout << 2 << endl;
cout << "RU" << endl;
}
if(b==11&&c==10)
{
cout << 1 << endl;
cout << "U" << endl;
}
if(b==11&&c==11)
{
cout << 2 << endl;
cout << "LU" << endl;
}
if(b==11&&c==12)
{
cout << 3 << endl;
cout << "LLU" << endl;
}
if(b==12&&c==1)
{
cout << 4 << endl;
cout << "RRUU" << endl;
}
if(b==12&&c==2)
{
cout << 3 << endl;
cout << "RUU" << endl;
}
if(b==12&&c==3)
{
cout << 2 << endl;
cout << "UU" << endl;
}
if(b==12&&c==4)
{
cout << 3 << endl;
cout << "LUU" << endl;
}
if(b==12&&c==5)
{
cout << 4 << endl;
cout << "LLUU" << endl;
}
if(b==12&&c==6)
{
cout << 5 << endl;
cout << "LLLUU" << endl;
}
if(b==12&&c==7)
{
cout << 5 << endl;
cout << "RRRUU" << endl;
}
if(b==12&&c==8)
{
cout << 4 << endl;
cout << "RRUU" << endl;
}
if(b==12&&c==9)
{
cout << 3 << endl;
cout << "RUU" << endl;
}
if(b==12&&c==10)
{
cout << 2 << endl;
cout << "UU" << endl;
}
if(b==12&&c==11)
{
cout << 3 << endl;
cout << "LUU" << endl;
}
if(b==12&&c==12)
{
cout << 4 << endl;
cout << "LLUU" << endl;
}
}
return 0;
}