Smzzl with Greedy Snake 模拟-贪心-阅读理解

题意 :

  • 给出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;
}

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值