- The Game of 31
-
Problem Description
The game of 31 was a favourite of con artists who rode the railroads in days of yore. The game is played with a deck of 24 cards: four labelled each of 1, 2, 3, 4, 5, 6. The cards in the deck are visible to both players, who alternately withdraw one card from the deck and place it on a pile. The object of the game is to be the last player to lay a card such that the sum of the cards in the pile does not exceed 31. Your task is to determine the eventual winner of a partially played game, assuming each player plays the remainder of the game using a perfect strategy.
For example, in the following game player B wins:
Player A plays 3
Player B plays 5
Player A plays 6
Player B plays 6
Player A plays 5
Player B plays 6Input
The input will consist of several lines; each line consists of a sequence of zero or more digits representing a partially completed game. The first digit is player A's move; the second player B's move; and so on. You are to complete the game using a perfect strategy for both players and to determine who wins.Output
For each game, print a line consisting of the input, followed by a space, followed by A or B to indicate the eventual winner of the game.Sample Input
356656
35665
3566
111126666
552525Sample Output
356656 B
35665 B
3566 A
111126666 A
552525 A
#include<iostream>
#include<cstdlib>
#include<stdio.h>
#include<string.h>
#include<string.h>
#include<memory.h>
using namespace std;
int sg[50];
int num[7];
bool dfs(int sum)
{
if(sum>=31) return 0;
for(int i=1;i<=6;i++)
{
if(num[i]&&sum+i<=31)
{
--num[i];
if(dfs(sum+i)==0) {++num[i];return 1;}
++num[i];//回溯
}
}
return 0;
}
int main()
{
char str[35];
while(scanf("%s",str)!=EOF)
{
int sum=0;
int l=strlen(str);
for(int i=1;i<=6;i++)
num[i]=4;
for(int i=0;i<l;i++)
{
sum+=str[i]-'0';
num[str[i]-'0']--;
}
if(sum>=31)
{
printf("%s ",str);
if(l&1) puts("A");
else puts("B");
continue;
}
printf("%s ",str);
if(dfs(sum))
{
if(l&1) puts("B");
else puts("A");
}
else
{
if(l&1) puts("A");
else puts("B");
}
}
}
/*
356656
35665
3566
111126666
552525
*/
- 其他相关推荐
- The Game of 31
- The game of 31 was a favorite of con artists who rode the railroads in days of yore. The game is played with a deck of 24 cards: four labeled each of 1, 2, 3, 4, 5, 6. The cards in the deck are visible to both players, who alternately withdraw one card from the deck and place it on a pile. The object of the game is to be the last player to lay a card such that the sum of the cards in the pile does not exceed 31. Your task is to determine the eventual winner of a partially played game, assuming each player plays the remainder of the game using a perfect stratefy.nFor example, in the following game player B wins:nnPlayer A plays 3nPlayer B plays 5nPlayer A plays 6nPlayer B plays 6nPlayer A plays 5nPlayer B plays 6nnnInputnnThe input will consist of several lines; each line consists of a sequence of zero or more digits representing a partially completed game. The first digit is player A's move; the second player B's move; and so on. You are to complete the game using a perfect strategy for both players and to determine who wins.nnnOutputnnFor each game, print a line consisting of the input, followed by a space, followed by A or B to indicate the eventual winner of the game.nnnSample Inputnn356656n35665n3566n111126666n552525nnnSample Outputnn356656 Bn35665 Bn3566 An111126666 An552525 An
- 生命游戏(c语言)
- Conway——the game of life,特别发行的精装版,以此来纪念一年来学习编程的蛋疼时光!
- android 最新31个游戏合集 game 31个_2.rar
- android 最新31个游戏合集 game 31.rar 第二个包
- android 最新31个游戏合集 game 31个_3.rar
- 第三个包,android 游戏 game G3 G2之前已经描述了
- 3D游戏引擎编程_part5of31
- 3D Game Engine Programming 3D游戏引擎编程 pdf 一共31个包,下齐后解压.
- game game game game game
- 很好玩的小游戏,适合上班时间消遣!有空下了玩玩吧!
- Game AI Pro 3 Collected Wisdom of Game AI Professionals 无水印pdf
- Game AI Pro 3 Collected Wisdom of Game AI Professionals 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
- Game Engine Architecture 3rd Edition
- 游戏引擎开发第三版高清版 New to the Third Edition This third edition offers the same comprehensive coverage of game engine architecture provided by previous editions, along with updated coverage of: computer and CPU hardware and memory caches, compiler optimizations, C++ language standardization, the IEEE-754 floating-point representation, 2D user interfaces, plus an entirely new chapter on hardware parallelism and concurrent programming.
- 3D游戏引擎编程_part3of31
- 3D Game Engine Programming<br>3D游戏引擎编程<br>pdf<br>一共31个包,下齐后解压.
- 3D游戏引擎编程part10of31
- 3D Game Engine Programming<br>3D游戏引擎编程<br>pdf<br>一共31个包,下齐后解压.
- 3D游戏引擎编程part25of31
- 3D Game Engine Programming<br>3D游戏引擎编程<br>pdf<br>一共31个包,下齐后解压.
- 3D游戏引擎编程part24of31
- 3D Game Engine Programming<br>3D游戏引擎编程<br>pdf<br>一共31个包,下齐后解压.
- 3D游戏引擎编程_part19of31
- 3D Game Engine Programming<br>3D游戏引擎编程<br>pdf<br>一共31个包,下齐后解压.
- 3D游戏引擎编程_part6of31
- 3D Game Engine Programming<br>3D游戏引擎编程<br>pdf<br>一共31个包,下齐后解压.
- 3D游戏引擎编程part14of31
- 3D Game Engine Programming<br>3D游戏引擎编程<br>pdf<br>一共31个包,下齐后解压.
- 3D游戏引擎编程part20of31
- 3D Game Engine Programming<br>3D游戏引擎编程<br>pdf<br>一共31个包,下齐后解压.
- 3D游戏引擎编程part31of31
- 3D Game Engine Programming<br>3D游戏引擎编程<br>pdf<br>一共31个包,下齐后解压.
- 3D游戏引擎编程part28of31
- 3D Game Engine Programming<br>3D游戏引擎编程<br>pdf<br>一共31个包,下齐后解压.
- 3D游戏引擎编程_part17of31
- 3D Game Engine Programming<br>3D游戏引擎编程<br>pdf<br>一共31个包,下齐后解压.
- 3D游戏引擎编程_part8of31
- 3D Game Engine Programming<br>3D游戏引擎编程<br>pdf<br>一共31个包,下齐后解压
- 3D游戏引擎编程_part9of31
- 3D Game Engine Programming<br>3D游戏引擎编程<br>pdf<br>一共31个包,下齐后解压.
- facebook instant game 发布指南-最佳实践
- 小游戏最佳实践构建小游戏时,要求使用 HTML5 语言。下面是关于开发小游戏的一系列最佳实践:加载时间与性能 初始加载时间不应超过 5 秒:小游戏必须能够“即时”加载,如果初始加载时间太长,玩家将趋向于流失。捆绑包的总大小最多可为 200 MB,但在初始加载期间,我们仅加载您的index.html明确要求的文件。因此,请确保在初始加载期间仅加载关键素材,用于开启首次会话,并延迟其他素材的加载,...
- 3D游戏引擎编程part22of31
- 3D Game Engine Programming<br>3D游戏引擎编程<br>pdf<br>一共31个包,下齐后解压.
- 3D游戏引擎编程part30of31
- 3D Game Engine Programming<br>3D游戏引擎编程<br>pdf<br>一共31个包,下齐后解压.
- 3D游戏引擎编程part15of31
- 3D Game Engine Programming<br>3D游戏引擎编程<br>pdf<br>一共31个包,下齐后解压.
- 3D游戏引擎编程_part7of31
- 3D Game Engine Programming<br>3D游戏引擎编程<br>pdf<br>一共31个包,下齐后解压.
- 3D游戏引擎编程part29of31
- 3D Game Engine Programming<br>3D游戏引擎编程<br>pdf<br>一共31个包,下齐后解压.
- 3D游戏引擎编程part11of31
- 3D Game Engine Programming<br>3D游戏引擎编程<br>pdf<br>一共31个包,下齐后解压.
- Game For Windows Live
- 用以解决某些游戏(如GTA4)缺少xlive.dll的问题,彻底解决此问题需要安装Game For Windows Live。
- Steven Tadelis - Game Theory. An Introduction
- Steven Tadelis - Game Theory. An Introduction [2013][A] pdf 英文
- Game Mechanics Advanced Game Design+machinations工具
- 国外的游戏开发书籍与相应工具,有助于游戏开发
- Game of Life问题及解法
- 289. Game of Life LeetCode
- The.Art.of.Game.Design.A.Book.of.Lenses.2nd.Edition
- The.Art.of.Game.Design最新第2版,游戏设计必备书籍!
- 3D游戏引擎编程_part4of31
- 3D Game Engine Programming<br>3D游戏引擎编程<br>pdf<br>一共31个包,下齐后解压.
- 3D游戏引擎编程part26of31
- 3D Game Engine Programming<br>3D游戏引擎编程<br>pdf<br>一共31个包,下齐后解压.
- 3D游戏引擎编程part12of31
- 3D Game Engine Programming<br>3D游戏引擎编程<br>pdf<br>一共31个包,下齐后解压.
- 3D游戏引擎编程part27of31
- 3D Game Engine Programming<br>3D游戏引擎编程<br>pdf<br>一共31个包,下齐后解压.
- 3D游戏引擎编程_part16of31
- 3D Game Engine Programming<br>3D游戏引擎编程<br>pdf<br>一共31个包,下齐后解压.
- 3D游戏引擎编程_part18of31
- 3D Game Engine Programming<br>3D游戏引擎编程<br>pdf<br>一共31个包,下齐后解压.
- 3D游戏引擎编程part23of31
- 3D Game Engine Programming<br>3D游戏引擎编程<br>pdf<br>一共31个包,下齐后解压.