日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時間:2025/3/19 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Smzzl with Greedy Snake 模拟-贪心-阅读理解 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題意 :

  • 給出n個食物及坐標(biāo),必須按順序吃掉,且只有在吃掉一個后,才會出現(xiàn)下一個。
  • 從(x, y)出發(fā),沿著d方向,每次可以前進或者順時針或者逆時針轉(zhuǎn)90度,用時都為1,求構(gòu)造用時最少的方案并記錄方案。

思路 :

  • 因為只有在吃掉一個后才會出現(xiàn)下一個,所以只要簡單模擬,貪心地在當(dāng)前吃完的地方直接比較兩種轉(zhuǎn)彎方式即可,不需要考慮更后面的步數(shù),互不影響。
  • 當(dāng)前方向如果和某個方向一致,那需要先走這個方向,但實際寫可以兩種方式都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; }

總結(jié)

以上是生活随笔為你收集整理的Smzzl with Greedy Snake 模拟-贪心-阅读理解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。