Codeforces Gym 100379L Game with a string 博弈+交互题
来源:互联网 发布:华为云服务器默认端口 编辑:程序博客网 时间:2024/06/05 15:35
题目大意:
有26堆石子,你可以选其中的若干堆取走每个选定堆中的一颗石子,没法取的人失败。你需要选择先手和后手,然后与程序交互,给出必胜的方案。
做法:
- 我们先看只有一堆的情况,如果这一堆中的石子数为偶数,那么先手必败,反之如果是奇数,那么先手必胜;
- 我们可以推广到26堆的情况,如果不存在奇数堆的石子那么先手必败,否则先手必胜。
- 至此,我们得到了一个最优方案。如果不存在奇数堆的石子那么我们选则后手,否则选先手。
- 每次将奇数堆的石子取走,使得对方面临必败状态即可。
代码:
#include <cstdio>#include <cstdlib>#include <cstring>#include <set>using namespace std;char str[1000];set<int>s[26];int hand=2,oper[1000],os,ct;bool get(){ int p; scanf("%d",&os); if(os==-1)return true; while(os--) { scanf("%d",&p); s[str[p-1]-'a'].erase(p); } return false;}int main(){ scanf("%s",str); int len=strlen(str); for(int i=0;i<len;i++)s[str[i]-'a'].insert(i+1); for(int i=0;i<26;i++) if(s[i].size()%2){hand=1;break;} //存在奇数堆选先手 printf("%d\n",hand); fflush(stdout); if(hand==2)get(); while(1){ int ct=0; for(int i=0;i<26;i++) if(s[i].size()%2) { oper[ct++]=*s[i].begin(); s[i].erase(s[i].begin()); //拿掉奇数堆 } printf("%d",ct); for(int i=0;i<ct;i++)printf(" %d",oper[i]); printf("\n"); fflush(stdout); if(get())break; } return 0;}
0 0
- Codeforces Gym 100379L Game with a string 博弈+交互题
- Codeforces Gym 100379G Wythoff’s game with three piles 博弈
- [Codeforces Gym] 100162A Box Game 博弈+二分图匹配
- Codeforces Gym 100814J Game 简单博弈
- [Codeforces 451A] Game With Sticks (博弈)
- 解题报告:Codeforces Round #432 (Div. 2) E.Arpa and a game with Mojtaba (博弈)
- Codeforces Round #432 C. Arpa and a game with Mojtaba 裸博弈+质数拆解
- codeforces 850C Arpa and a game with Mojtaba(博弈,bitmask)
- Codeforces 778A-String Game
- Codeforces 778A String Game
- CodeForces Gym 100989L Plus or Minus (A)
- Codeforces Round #432 (Div. 2) (Codeforces 850C) E. Arpa and a game with Mojtaba 博弈+状态压缩
- codeforces 258 A Game With Sticks(水题)
- Codeforces 451A Game With Sticks
- Codeforces 451A Game With Sticks(水题)
- Codeforces 451A Game With Sticks
- Codeforces 451 A . Game With Sticks
- codeforces 451A Game With Sticks
- 治愈IT职业生涯的消化系统
- Eclipse external tools以及windows dos“无法访问android.app.Activity 找不到android.app.Activity的类文件”详细解决办法
- Python正则表达式指南
- ZOJ 3778 Talented Chef
- js闭包详解
- Codeforces Gym 100379L Game with a string 博弈+交互题
- Tomcat中常见线程说明
- Android Support v4、v7、v13 介绍
- Android Studio如何发布APK
- CentOS上常用的命令汇总
- Hibernate中left join,right join等用法的详解
- Android数据库升级、降级、创建(onCreate() onUpgrade() onDowngrade())的注意点
- 泡泡堂 DirectX11 Demo 2016.8.14 v1.2更新
- 30个后台管理系统模板