Description
LCR is a simple game for three or more players. Each player starts with three chips and the object is to be the last person to have any chips. Starting with Player 1, each person rolls a set of three dice. Each die has six faces, one face with an L, one with a C, one with an R and three with a dot. For each L rolled, the player must pass a chip to the player on their left (Player 2 is considered to be to the left of Player 1); for each R rolled, the player passes a chip to the player on their right; and for each C rolled, the player puts a chip in a central pile which belongs to no player. No action is taken for any dot that is rolled. Play continues until only one player has any chips left. In addition, the following rules apply:
1. A player with no chips is not out of the game, since they may later gain chips based on other players‘ rolls.
2. A player with only 1 or 2 chips left only rolls 1 or 2 dice, respectively. A player with no chips left does not roll but just passes the dice to the next player.
Your job is to simulate this game given a sequence of dice rolls.
Input
Input will consist of multiple test cases. Each test case will consist of one line containing an integer n (indicating the number of players in the game) and a string (specifying the dice rolls). There will be at most 10 players in any game, and the string will consist only of the characters `L‘, `C‘, `R‘ and `.‘. In some test cases, there may be more dice rolls than are needed (i.e., some player wins the game before you use all the dice rolls). If there are not enough dice rolls left to complete a turn (for example, only two dice rolls are left for a player with 3 or more chips) then those dice rolls should be ignored. A value of n = 0 will indicate end of input.
Output
For each test case, output the phrase `Game i :‘ on a single line (where i is the case number starting at 1) followed by a description of the state of the game. This desciption will consist of n + 1 lines of the form
Player 1:c1
Player 2:c2
...
Player n :cn
Center:ct
where c1, c2...cn are the number of chips each player has at the time the simulation ended (either because some player has won or there are no more remaining dice rolls) and ct is the number of chips in the center pile. In addition, if some player has won, you should append the string `(W)‘ after their chip count; otherwise you should append the string `(*)‘ after the chip count of the player who is the next to roll. The only blank on any line should come before the game number or the player number. Use a single blank line to separate test cases.
Sample Input
3 LR.CCR.L.RLLLCLR.LL..R...CLR.
5 RL....C.L
0
Sample Output
Game 1:
Player 1:0
Player 2:0
Player 3:6(W)
Center:3
Game 2:
Player 1:1
Player 2:4
Player 3:1
Player 4:4(*)
Player 5:4
Center:1
Source
2008 East Central Regional Contest
题意:有n(小于等于10)个人,每个人3个筹码,编号为n,n-1,n-2,……,3,2,1 有3个筛子,六个面分别为L、R、C和三个点(.) ,从1这个人开始掷3个筛子,若为L,表示给左边人一个筹码,R为给右边一个筹码,C表示放一个筹码在中间,点表示不进行操作。若这个人筹码少于3个,则掷相应筹码数的筛子,例如这个人没有筹码了,他就不掷筛子,直接给下一个人。给一个筛子序列,模拟过程,序列长度可能在分出输赢时还有剩余,也可能还没分出输赢,长度不够。若过程中剩余筛子数不够这个人掷(小于这个人的筹码数),则剩余筛子忽略不计。
思路:本题就是模拟,没有什么算法,有两点需要注意的地方(我开始时没有注意到这两点,代码wa了很多次,终于找出它bug了!)
1、注意这样的数据 2 CC.RRC 第二个人掷完后没有筛子序列了,但是1号已经赢了。
2、筛子序列遍历完了但是还没有胜负,i号掷完后不一定改i+1掷,因为i+1可能没有筹码了,所以(*)不一定在i+1号后。
#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> using namespace std; char str[1000005]; int flag,tp,center; int p[15]; bool check(int n) { for(int k=1;k<=n;k++) { if(p[k]+center==3*n) { tp=k; flag=1; return true; } } return false; } void moli(int n) { int len=strlen(str); for(int i=0;i<len;) { if(check(n)) return ; int t; if(p[tp]>=3) t=3; else t=p[tp]; if(len-i<t) { return; } for(int j=0;j<t;j++) { if(str[i]==‘L‘) { p[tp]--; if(tp==n) p[1]++; else p[tp+1]++; } else if(str[i]==‘R‘) { p[tp]--; if(tp==1) p[n]++; else p[tp-1]++; } else if(str[i]==‘C‘) { p[tp]--; center++; } i++; } if(check(n)) return ; tp++; if(tp==n+1) tp=1; } while(!p[tp]) { if(tp==n) tp=1; else tp++; } } int main() { int n,Case=1; while(scanf("%d",&n)!=EOF&&n) { scanf("%s",str); flag=0; tp=1; center=0; for(int i=0;i<15;i++) p[i]=3; moli(n); if(Case>1) printf("\n"); printf("Game %d:\n",Case++); for(int i=1;i<=n;i++) { printf("Player %d:%d",i,p[i]); if(tp==i&&flag) printf("(W)\n"); else if(tp==i&&!flag) printf("(*)\n"); else printf("\n"); } printf("Center:%d\n",center); } return 0; }