Google Code Jam Notes - Rotate - Java

Problem:

In the exciting game of Join-K, red and blue pieces are dropped into an N-by-N table. The table stands up vertically so that pieces drop down to the bottom-most empty slots in their column. For example, consider the following two configurations:

    - Legal Position -

          .......
          .......
          .......
          ....R..
          ...RB..
          ..BRB..
          .RBBR..
   - Illegal Position -

          .......
          .......
          .......
          .......
   Bad -> ..BR...
          ...R...
          .RBBR..

In these pictures, each '.' represents an empty slot, each 'R' represents a slot filled with a red piece, and each 'B' represents a slot filled with a blue piece. The left configuration is legal, but the right one is not. This is because one of the pieces in the third column (marked with the arrow) has not fallen down to the empty slot below it.

A player wins if they can place at least K pieces of their color in a row, either horizontally, vertically, or diagonally. The four possible orientations are shown below:

      - Four in a row -

     R   RRRR    R   R
     R          R     R
     R         R       R
     R        R         R
 
In the "Legal Position" diagram at the beginning of the problem statement, both players had lined up two pieces in a row, but not three.

As it turns out, you are right now playing a very exciting game of Join-K, and you have a tricky plan to ensure victory! When your opponent is not looking, you are going to rotate the board 90 degrees clockwise onto its side. Gravity will then cause the pieces to fall down into a new position as shown below:

    - Start -

     .......
     .......
     .......
     ...R...
     ...RB..
     ..BRB..
     .RBBR..
   - Rotate -

     .......
     R......
     BB.....
     BRRR...
     RBB....
     .......
     .......
   - Gravity -

     .......
     .......
     .......
     R......
     BB.....
     BRR....
     RBBR...
Unfortunately, you only have time to rotate once before your opponent will notice.

All that remains is picking the right time to make your move. Given a board position, you should determine which player (or players!) will have K pieces in a row after you rotate the board clockwise and gravity takes effect in the new direction.

Notes

  • You can rotate the board only once.
  • Assume that gravity only takes effect after the board has been rotated completely.
  • Only check for winners after gravity has finished taking effect.

Input

The first line of the input gives the number of test cases, TT test cases follow, each beginning with a line containing the integers N and K. The next N lines will each be exactly N characters long, showing the initial position of the board, using the same format as the diagrams above.

The initial position in each test case will be a legal position that can occur during a game of Join-K. In particular, neither player will have already formed K pieces in a row.

Output

For each test case, output one line containing "Case #x: y", where x is the case number (starting from 1), and y is one of "Red", "Blue", "Neither", or "Both". Here, y indicates which player or players will have K pieces in a row after you rotate the board.

Limits

1 ≤ T ≤ 100.
3 ≤ K ≤ N.

Small dataset

3 ≤ N ≤ 7.

Large dataset

3 ≤ N ≤ 50.

Sample


Input 
 

Output 
 
4
7 3
.......
.......
.......
...R...
...BB..
..BRB..
.RRBR..
6 4
......
......
.R...R
.R..BB
.R.RBR
RB.BBB
4 4
R...
BR..
BR..
BR..
3 3
B..
RB.
RB.
Case #1: Neither
Case #2: Both
Case #3: Red
Case #4: Blue


Analysis:
The trick for this problem is that you don't need to rotate the table! Just push all the characters to the right side of the table, it would be fine.

For checking the same characters in a row in each direction, I use an incremental vector approach. Create a horizontal vector{0, 1, 1, -1} and vertical vector{1, 0, 1, 1} to finish the job.

Time complexity: (O(K*n^2)).  

My solution: (Your opinion is highly appreciated)

package codeJam.google.com;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @author Zhenyi 2013 Dec 22, 2013 14:07:42 PM
 */
public class Rotate {
	final static int[] h = { 0, 1, 1, -1 };
	final static int[] v = { 1, 0, 1, 1 };
	static int K, N;

	public static void main(String[] args) throws IOException {
		BufferedReader in = new BufferedReader(new FileReader(
				"C:/Users/Zhenyi/Downloads/A-small-practice.in"));
		FileWriter out = new FileWriter(
				"C:/Users/Zhenyi/Downloads/A-small-practice.out");
		// BufferedReader in = new BufferedReader(new
		// FileReader("C:/Users/Zhenyi/Downloads/A-large-practice.in"));
		// FileWriter out = new
		// FileWriter("C:/Users/Zhenyi/Downloads/A-large-practice.out");

		int T = new Integer(in.readLine());

		for (int cases = 1; cases <= T; cases++) {
			String[] st = new String(in.readLine()).split("\\s");
			N = new Integer(st[0]);
			K = new Integer(st[1]);
			char[][] start = new char[N][N];
			for (int i = 0; i < N; i++) {
				start[i] = new String(in.readLine()).toCharArray();
			}

			for (int row = 0; row < N; row++) {
				int x = N - 1;
				for (int column = N - 1; column >= 0; column--) {
					if (start[row][column] != '.') {
						start[row][x] = start[row][column];
						x--;
					}
				}
				while (x >= 0) {
					start[row][x] = '.';
					x--;
				}
			}

			String result = "";
			boolean isR = false;
			boolean isB = false;
			for (int i = 0; i < N; i++) {
				for (int j = 0; j < N; j++) {
					for (int l = 0; l < 4; l++) {
						if (check(i, j, l, start, 'R')) {
							isR = true;
						}
						if (check(i, j, l, start, 'B'))
							isB = true;
					}
				}
			}
			if (isR && isB)
				result = "Both";
			if (isR && !isB)
				result = "Red";
			if (!isR && isB)
				result = "Blue";
			if (!isR && !isB)
				result = "Neither";
			out.write("Case #" + cases + ": " + result);
			out.write("\n");

		}
		in.close();
		out.flush();
		out.close();
	}

	private static boolean check(int i, int j, int l, char[][] c, char crb) {
		// TODO Auto-generated method stub
		if ((i + h[l] * (K - 1) < 0) || (i + h[l] * (K - 1) >= N)
				|| (j + v[l] * (K - 1) < 0) || (j + v[l] * (K - 1) >= N)) {
			return false;
		}
		for (int p = 0; p < K; p++) {
			if (c[i + h[l] * p][j + v[l] * p] != crb)
				return false;
		}

		return true;
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值