SRM 606 D2 L3:EllysCandyGame
来源:互联网 发布:淘宝客服工作总结1 编辑:程序博客网 时间:2024/05/19 06:35
题目:http://community.topcoder.com/stat?c=problem_statement&pm=12394
递归,搜索所有情况。
代码:
#include <algorithm>#include <iostream>#include <sstream>#include <string>#include <vector>#include <stack>#include <deque>#include <queue>#include <set>#include <map>#include <cstdio>#include <cstdlib>#include <cctype>#include <cmath>#include <cstring>using namespace std;/*************** Program Begin **********************/class EllysCandyGame {public:int N;vector <int> sweets;int isFirstWin(int w1, int w2){bool ok = false;int res = 1;for (int j = 0; j < N; j++) {if (sweets[j] != 0) {ok = true;if (j - 1 >= 0) {sweets[j-1] = 2 * sweets[j-1];}if (j + 1 < N) {sweets[j+1] = 2 * sweets[j+1];}int take = sweets[j];sweets[j] = 0;int nres = isFirstWin(w2 + take, w1);if (1 == nres) {if (j - 1 >= 0) {sweets[j-1] = sweets[j-1] / 2;}if (j + 1 < N) {sweets[j+1] = sweets[j+1] / 2;}sweets[j] = take;return -1;}if (0 == nres) {res = 0;}if (j - 1 >= 0) {sweets[j-1] = sweets[j-1] / 2;}if (j + 1 < N) {sweets[j+1] = sweets[j+1] / 2;}sweets[j] = take;}}if (!ok) {// 结束if (w1 == w2) {return 0;}if (w1 > w2) {return 1;}if (w1 < w2) {return -1;}}return res;} string getWinner(vector <int> sw) { string res;this->N = sw.size();this->sweets = sw;int r = -1;bool noChoice = true;for (int i = 0; i < N; i++) {if (sweets[i] != 0) {// 起始选择noChoice = false;if (i - 1 >= 0) {sweets[i-1] = 2 * sweets[i-1];}if (i + 1 < N) {sweets[i+1] = 2 * sweets[i+1];}int take = sweets[i];sweets[i] = 0;int rt = isFirstWin(take, 0);if (rt == 1) {r = 1;break;} else if (rt == 0) {r = 0;}if (i - 1 >= 0) {sweets[i-1] = sweets[i-1] / 2;}if (i + 1 < N) {sweets[i+1] = sweets[i+1] / 2 ;}sweets[i] = take;}}if (noChoice) {return "Draw";}if (r == 0) {return "Draw";}if (r == 1) {return "Elly";}if (r == -1) {return "Kris";}return res; }};/************** Program End ************************/
0 0
- SRM 606 D2 L3:EllysCandyGame
- SRM 576 D2 L3:CharacterBoard2
- SRM 603 D2 L3:GraphWalkWithProbabilities
- SRM 606 DIV2 1000 EllysCandyGame
- SRM 581 D2 L3:TreeUnionDiv2,Floyd算法
- SRM 588 D2 L3:GameInDarknessDiv2,DFS
- SRM 605 D2 L3: AlienAndSetDiv2,DP
- SRM 608 D2 L3:BigOEasy,DFS
- SRM 607 D2 L3:CombinationLockDiv2,DP
- SRM 595 D2 L3:LittleElephantAndXor, dp
- SRM 596 D2 L3:SparseFactorialDiv2,math
- SRM 601 D2 L3:WinterAndReindeers,dp
- SRM 606 D2 L2:EllysNumberGuessing
- SRM 605 D2 L2:AlienAndGame
- SRM 608 D2 L2:MysticAndCandiesEasy
- SRM 597 D2 L2:LittleElephantAndString
- SRM 581 D2 L2:SurveillanceSystem,重叠度
- SRM 607 L2 D2:PalindromicSubstringsDiv2,DP
- struts2原理
- Struts2工作原理
- SRM 606 D2 L2:EllysNumberGuessing
- struts2原理图
- 定制Sublime - 主题/快捷键
- SRM 606 D2 L3:EllysCandyGame
- 关于给10期验收软工文档
- 【C语言】关于scanf语句的记录
- lubunt 自动挂载TF卡作为数据卡
- db_recovery_file_dest参数
- POJ 3322 Bloxorz I 三维BFS
- 约瑟夫问题
- C++连接mysql数据库的两种方法
- 关于Python的模块