/
Rotate.java
159 lines (136 loc) · 5.3 KB
/
Rotate.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package com.unitedcoders.examples.codejam;
public class Rotate {
static final int N = 7;
static final int K = 3;
/**
* @param args
*/
public static void main(String[] args) {
char[][] board = { { 'R', '.', 'B', '.', 'R', '.', '.' }, { '.', '.', 'R', '.', 'B', 'B', '.' },
{ 'R', '.', 'B', '.', '.', '.', 'B' }, { 'R', '.', 'B', '.', '.', '.', '.' },
{ '.', 'R', 'B', 'B', '.', '.', '.' }, { '.', 'B', 'R', 'B', '.', '.', '.' },
{ '.', 'R', 'R', 'B', 'R', '.', '.' } };
System.out.println("the board");
printBoard(board);
System.out.println("after 'rotation'");
fakeRotate(board);
printBoard(board);
System.out.println("Winners");
checkForWinner(board);
}
/**
* we don't really rotate, we just push everything to the right
*
* @param board
*/
public static void fakeRotate(char[][] board) {
for (int i = 0; i < N; i++) {
for (int j = N - 1; j >= 0; j--) {
if (board[i][j] != '.') {
// push to right
int m = 1;
while ((j + m) < N && board[i][j + m] == '.') {
board[i][j + m] = board[i][j + (m - 1)];
board[i][j + (m - 1)] = '.';
m++;
}
}
}
}
}
/**
* Now we walk trough the 2dim array and check only to right, diagonal and downwards. That's enough, because we
* start from "top left" and walk through line wise.
*
* @param board
*/
public static void checkForWinner(char[][] board) {
boolean redWins = false;
boolean blueWins = false;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (board[i][j] != '.') {
// winning once is good enough
if (board[i][j] == 'B' && blueWins == true) {
break;
}
if (board[i][j] == 'R' && redWins == true) {
break;
}
// check to the right
int m = 1;
while (j + m < N && board[i][j + m] == board[i][j]) {
if (++m == K) {
switch (board[i][j]) {
case 'R':
redWins = true;
System.out.println("RED");
break;
case 'B':
blueWins = true;
System.out.println("BLUE");
break;
}
}
}
// check bottom
m = 1;
while (i + m < N && board[i + m][j] == board[i][j]) {
if (++m == K) {
switch (board[i][j]) {
case 'R':
redWins = true;
System.out.println("RED");
break;
case 'B':
blueWins = true;
System.out.println("BLUE");
break;
}
}
}
// check diagonal bottom
m = 1;
while (i + m < N && j + m < N && board[i + m][j + m] == board[i][j]) {
if (++m == K) {
switch (board[i][j]) {
case 'R':
redWins = true;
System.out.println("RED");
break;
case 'B':
blueWins = true;
System.out.println("BLUE");
break;
}
}
}
// check diagonal top
m = 1;
while (i - m >= 0 && j + m < N && board[i - m][j + m] == board[i][j]) {
if (++m == K) {
switch (board[i][j]) {
case 'R':
redWins = true;
System.out.println("RED");
break;
case 'B':
blueWins = true;
System.out.println("BLUE");
break;
}
}
}
}
}
}
}
public static void printBoard(char[][] board) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
System.out.print(board[i][j]);
}
System.out.print("\n");
}
}
}