F - GlitchBot(我就是个**)

这题代码本可以优化很多,最后堆到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;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值