Buttercola: LinkedIn: Can I Win
In "the 100 game," two players take turns adding, to a running
total, any integer from 1..10. The player who first causes the running
total to reach or exceed 100 wins.
What if we change the game so that players cannot re-use integers?
For example, if two players might take turns drawing from a common pool of numbers
of 1..15 without replacement until they reach a total >= 100. This problem is
to write a program that determines which player would win with ideal play.
Write a procedure, "Boolean canIWin(int maxChoosableInteger, int desiredTotal)",
which returns true if the first player to move can force a win with optimal play.
Your priority should be programmer efficiency; don't focus on minimizing
either space or time complexity.
Solution:
The crux of the problem is if the largest number remaining is greater than the target remaining, then the player is sure to win.
http://blog.csdn.net/craiglin1992/article/details/44929861
//The catch is: when the largest number remaining is greater than the target remaining, the player is sure to win //helper returns true if the CURRENT player can win public boolean canIWin(int max, int target) { List<Integer> candidates = new ArrayList<Integer>(); for (int i = 1; i <= max; i++){ candidates.add(i); } return helper(candidates, target); } public boolean helper(List<Integer> candidates, int target){ if (candidates.get(candidates.size()-1) >= target){ return true; } for (int i = 0; i < candidates.size(); i++){ int removed = candidates.remove(i); if (!helper(candidates, target-removed)){ candidates.add(i, removed); return true; } candidates.add(i, removed); } return false; }
http://codeanytime.blogspot.com/2015/01/caniwin.html
如果可以重复选数字,那么如果 target % (maxChoosable + 1) == 0 那么玩家2必胜 ,如果不是0那么玩家1第一步先拿到target % (maxChoosable + 1),然后对应玩家2拿 的数字来拿 maxChoosable + 1 - player2Num,最后就刚好抢到target。
http://www.mitbbs.com/article_t/JobHunting/32861097.html
我理解的是,题目要求是running total,也就是两个人取数字的和超过一个数。
假设池子里还有三个数,1,2,3。要求达到的目标为5,两个玩家。
现在轮到玩家1取数字,如果贪心的取最大数字,则取3,total=3,那么玩家2随后取2
就能胜利。
如果玩家1不取3而是取1,total=1,玩家2无论从剩下的2和3里面取哪个数,他都必输。
http://www.careercup.com/question?id=5116481574535168
http://shirleyisnotageek.blogspot.com/2014/12/the-100-game.html
Read full article from Buttercola: LinkedIn: Can I Win
In "the 100 game," two players take turns adding, to a running
total, any integer from 1..10. The player who first causes the running
total to reach or exceed 100 wins.
What if we change the game so that players cannot re-use integers?
For example, if two players might take turns drawing from a common pool of numbers
of 1..15 without replacement until they reach a total >= 100. This problem is
to write a program that determines which player would win with ideal play.
Write a procedure, "Boolean canIWin(int maxChoosableInteger, int desiredTotal)",
which returns true if the first player to move can force a win with optimal play.
Your priority should be programmer efficiency; don't focus on minimizing
either space or time complexity.
Solution:
The crux of the problem is if the largest number remaining is greater than the target remaining, then the player is sure to win.
Boolean canIWin(
int
maxChoosableInteger,
int
desiredTotal) {
if
(maxChoosableInteger <=
0
|| desiredTotal <=
0
) {
return
false
;
}
List<Integer> pool =
new
ArrayList<>();
for
(
int
i =
1
; i <= maxChoosableInteger; i++) {
pool.add(i);
}
return
canIWinHelper(pool, desiredTotal);
}
private
boolean
canIWinHelper(List<Integer> pool,
int
desiredTotal) {
if
(pool.get(pool.size() -
1
) >= desiredTotal) {
return
true
;
}
for
(
int
i =
0
; i < pool.size(); i++) {
int
removed = pool.remove(i);
boolean
win = !canIWinHelper(pool, desiredTotal - removed);
pool.add(i, removed);
if
(win) {
return
true
;
}
}
return
false
;
}
//The catch is: when the largest number remaining is greater than the target remaining, the player is sure to win //helper returns true if the CURRENT player can win public boolean canIWin(int max, int target) { List<Integer> candidates = new ArrayList<Integer>(); for (int i = 1; i <= max; i++){ candidates.add(i); } return helper(candidates, target); } public boolean helper(List<Integer> candidates, int target){ if (candidates.get(candidates.size()-1) >= target){ return true; } for (int i = 0; i < candidates.size(); i++){ int removed = candidates.remove(i); if (!helper(candidates, target-removed)){ candidates.add(i, removed); return true; } candidates.add(i, removed); } return false; }
http://codeanytime.blogspot.com/2015/01/caniwin.html
如果可以重复选数字,那么如果 target % (maxChoosable + 1) == 0 那么玩家2必胜 ,如果不是0那么玩家1第一步先拿到target % (maxChoosable + 1),然后对应玩家2拿 的数字来拿 maxChoosable + 1 - player2Num,最后就刚好抢到target。
http://www.mitbbs.com/article_t/JobHunting/32861097.html
我理解的是,题目要求是running total,也就是两个人取数字的和超过一个数。
假设池子里还有三个数,1,2,3。要求达到的目标为5,两个玩家。
现在轮到玩家1取数字,如果贪心的取最大数字,则取3,total=3,那么玩家2随后取2
就能胜利。
如果玩家1不取3而是取1,total=1,玩家2无论从剩下的2和3里面取哪个数,他都必输。
http://www.careercup.com/question?id=5116481574535168
http://shirleyisnotageek.blogspot.com/2014/12/the-100-game.html
Read full article from Buttercola: LinkedIn: Can I Win