#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
int m,n;
int t;
int flag = 0;
int f = 0;
string s[60],str[4] = {"Forward","Right","Left"};
int dir[4][2] ={
{1,0},{0,1},{-1,0},{0,-1}
};
int main()
{
cin>>m>>n>>t;
for(int i = 0;i<t;i++)
cin>>s[i];
for(int i = 0;i<t;i++){
string str1 = s[i];
int f = 0;
for(int k = 0;k<3;k++){
s[i] = str[k];
flag = 0;
//cout<<s[i]<<" "<<str[k]<<endl;
int x = 0;
int y = 0;
/*cout<<i<<" "<<k<<endl;
for(int i = 0;i<t;i++)
cout<<s[i]<<" ";
cout<<endl;*/
for(int j = 0;j<t;j++){
//cout<<flag<<endl;
if(s[j]=="Forward"){
x=x+dir[flag][0];
y=y+dir[flag][1];
// cout<<dir[flag][0]<<" "<<dir[flag][1]<<endl;
}else if(s[j]=="Right"){
if(flag == 0)
flag = 3;
else if(flag == 1)
flag = 0;
else if(flag == 2)
flag = 1;
else if(flag == 3)
flag = 2;
}else if(s[j]=="Left"){
if(flag == 0)
flag = 1;
else if(flag == 1)
flag = 2;
else if(flag == 2)
flag = 3;
else if(flag == 3)
flag = 0;
}
//cout<<flag<<" "<<j<<" "<<x<<" "<<y<<endl;
}
if(-y==m&&x==n){
cout<<i+1<<" "<<s[i]<<endl;
f = 1;
}
s[i] = str1;
if(f)
break;
}
if(f)
break;
}
return 0;
}
问题 F: GlitchBot
时间限制: 1 Sec 内存限制: 128 MB
提交: 219 解决: 106
[提交] [状态] [命题人:admin]
题目描述
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 90-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) 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.
输入
The first line of the input contains the x and y integer coordinates of the target destination, where −50 ≤ x ≤ 50 and −50 ≤ y ≤ 50. The following line contains an integer n representing the number of instructions in the list, where 1 ≤ n ≤ 50. The remaining n lines each contain a single instruction. These instructions may be: “Left”, “Forward”, or “Right”.
输出
Identify how to correct the robot’s instructions by printing the line number (starting at 1) 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.
样例输入
复制样例数据
3 2
11
Forward
Right
Forward
Forward
Left
Forward
Forward
Left
Forward
Right
Forward
样例输出
8 Right