算法来了,和这个类似 https://leetcode.com/problems/cat-and-mouse/solution/ , 主要在于建立状态,和分析neighbors。
- #include <iostream>
- #include <vector>
- #include <array>
- #include <map>
- #include <set>
- using namespace std;
- class Solution {
- vector<set<array<int, 4>>> m; // #piles -> all possibilities
- map<array<int, 4>, int> nei; // state->result, 每个state有3个结果:0平局,1先走者负,2后走者负。
- public:
- // 创建m:key为非0堆(pile)的个数,value为每种情况的位表示,每个堆可以用4 bits表示,每个integer表示一个color,所以有4个integer,组成一个array。
- void gc2g(int i, int r, vector<int>& a) {
- //cout << i << "," << r << "," << a.size() << endl;
- if (i > 11) cout << i << endl;
- if (r == 0 || i == 11) {
- int pre = a[i];
- a[i] = r;
- array<int, 4> aa;
- int nz = 0;
- auto b = a;
- for (int j = 0; j < 4; j++) {
- std::sort(b.begin() + j * 3, b.begin() + j * 3 + 3);
- aa[j] = (b[3 * j] << 8) | (b[3 * j + 1] << 4) | b[3 * j + 2];
- if (b[3 * j]) nz++;
- if (b[3 * j + 1]) nz++;
- if (b[3 * j + 2]) nz++;
- }
- std::sort(aa.begin(), aa.end());
- m[nz].insert(aa);
- a[i] = pre;
- }
- else {
- for (int j = r; j >= 0; j--) {
- int pre = a[i];
- a[i] = j;
- gc2g(i + 1, r - j, a);
- a[i] = pre;
- }
- }
- }
- // 找出每种状态对应的结果nei
- void getnei() {
- for (int i = 1; i <= 12; i++) {
- if (i == 1) {
- for (auto& t : m[1]) nei[t] = 1; // 先走者负
- }
- else {
- for (auto& t : m[i]) { // t is 4-tuple
- vector<int> cnt(3, 0); // 分析上一层neighbors具有的状态
- vector<int> a(12, 0);
- for (int j = 0; j < 4; j++) {
- a[j * 3] = t[j] >> 8;
- a[j * 3 + 1] = (t[j] >> 4) & 15;
- a[j * 3 + 2] = (t[j] & 15);
- }
- for (int j = 0; j < 11; j++) {
- if (a[j] == 0) continue;
- for (int k = j + 1; k < 12; k++) {
- if (a[k] == 0) continue;
- if (j / 3 == k / 3) {
- auto b = a;
- auto key = t;
- int ind = j / 3;
- b[j] += b[k];
- b[k] = 0;
- std::sort(b.begin() + ind * 3, b.begin() + ind * 3 + 3);
- key[ind] = (b[ind * 3] << 8) | (b[ind * 3 + 1] << 4) | b[ind * 3 + 2];
- sort(key.begin(), key.end());
- cnt[nei[key]] = 1;
- }
- else if (a[j] == a[k]) {
- auto b = a;
- auto key = t;
- b[j] += b[k];
- b[k] = 0;
- int ind = j / 3;
- std::sort(b.begin() + ind * 3, b.begin() + ind * 3 + 3);
- key[ind] = (b[ind * 3] << 8) | (b[ind * 3 + 1] << 4) | b[ind * 3 + 2];
- ind = k / 3;
- std::sort(b.begin() + ind * 3, b.begin() + ind * 3 + 3);
- key[ind] = (b[ind * 3] << 8) | (b[ind * 3 + 1] << 4) | b[ind * 3 + 2];
- sort(key.begin(), key.end());
- cnt[nei[key]] = 1;
- b = a;
- key = t;
- b[k] += b[j];
- b[j] = 0;
- ind = k / 3;
- std::sort(b.begin() + ind * 3, b.begin() + ind * 3 + 3);
- key[ind] = (b[ind * 3] << 8) | (b[ind * 3 + 1] << 4) | b[ind * 3 + 2];
- ind = j / 3;
- std::sort(b.begin() + ind * 3, b.begin() + ind * 3 + 3);
- key[ind] = (b[ind * 3] << 8) | (b[ind * 3 + 1] << 4) | b[ind * 3 + 2];
- sort(key.begin(), key.end());
- cnt[nei[key]] = 1;
- }
- }
- }
- if (i == 12) cout << cnt[0] << "," << cnt[1] << "," << cnt[2] << endl;
- int sum = cnt[0] + cnt[1] + cnt[2];
- if (sum == 0) nei[t] = 1; // 不能移动,所以先走者负
- else if (cnt[1] = 1) { // move到上一层对应的这个状态,先走者嬴,后走者负。
- nei[t] = 2; // 后走者负
- }
- else if (cnt[0] == 1) { // 平局
- nei[t] = 0; //平局
- }
- else // cnt[2] = 1, 每个move都导致先走者负
- nei[t] = 1;
- }
- }
- }
- }
- int test() {
- m.assign(13, set<array<int, 4>>()); // sorted within color and sorted between colors
- vector<int> a(12, 0);
- gc2g(0, 12, a); // index, remains, a
- int sum = 0;
- for (auto& t : m) {
- sum += t.size();
- }
- cout << sum << endl;
- cout << m[12].size() << endl;
- cout << m[1].size() << endl;
- getnei();
- int x = (1 << 8) | (1 << 4) | 1;
- array<int, 4> key = { x, x, x, x };
- cout << nei[key] << endl;
- for (auto& t : m[3]) {
- for (int i = 0; i < 4; i++) {
- cout << (t[i] >> 8) << ",";
- cout << ((t[i] >> 4) & 15) << ",";
- cout << (t[i] & 15) << ",";
- }
- cout << "-->" << nei[t] << endl;
- }
- return 0;
- }
- };
- int main(int argc, char* argv[])
- {
- Solution sol;
- sol.test();
- return 0;
- }
复制代码 |