博弈 HDOJ 4371 Alice and Bob

2023-05-23,,

题目传送门

题意:Alice和 Bob轮流写数字,假设第 i 次的数字是S[i] ,那么第 i+1 次的数字 S[i+1] = S[i] + d[k] 或 S[i] - d[k],条件是 S[i+1] <= n && S[i-1]<S[i+1]

分析:设d[]最小的数字为mn,除此之外设为d,第一次A写了0,第二次B如果写了d,那么A可以写d - mn,确保自己有数直到胜利;如果B第一次写了mn,那么以后的数都只能加mn直到>n,这个很好判断谁胜利。

收获:博弈题想到了就简单了

代码:

#include <cstdio>
#include <algorithm>
using namespace std; const int M = 1e2 + 10;
int a[M]; int main(void) {
int T, cas = 0;
int n, m;
scanf ("%d", &T);
while (T--) {
scanf ("%d%d", &n, &m);
for (int i=1; i<=m; ++i) scanf ("%d", &a[i]);
printf ("Case #%d: ", ++cas);
sort (a+1, a+1+m);
if ((n / a[1]) & 1) puts ("Bob");
else puts ("Alice");
} return 0;
}

  

博弈 HDOJ 4371 Alice and Bob的相关教程结束。

《博弈 HDOJ 4371 Alice and Bob.doc》

下载本文的Word格式文档,以方便收藏与打印。