UVa11459 - Snakes and Ladders(飞行棋类的游戏)

Problem D: Snakes and Ladders

Snakes and Ladders is a board game played on a 10 by 10 grid. The squaresof the grid are numbered 1 to 100. Each player has a token which is placedon the square numbered 1 at the beginning of the game. Players take turnsrolling a die which provides a random number between 1 and 6.After rolling, the player advances his or her token the numberof squares shown on the die. If this would put the token pastthe square numbered 100, the token is advanced to 100.After advancing, if the token ison a square containing the bottom of a ladder, the token mustbe moved to the square containing the top of the ladder.Similarly, if the token is on a square containing the mouth of a snake,the token must be moved to the square containing the tail of the snake.No square contains more than one endpoint of any snake or ladder.The square numbered 100 does not contain the mouth of a snake orthe bottom of a ladder. A player wins when his or her token reached thesquare numbered 100. At that point, the game ends.

Given the configuration of the snakes and ladders on a game board anda sequence of die rolls, you are to determine the positions of all thetokens on the game board. The sequence of die rolls need not be complete(i.e. it need not lead to any player winning). The sequence of die rollsmay also continue after the game is over; in this case, the die rolls afterthe end of the game should be ignored.

Input Specification

The first line is the number of test cases to follow. The test cases follow,one after another; the format of each test case is the following:

The first line contains three positive integers:the number a of players,the number b of snakes or ladders, andthe number c of die rolls.There will be no more than 1000000 playersand no more than 1000000 die rolls.Each of the next b lines contains twointegers specifying a snake or ladder. The first integer indicates thesquare containing the mouth of the snake or the bottom of the ladder.The second integer indicates the square containing the tail of the snakeor the top of the ladder. The following c lines each contain one integergiving number rolled on the die.

Sample Input

1
3 1 3
4 20
3
4
5

Output Specification

For each player, output a single line containing a string of theform Position of player N is P., where Nis replaced with the number of the player and Pis replaced with the final position of the player.

Output for Sample Input

Position of player 1 is 20.
Position of player 2 is 5.
Position of player 3 is 6.
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.HashMap;
import java.util.Arrays;

public class Main 
{
	public static final boolean DEBUG = false;
	public StreamTokenizer tokenizer;
	public BufferedReader cin;
	public PrintWriter cout;
	public int[] players, rolls;
	public int numOfPlayers, numOfRolls, numOfSnake;
	public HashMap<Integer, Integer> ladderOrSnake;
	
	public void init() 
	{
		try {
			if (DEBUG) {
				cin = new BufferedReader(new InputStreamReader(
						new FileInputStream("d:\\OJ\\uva_in.txt")));
			} else {
				cin = new BufferedReader(new InputStreamReader(System.in));
			}
			cout = new PrintWriter(new OutputStreamWriter(System.out));
			tokenizer = new StreamTokenizer(cin);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String next() 
	{
		try {

			tokenizer.nextToken();
			if (tokenizer.ttype == StreamTokenizer.TT_EOF)
				return null;
			else if (tokenizer.ttype == StreamTokenizer.TT_WORD)
				return tokenizer.sval;
			else if (tokenizer.ttype == StreamTokenizer.TT_NUMBER)
				return String.valueOf((int) tokenizer.nval);
			else
				return null;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public boolean input() 
	{
		numOfPlayers = Integer.parseInt(next());
		numOfSnake = Integer.parseInt(next());
		numOfRolls = Integer.parseInt(next());
		ladderOrSnake = new HashMap<Integer, Integer>();
		
		players = new int[numOfPlayers];
		Arrays.fill(players, 1);
		
		for (int i = 0; i < numOfSnake; i++) {
			int a = Integer.parseInt(next());
			int b = Integer.parseInt(next());
			ladderOrSnake.put(a, b);
		}
		
		rolls = new int[numOfRolls];
		for (int i = 0; i < numOfRolls; i++) {
			rolls[i] = Integer.parseInt(next());
		}
		return true;
	}

	public void solve() 
	{
		for (int i = 0; i < numOfRolls; i++) {
			int curPlayer = i % numOfPlayers;
			int newPos = players[curPlayer] + rolls[i];
			if (ladderOrSnake.containsKey(newPos)) {
				newPos = ladderOrSnake.get(newPos);
			}
			players[curPlayer] = newPos;
			if (newPos == 100) break;
		}
		
		for (int i = 0; i < numOfPlayers; i++) {
			cout.println("Position of player " + (i + 1) + " is " + players[i] + ".");
		}
		cout.flush();
	}

	public static void main(String[] args) 
	{
		Main solver = new Main();
		solver.init();

		int t = Integer.parseInt(solver.next());
		while (t-- > 0) {
			solver.input();
			solver.solve();
		}
	}
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值