📣 黑五网一特惠|VIP一年通行证5折,蓝莓、Offer多多同步特惠
12
返回列表 发新帖
楼主: yF68U
跳转到指定楼层
上一主题 下一主题
收起左侧

Google 很有意思的新题(hard)

地里匿名用户
🔗
匿名用户-OKKNE  2022-4-20 12:42:44
yF68U 发表于 2022-4-19 20:39
原来刷题网已经有这个tag了…好吧那我真的落伍了,这个是真没想到

算法来了,和这个类似 https://leetcode.com/problems/cat-and-mouse/solution/ , 主要在于建立状态,和分析neighbors。

  1. #include <iostream>
  2. #include <vector>
  3. #include <array>
  4. #include <map>
  5. #include <set>

  6. using namespace std;

  7. class Solution {

  8.     vector<set<array<int, 4>>> m; // #piles -> all  possibilities
  9.     map<array<int, 4>, int> nei; // state->result, 每个state有3个结果:0平局,1先走者负,2后走者负。

  10. public:

  11.     // 创建m:key为非0堆(pile)的个数,value为每种情况的位表示,每个堆可以用4 bits表示,每个integer表示一个color,所以有4个integer,组成一个array。
  12.     void gc2g(int i, int r, vector<int>& a) {
  13.         //cout << i << "," << r << "," << a.size() << endl;
  14.         if (i > 11) cout << i << endl;
  15.         if (r == 0 || i == 11) {
  16.             int pre = a[i];
  17.             a[i] = r;
  18.             array<int, 4> aa;
  19.             int nz = 0;
  20.             auto b = a;
  21.             for (int j = 0; j < 4; j++) {
  22.                 std::sort(b.begin() + j * 3, b.begin() + j * 3 + 3);
  23.                 aa[j] = (b[3 * j] << 8) | (b[3 * j + 1] << 4) | b[3 * j + 2];
  24.                 if (b[3 * j]) nz++;
  25.                 if (b[3 * j + 1]) nz++;
  26.                 if (b[3 * j + 2]) nz++;
  27.             }
  28.             std::sort(aa.begin(), aa.end());
  29.             m[nz].insert(aa);
  30.             a[i] = pre;
  31.         }
  32.         else {
  33.             for (int j = r; j >= 0; j--) {
  34.                 int pre = a[i];
  35.                 a[i] = j;
  36.                 gc2g(i + 1, r - j, a);
  37.                 a[i] = pre;
  38.             }
  39.         }
  40.     }

  41.     // 找出每种状态对应的结果nei
  42.     void getnei() {
  43.         for (int i = 1; i <= 12; i++) {
  44.             if (i == 1) {
  45.                 for (auto& t : m[1]) nei[t] = 1; // 先走者负
  46.             }
  47.             else {
  48.                 for (auto& t : m[i]) { // t is 4-tuple
  49.                     vector<int> cnt(3, 0); // 分析上一层neighbors具有的状态
  50.                     vector<int> a(12, 0);
  51.                     for (int j = 0; j < 4; j++) {
  52.                         a[j * 3] = t[j] >> 8;
  53.                         a[j * 3 + 1] = (t[j] >> 4) & 15;
  54.                         a[j * 3 + 2] = (t[j] & 15);
  55.                     }
  56.                     for (int j = 0; j < 11; j++) {
  57.                         if (a[j] == 0) continue;
  58.                         for (int k = j + 1; k < 12; k++) {
  59.                             if (a[k] == 0) continue;
  60.                             if (j / 3 == k / 3) {
  61.                                 auto b = a;
  62.                                 auto key = t;
  63.                                 int ind = j / 3;
  64.                                 b[j] += b[k];
  65.                                 b[k] = 0;

  66.                                 std::sort(b.begin() + ind * 3, b.begin() + ind * 3 + 3);
  67.                                 key[ind] = (b[ind * 3] << 8) | (b[ind * 3 + 1] << 4) | b[ind * 3 + 2];
  68.                                 sort(key.begin(), key.end());
  69.                                 cnt[nei[key]] = 1;
  70.                             }
  71.                             else if (a[j] == a[k]) {
  72.                                 auto b = a;
  73.                                 auto key = t;
  74.                                 b[j] += b[k];
  75.                                 b[k] = 0;

  76.                                 int ind = j / 3;
  77.                                 std::sort(b.begin() + ind * 3, b.begin() + ind * 3 + 3);
  78.                                 key[ind] = (b[ind * 3] << 8) | (b[ind * 3 + 1] << 4) | b[ind * 3 + 2];

  79.                                 ind = k / 3;
  80.                                 std::sort(b.begin() + ind * 3, b.begin() + ind * 3 + 3);
  81.                                 key[ind] = (b[ind * 3] << 8) | (b[ind * 3 + 1] << 4) | b[ind * 3 + 2];

  82.                                 sort(key.begin(), key.end());
  83.                                 cnt[nei[key]] = 1;

  84.                                 b = a;
  85.                                 key = t;
  86.                                 b[k] += b[j];
  87.                                 b[j] = 0;

  88.                                 ind = k / 3;
  89.                                 std::sort(b.begin() + ind * 3, b.begin() + ind * 3 + 3);
  90.                                 key[ind] = (b[ind * 3] << 8) | (b[ind * 3 + 1] << 4) | b[ind * 3 + 2];

  91.                                 ind = j / 3;
  92.                                 std::sort(b.begin() + ind * 3, b.begin() + ind * 3 + 3);
  93.                                 key[ind] = (b[ind * 3] << 8) | (b[ind * 3 + 1] << 4) | b[ind * 3 + 2];

  94.                                 sort(key.begin(), key.end());
  95.                                 cnt[nei[key]] = 1;
  96.                             }
  97.                         }
  98.                     }
  99.                     if (i == 12) cout << cnt[0] << "," << cnt[1] << "," << cnt[2] << endl;
  100.                     int sum = cnt[0] + cnt[1] + cnt[2];
  101.                     if (sum == 0) nei[t] = 1; // 不能移动,所以先走者负
  102.                     else if (cnt[1] = 1) { // move到上一层对应的这个状态,先走者嬴,后走者负。
  103.                         nei[t] = 2; // 后走者负
  104.                     }
  105.                     else if (cnt[0] == 1) { // 平局
  106.                         nei[t] = 0; //平局
  107.                     }
  108.                     else // cnt[2] = 1, 每个move都导致先走者负
  109.                         nei[t] = 1;
  110.                 }
  111.             }
  112.         }
  113.     }

  114.     int test() {
  115.         m.assign(13, set<array<int, 4>>()); // sorted within color and sorted between colors
  116.         vector<int> a(12, 0);
  117.         gc2g(0, 12, a); // index, remains, a

  118.         int sum = 0;
  119.         for (auto& t : m) {
  120.             sum += t.size();
  121.         }
  122.         cout << sum << endl;
  123.         cout << m[12].size() << endl;
  124.         cout << m[1].size() << endl;

  125.         getnei();

  126.         int x = (1 << 8) | (1 << 4) | 1;
  127.         array<int, 4> key = { x, x, x, x };
  128.         cout << nei[key] << endl;

  129.         for (auto& t : m[3]) {
  130.             for (int i = 0; i < 4; i++) {
  131.                 cout << (t[i] >> 8) << ",";
  132.                 cout << ((t[i] >> 4) & 15) << ",";
  133.                 cout << (t[i] & 15) << ",";
  134.             }
  135.             cout << "-->" << nei[t] << endl;
  136.         }

  137.         return 0;
  138.     }
  139. };

  140. int main(int argc, char* argv[])
  141. {
  142.     Solution sol;
  143.     sol.test();

  144.     return 0;
  145. }
复制代码
回复

使用道具 举报

🔗
1Z3J 2022-4-20 16:19:34 | 只看该作者
全局:
请问楼主是社招吗 30分钟想出这个题目并写完代码 难度对没打过竞赛的ng感觉额... 这就是谷歌吧
回复

使用道具 举报

🔗
 楼主| yF68U 2022-4-20 22:14:12 | 只看该作者
全局:
1Z3J 发表于 2022-4-20 16:19
请问楼主是社招吗 30分钟想出这个题目并写完代码 难度对没打过竞赛的ng感觉额... 这就是谷歌吧

社招,面的L5,不确定狗对于不同level的coding题目难度是不是有明显差别,至少别的公司我没看出来有差别
回复

使用道具 举报

地里匿名用户
🔗
匿名用户-OKKNE  2022-4-21 03:16:50
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Popular Interview Questions from Top Companies
回复

使用道具 举报

🔗
immikiya12 2022-4-21 06:51:42 | 只看该作者
全局:
应该是minmax + 剪枝

minmax很简单,做一个DFS,在每个终局回溯结果+1或-1

剪枝就是如果一个节点已经有确定对手有必胜招数就没必要继续搜索了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表