GYM MaratonIME plays Nim 交互博弈

题目链接

GYM101744

题解

题意

两堆石子,人机博弈。

思路

先判断是否有0,有的话直接把另一个取完。
没有的话取多的那部分,让两堆石子数量相同,之后对称博弈就好。

AC代码

#include <bits/stdc++.h>

using namespace std;

int main() {
    int x, y;
    cin >> x >> y;
    if (x == 0 || y == 0) {
        if (x == 0) {
            cout << 2 << ' ' << y  << '\n';
        } else if (y == 0) {
            cout << 1 << ' ' << x  << '\n';
        }
    } else {
        if (x > y) {
            cout << 1 << ' ' << x - y << '\n';
        } else {
            cout << 2 << ' ' << y - x << '\n';
        }
    }
    cout.flush();
    int a, b;
    while (cin >> a >> b) {
        cout << (a == 1 ? 2 : 1) << ' ' << b << '\n';
        cout.flush();
    }
    return 0;
}

后记

先来一句,我是憨批。
第一发wa的代码长这样:

#include <bits/stdc++.h>

using namespace std;

int main() {
    int x, y;
    cin >> x >> y;
    if (x == 0 || y == 0) {
        if (x == 0) {
            cout << 2 << ' ' << y - 1 << '\n';
        } else if (y == 0) {
            cout << 1 << ' ' << x - 1 << '\n';
        }
    } else {
        if (x > y) {
            cout << 1 << ' ' << x - y << '\n';
        } else {
            cout << 2 << ' ' << y - x << '\n';
        }
    }
    cout.flush();
    int a, b;
    while (cin >> a >> b) {
        cout << (a == 1 ? 2 : 1) << ' ' << b << '\n';
        cout.flush();
    }
    return 0;
}

脑子一抽就想成了对面取完我没得取就赢,然后憨批得加了个-1,之后就开始放飞自我,最后一发wa掉的代码太长了我放在了最后来记录一下憨批时刻(差一点就就没准能大概ak了QAQ

最后,题目里真的有说机器人每次都会选最优策略吗???

#include <bits/stdc++.h>

using namespace std;

int main() {
    int x, y;
    cin >> x >> y;
    if (x == 0 || y == 0) {
        if (x == 0) {
            if (y == 1) {
                cout << 2 << ' ' << 1 << '\n';
                cout.flush();
                return 0;
            }
            cout << 2 << ' ' << y - 2 << '\n';
            cout.flush();
            cin >> x >> y;
            cout << x << ' ' << y << '\n';
            cout.flush();
        } else if (y == 0) {
            if (x == 1) {
                cout << 1 << ' ' << 1 << '\n';
                cout.flush();
                return 0;
            }
            cout << 1 << ' ' << x - 2 << '\n';
            cout.flush();
            cin >> x >> y;
            cout << x << ' ' << y << '\n';
            cout.flush();
        }
    } else {
        if (x == 1 || y == 1) {
            if (x == 1) {
                cout << 2 << ' ' << y - 1 << '\n';
                cout.flush();
                cin >> x >> y;
                cout << (x == 1 ? 2 : 1) << ' ' << 1 << '\n';
                cout.flush();
            } else if (y == 1) {
                cout << 1 << ' ' << x - 1 << '\n';
                cout.flush();
                cin >> x >> y;
                cout << (x == 1 ? 2 : 1) << ' ' << 1 << '\n';
                cout.flush();
            }
            return 0;
        } else {
            int xx = x, yy = y;
            if (x == 2 || y == 2) {
                if (x == 2) {
                    cout << 2 << ' ' << 1 << '\n';
                    cout.flush();
                    yy -= 1;
                } else if (y == 2) {
                    cout << 1 << ' ' << 1 << '\n';
                    cout.flush();
                    xx -= 1;
                }
            } else {
                cout << 1 << ' ' << 1 << '\n';
                cout.flush();
                xx -= 1;
            }
            while (cin >> x >> y) {
                if (x == 1) xx -= y;
                else yy -= y;
                if (xx == 1) {
                    cout << 2 << ' ' << yy - 1 << '\n';
                    cout.flush();
                    cin >> x >> y;
                    cout << (x == 1 ? 2 : 1) << ' ' << 1 << '\n';
                    cout.flush();
                    return 0;
                } else if (yy == 1) {
                    cout << 1 << ' ' << xx - 1 << '\n';
                    cout.flush();
                    cin >> x >> y;
                    cout << (x == 1 ? 2 : 1) << ' ' << 1 << '\n';
                    cout.flush();
                    return 0;
                } else if (x == 2 || y == 2) {
                    if (x == 2) {
                        cout << 2 << ' ' << 1 << '\n';
                        cout.flush();
                        yy -= 1;
                    } else if (y == 2) {
                        cout << 1 << ' ' << 1 << '\n';
                        cout.flush();
                        xx -= 1;
                    }
                } else {
                    cout << 1 << ' ' << 1 << '\n';
                    cout.flush();
                    xx -= 1;
                }
            }
        }
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值