题目链接
题解
题意
两堆石子,人机博弈。
思路
先判断是否有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;
}
}
}
}
}