链接:
https://cn.vjudge.net/problem/1148484/origin
题意:
机器人的某一步出错,导致到达不了指定位置,找出哪一步,并改正。
思路:
遍历即可,第一个答案输出
代码:
#include <bits/stdc++.h>
using namespace std;
int step[200];
int dx[] = {0 , 1 , 0 , -1};//上右下左顺序固定
int dy[] = {1 , 0 , -1 , 0};
int x_0,y_0,n;
bool judge()
{
int direction = 0;
int x = 0,y = 0;
for(int i = 0 ; i < n; i++)
{
if(step[i] == 0)
{
x = x + dx[direction];
y = y + dy[direction];
}
else if(step[i] == 1) direction = (direction + 3) % 4;
else if(step[i] == 2) direction = (direction + 1) % 4;
}
return (x == x_0 && y == y_0);
}
int main()
{
cin>>x_0>>y_0>>n;
string s;
for(int i = 0; i < n; i++)
{
cin>>s;
if(s[0] == 'F') step[i] = 0;
else if(s[0] == 'L') step[i] = 1;
else if(s[0] == 'R') step[i] = 2;
}
bool flag = 1;
for(int i = 0 ; i < n && flag; i++)
{
for(int j = 0; j <= 2; j++)
{
step[i] = (step[i] + 1) % 3; //太精髓了吧。
if(judge())
{
printf("%d ",i+1);
if(step[i] == 0) cout<<"Forward"<<endl;
else if(step[i] == 1) cout<<"Left"<<endl;
else if(step[i] == 2) cout<<"Right"<<endl;
flag = 0;
break;
}
}
}
return 0;
}