题意 :
- 给出n个食物及坐标,必须按顺序吃掉,且只有在吃掉一个后,才会出现下一个。
- 从(x, y)出发,沿着d方向,每次可以前进或者顺时针或者逆时针转90度,用时都为1,求构造用时最少的方案并记录方案。
思路 :
- 因为只有在吃掉一个后才会出现下一个,所以只要简单模拟,贪心地在当前吃完的地方直接比较两种转弯方式即可,不需要考虑更后面的步数,互不影响。
- 当前方向如果和某个方向一致,那需要先走这个方向,但实际写可以两种方式都run一遍比较方案长短。
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <map>
#define endl '\n'
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
using namespace std;
const double pi = acos(-1);
typedef long long ll;
string go(int ex, int ey, int &d, int sx, int sy)
{
string ans = "";
if (ex < sx && d != 1)
{
if (d == 0) ans += "c";
if (d == 2) ans += "u";
if (d == 3) ans += "cc";
d = 1;
}
if (ex > sx && d != 3)
{
if (d == 0) ans += "u";
if (d == 1) ans += "cc";
if (d == 2) ans += "c";
d = 3;
}
ans += string(abs(ex - sx), 'f');
if (ey < sy && d != 0)
{
if (d == 1) ans += "u";
if (d == 2) ans += "cc";
if (d == 3) ans += "c";
d = 0;
}
if (ey > sy && d != 2)
{
if (d == 0) ans += "cc";
if (d == 1) ans += "c";
if (d == 3) ans += "u";
d = 2;
}
ans += string(abs(ey - sy), 'f');
return ans;
}
string go2(int ex, int ey, int &d, int sx, int sy)
{
string ans = "";
if (ey < sy && d != 0)
{
if (d == 1) ans += "u";
if (d == 2) ans += "cc";
if (d == 3) ans += "c";
d = 0;
}
if (ey > sy && d != 2)
{
if (d == 0) ans += "cc";
if (d == 1) ans += "c";
if (d == 3) ans += "u";
d = 2;
}
ans += string(abs(ey - sy), 'f');
if (ex < sx && d != 1)
{
if (d == 0) ans += "c";
if (d == 2) ans += "u";
if (d == 3) ans += "cc";
d = 1;
}
if (ex > sx && d != 3)
{
if (d == 0) ans += "u";
if (d == 1) ans += "cc";
if (d == 2) ans += "c";
d = 3;
}
ans += string(abs(ex - sx), 'f');
return ans;
}
int main()
{
IOS;
int T;
cin >> T;
while (T -- )
{
int ex, ey, d;
cin >> ex >> ey >> d;
int n;
cin >> n;
string ans = "";
for (int i = 1; i <= n; i ++ )
{
int sx, sy;
cin >> sx >> sy;
int d1 = d, d2 = d;
string t1 = go(ex, ey, d1, sx, sy);
string t2 = go2(ex, ey, d2, sx, sy);
if (t1.size() < t2.size()) ans += t1, d = d1;
else ans += t2, d = d2;
ex = sx, ey = sy;
}
cout << ans << endl;
}
return 0;
}