这题代码本可以优化很多,最后堆到160多行也是麻了
#include <iostream>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <set>
#include <cmath>
#include <queue>
#include <bitset>
#include <vector>
#include <unordered_map>
#define int long long
#define endl '\n'
#define lowbit(x) x &(-x)
#define mh(x) memset(x, -1, sizeof h)
#define debug(x) cerr << #x << "=" << x << endl;
#define brk exit(0);
#define rep(i, a, b) for (int i = a; i < b; ++i)
using namespace std;
const int N = 1e5 + 10;
const int M = 2 * N;
const int mod = 998244353;
const double esp = 1e-6;
const double pi = acos(-1);
typedef pair<int, int> PII;
typedef long long ll;
int n, m,t;
string op[N];
bool dfs(int u, int lx, int ly, int dir)
{
if (u == t+1)
{
//cout << lx << " " << ly << endl;
return ((lx == n) && (ly == m));
}
if (op[u] == "Forward")
{
if (dir == 1)
if (dfs(u + 1, lx, ly + 1, dir))
return true;
if (dir == 2)
if (dfs(u + 1, lx - 1, ly, dir))
return true;
if (dir == 3)
if (dfs(u + 1, lx, ly - 1, dir))
return true;
if (dir == 4)
if (dfs(u + 1, lx + 1, ly, dir))
return true;
}
else if (op[u] == "Right")
{
dir = dir - 1 + ((dir - 1 > 0) ? 0 : 4);
if (dfs(u + 1, lx, ly, dir))
return true;
}
else if (op[u] == "Left")
{
dir = (dir + 1) % 5 + ((dir + 1) % 5 > 0 ? 0 : 1);
if (dfs(u + 1, lx, ly, dir))
return true;
}
return false;
}
string opp[3] = {"Forward", "Right", "Left"};
void solve()
{
cin >> n >> m;
scanf("%d", &t);
for (int i = 1; i <= t; i++)
cin >> op[i];
int lx = 0, ly = 0;
int dir = 1;
for (int i = 1; i <= t; i++)
{
// if(i==8)
// {
// cout << dir << endl;
// cout << lx << " " << ly << endl;
// }
for (int j = 0; j < 3; j++)
{
if (opp[j] == op[i])
continue;
if (opp[j] == "Forward")
{
if (dir == 1)
if (dfs(i + 1, lx, ly + 1, dir))
{
cout << i << " ";
cout << opp[j] << endl;
return;
}
if (dir == 2)
if (dfs(i + 1, lx - 1, ly, dir))
{
cout << i <<" ";
cout << opp[j] << endl;
return;
}
if (dir == 3)
if (dfs(i + 1, lx, ly - 1, dir))
{
cout << i <<" ";
cout << opp[j] << endl;
return;
}
if (dir == 4)
if (dfs(i + 1, lx + 1, ly, dir))
{
cout << i <<" ";
cout << opp[j] << endl;
return;
}
}
else if (opp[j] == "Right")
{
if (dfs(i + 1, lx, ly, dir - 1 + ((dir - 1 > 0) ? 0 : 4)))
{
cout << i << " ";
cout << opp[j] << endl;
return;
}
}
else if (opp[j] == "Left")
{
if (dfs(i + 1, lx, ly, (dir + 1) % 5 + ((dir + 1) % 5 > 0 ? 0 : 1)))
{
cout << i <<" ";
cout << opp[j] << endl;
return;
}
}
}
if (op[i] =="Forward" ){
if (dir == 1)
ly += 1;
if (dir == 2)
lx -= 1;
if (dir == 3)
ly -= 1;
if (dir == 4)
lx += 1;
}
if(op[i]=="Right")
{
dir = dir - 1 + ((dir - 1 > 0) ? 0 : 4);
}
if(op[i]=="Left")
{
dir = (dir + 1) % 5 + ((dir + 1) % 5 > 0 ? 0 : 1);
}
}
}
signed main()
{
// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int T = 1;
// scanf("%d", &T);
while (T--)
solve();
return 0;
}