GlitchBot -HZNU寒假集训

One of our delivery robots is malfunctioning! The job of the robot is simple; it should follow a list of instructions in order to reach a target destination. The list of instructions is originally correct to get the robot to the target. However, something is going wrong as we upload the instructions into the robot’s memory. During the upload, one random instruction from the list takes on a different value than intended. Yes, there is always a single bad instruction in the robot’s memory and it always results in the robot arriving at an incorrect destination as it finishes executing the list of instructions.

The robot can execute the instructions “Left”, “Right”, and “Forward”. The “Left” and “Right” instructions do not result in spatial movement but result in a 9090-degree turn in the corresponding direction. “Forward” is the only instruction that results in spatial movement, causing the robot to move one unit in the direction it is facing. The robot always starts at the origin (0,0)(0,0) of a grid and faces north along the positive y-axis.

Given the coordinates of the target destination and the list of instructions that the robot has in its memory, you are to identify a correction to the instructions to help the robot reach the proper destination.

Input

The first line of the input contains the xx and yy integer coordinates of the target destination, where −50≤x≤50−50≤x≤50 and −50≤y≤50−50≤y≤50. The following line contains an integer nn representing the number of instructions in the list, where 1≤n≤501≤n≤50. The remaining nn lines each contain a single instruction. These instructions may be: “Left”, “Forward”, or “Right”.

Output

Identify how to correct the robot’s instructions by printing the line number (starting at 11) of an incorrect input instruction, followed by an instruction substitution that would make the robot reach the target destination. If there are multiple ways to fix the instructions, report the fix that occurs for the earliest line number in the sequence of instructions. There is always exactly one unique earliest fix.

Sample Input 1 Sample Output 1
3 2
11
Forward
Right
Forward
Forward
Left
Forward
Forward
Left
Forward
Right
Forward
8 Right
Sample Input 2 Sample Output 2
-1 1
3
Right
Left
Forward
1 Forward

题解:给一个指定的点,给机器人下指令,前进、向右或者向左转,然后机器人出了故障,中间有一个命令出错了(可能有多种解决方案,输出最早的),找到第几个出错,并输出正确指令。

 #include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
int x, y;
int n;
char ins[][];
int map[][] = {};
int go[][] = { {,},{,},{-,},{,-} };
int main()
{
int turn = ;
scanf("%d %d", &x, &y);
scanf("%d", &n);
for (int i = ; i <= n; i++)
{
scanf("%s", ins[i]);
}
for (int i = ; i <= n; i++)
{
if (strcmp(ins[i], "Forward") == )
{
strcpy(ins[i],"Right");
turn = ;
for (int j = ; j <= n; j++)
{
if (strcmp(ins[j], "Forward") == )
{
map[][] += go[turn][]; //y
map[][] += go[turn][]; //x
}
else if (strcmp(ins[j], "Right") == )
{
turn++;
if (turn > ) turn = ;
}
else
{
turn--;
if (turn < ) turn = ;
}
}
if (map[][] == y&&map[][] == x)
{
printf("%d %s\n", i, ins[i]);
break;
}
map[][] = ;
map[][] = ;
strcpy(ins[i], "Left");
turn = ;
for (int j = ; j <= n; j++)
{
if (strcmp(ins[j], "Forward") == )
{
map[][] += go[turn][]; //y
map[][] += go[turn][]; //x
}
else if (strcmp(ins[j], "Right") == )
{
turn++;
if (turn > ) turn = ;
}
else
{
turn--;
if (turn < ) turn = ;
}
}
if (map[][] == y&&map[][] == x)
{
printf("%d %s\n", i, ins[i]);
break;
}
strcpy(ins[i], "Forward");
map[][] = ;
map[][] = ;
}
else if (strcmp(ins[i], "Right") == )
{ strcpy(ins[i], "Forward");
turn = ;
for (int j = ; j <= n; j++)
{
if (strcmp(ins[j], "Forward") == )
{
map[][] += go[turn][]; //y
map[][] += go[turn][]; //x
}
else if (strcmp(ins[j], "Right") == )
{
turn++;
if (turn > ) turn = ;
}
else
{
turn--;
if (turn < ) turn = ;
}
}
if (map[][] == y&&map[][] == x)
{
printf("%d %s\n", i, ins[i]);
break;
}
map[][] = ;
map[][] = ;
strcpy(ins[i], "Left");
turn = ;
for (int j = ; j <= n; j++)
{
if (strcmp(ins[j], "Forward") == )
{
map[][] += go[turn][]; //y
map[][] += go[turn][]; //x
}
else if (strcmp(ins[j], "Right") == )
{
turn++;
if (turn > ) turn = ;
}
else
{
turn--;
if (turn < ) turn = ;
}
}
if (map[][] == y&&map[][] == x)
{
printf("%d %s\n", i, ins[i]);
break;
}
strcpy(ins[i], "Right");
map[][] = ;
map[][] = ;
}
else
{ strcpy(ins[i], "Right");
turn = ;
for (int j = ; j <= n; j++)
{
if (strcmp(ins[j], "Forward") == )
{
map[][] += go[turn][]; //y
map[][] += go[turn][]; //x
}
else if (strcmp(ins[j], "Right") == )
{
turn++;
if (turn > ) turn = ;
}
else
{
turn--;
if (turn < ) turn = ;
}
}
if (map[][] == y&&map[][] == x)
{
printf("%d %s\n", i, ins[i]);
break;
}
map[][] = ;
map[][] = ;
strcpy(ins[i], "Forward");
turn = ;
for (int j = ; j <= n; j++)
{
if (strcmp(ins[j], "Forward") == )
{
map[][] += go[turn][]; //y
map[][] += go[turn][]; //x
}
else if (strcmp(ins[j], "Right") == )
{
turn++;
if (turn > ) turn = ;
}
else
{
turn--;
if (turn < ) turn = ;
}
}
if (map[][] == y&&map[][] == x)
{
printf("%d %s\n", i, ins[i]);
break;
}
strcpy(ins[i], "Left");
map[][] = ;
map[][] = ;
} }
return ;
}
上一篇:less分页阅读


下一篇:设计模式学习——代理模式(Proxy Pattern)之 强制代理(强校验,防绕过)