Algorithm Practice for 1576

Colorville
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 283 Accepted: 162

Description

A simple matching game for children uses a board that is a sequence of colored squares. Each player has a game piece. Players alternate turns, drawing cards containing either one colored square or two colored squares of the same color. Players move their pieces forward on the board to the next square that matches the single color on the card, or forward to the second matching square if the card contains two colored squares, or forward to the last square on the board if there is no square matching the description above. A player wins if his piece lands on the last square of the board. It is possible for all the cards to be drawn and still not have a winner. 

This problem represents colors with capital letters from A-Z. Below is a diagram of a sample board. 

Consider the deck of cards: R, B, GG, Y, P, B, P, RR 

For 3 players, the game proceeds as follows: 

Player 1 draws R, moves to 1st square 
Player 2 draws B, moves to 5th square 
Player 3 draws GG, moves to 8th square 
Player 1 draws Y, moves to 2nd square 
Player 2 draws P, moves to 11th square 
Player 3 draws B, moves to 9th square 
Player 1 draws P, moves to 4th square 
Player 2 draws RR, Wins! (no Rs in front of piece so it goes to last square) 

Using the same board and the same deck of cards, but with 2 players, Player 1 wins after 7 cards. With 4 players, no one wins after exhausting the deck of 8 cards.

Input

Input consists of information for one or more games. Each game starts with one line containing the number of players (1-4), the number of squares on the board (1-79), and the number of cards in the deck (1-200). This is followed by a single line of characters representing the colored squares on the board. Following this are the cards in the deck, one card per line. Cards can have only a single character, or two of the same character. The end of the input is signalled by a line with 0 for the number of players - the other two values will be present but indeterminate.

Output

For each game, the output is either the winning player and the total number of cards drawn, or the number of cards in the deck, as shown in the sample output. Always use the plural "cards".

Sample Input

2 13 8
RYGPBRYGBRPOP
R
B
GG
Y
P
B
P
RR
2 6 5
RYGRYB
R
YY
G
G
B
3 9 6
QQQQQQQQQ
Q
QQ
Q
Q
QQ
Q
0 6 0

Sample Output

Player 1 won after 7 cards.
Player 2 won after 4 cards.
No player won after 6 cards.

Solution

package id1576;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;

public class Colorville {
	char[] board = null;
	BufferedReader in = null;
	String[] strArry = null;
	int playerNum, boardSize, cardNum;
	
	public Colorville() throws IOException{
		in = new BufferedReader(new InputStreamReader(System.in));
		strArry = in.readLine().split(" ");
		while(!strArry[0].equals("0")){
			startGame(strArry);
			strArry = in.readLine().split(" ");
		}
	}
	
	public void startGame(String[] strArry) throws IOException{
		int playerNum, boardSize, cardNum;
		int result = 0;
		String board = null;
		Player[] player = null;
		playerNum = Integer.parseInt(strArry[0]);
		boardSize = Integer.parseInt(strArry[1]);
		cardNum = Integer.parseInt(strArry[2]);
		player = new Player[playerNum];
		for(int i = 0; i < playerNum; i++)
			player[i] = new Player();
		board = in.readLine();
		for(int i = 0; i < cardNum; i++)
			player[i % playerNum].push(in.readLine());
		for(int i = 0; i < cardNum; i++){
			result = player[i % playerNum].isWiner(board);
			if(result == 1) {
				System.out.println("Player " + (1 + (i % playerNum)) + " won after " + (i + 1) + " cards");
				return;
			} else if(result == 0){
				continue;
			} else if(result == -1){
				System.out.println("No player won after " + i + " cards");
				return;
			} 
		}
		System.out.println("No player won after " + cardNum + " cards");
		return;
	}

	
	public static void main(String[] args) {
		try {
			new Colorville();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private class Player{
		LinkedList<String> cards = null;
		int pointer = 0;
		public Player(){
			cards = new LinkedList<String>();
		}
		
		public void push(String str){
			cards.offer(str);
		}
		public String pop(){
			return cards.poll();
		}
		public boolean containCard(){
			return !cards.isEmpty();
		}
		
		// return 1 if player win, 0 if not, -1 if no one win.
		public int isWiner(String board){
			String subBoard = null;
			String cardStr = null;
			int subPointer = 0;
			if (!this.containCard())
				return -1;
			cardStr = this.pop();
			if(cardStr.length() == 1){
				subBoard = board.substring(pointer);
				subPointer = subBoard.indexOf(cardStr);
				pointer += subPointer + 1;
				if(pointer == board.length() || subPointer == -1)
					return 1;
			} else{ 
				String temp = null;
				subBoard= board.substring(pointer);
				temp = subBoard;
				subPointer = subBoard.indexOf(cardStr.charAt(0));
				if(subPointer == -1) return 1;
				pointer += subPointer+ 1;
				subBoard = board.substring(pointer);
				subPointer = subBoard.indexOf(cardStr.charAt(0));
				pointer +=subPointer  + 1;

				if(pointer == board.length() || subPointer == -1)
					return 1;
			}
			return 0;
		}
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值