🌕🌗🌑🌑🌑
# 題目連結
- 題目連結
- Online Judge
- uDebug
# 題目說明
Time limit: 3.000 seconds
# 題目
The game of Spot is played on an N × N
board as shown below for N = 4
. During the game, alternate players may either place a black counter (spot) in an empty square or remove one from the board, thus producing a variety of patterns. If a board pattern (or its rotation by 90 degrees
or 180 degrees
) is repeated during a game, the player producing that pattern loses and the other player wins. The game terminates in a draw after 2N
moves if no duplicate pattern is produced before then.
Consider the following patterns:
If the first pattern had been produced earlier, then any of the following three patterns (plus one other not shown) would terminate the game, whereas the last one would not.
# Input
Input will consist of a series of games, each consisting of the size of the board, N (2 ≤ N ≤ 50)
followed, on separate lines, by 2N
moves, whether they are all necessary or not. Each move will consist of the coordinates of a square (integers in the range 1..N
) followed by a blank and a character ‘+’
or ‘-’
indicating the addition or removal of a spot respectively. You may assume that all moves are legal, that is there will never be an attempt to place a spot on an occupied square, nor to remove a non-existent spot. Input will be terminated by a zero (0)
.
# Output
Output will consist of one line for each game indicating which player won and on which move, or that the game ended in a draw. See the Sample Output below for the exact format.
# Sample Input
2
1 1 +
2 2 +
2 2 -
1 2 +
2
1 1 +
2 2 +
1 2 +
2 2 -
0
# Sample Output
Player 2 wins on move 3
Draw
# 解題技巧
運用到 StringBuffer
:
StringBuffer sb = new StringBuffer()
orStringBuffer sb = new StringBuffer(string)
sb.append(string)
sb.setCharAt(index, char)
# Solution
import java.util.*; | |
public class Main{ | |
static StringBuffer str0; | |
static StringBuffer str90; | |
static StringBuffer str180; | |
static StringBuffer str270; | |
static Set<String> map; | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
int N = sc.nextInt(); | |
while(N != 0){ | |
map = new HashSet<>(); | |
str0 = new StringBuffer(); | |
int play = 0; | |
boolean isDraw = true; | |
for(int i = 0; i < N * N; i++){ | |
str0.append("-"); | |
} | |
str90 = new StringBuffer(str0); | |
str180 = new StringBuffer(str0); | |
str270 = new StringBuffer(str0); | |
for(int i = 1; i <= 2 * N; i++){ | |
int x = sc.nextInt(); | |
int y = sc.nextInt(); | |
char doing = sc.next().charAt(0); | |
if(!isDraw){ | |
continue; | |
} | |
if(isWin(N, x - 1, y - 1, doing)){ | |
isDraw = false; | |
play = i; | |
} | |
} | |
System.out.println((isDraw ? "Draw" : "Player " + (play % 2 + 1) + " wins on move " + play)); | |
N = sc.nextInt(); | |
} | |
sc.close(); | |
} | |
public static boolean isWin(int n, int x, int y, char doing){ | |
str0.setCharAt(toOneArray(x, y, n), doing); | |
str90.setCharAt(toOneArray(n - x - 1, y, n), doing); | |
str180.setCharAt(toOneArray(n - x - 1, n - y - 1, n), doing); | |
str270.setCharAt(toOneArray(x, n - y - 1, n), doing); | |
if(map.contains(str0.toString())){ | |
return true; | |
} | |
map.add(str0.toString()); | |
map.add(str90.toString()); | |
map.add(str180.toString()); | |
map.add(str270.toString()); | |
return false; | |
} | |
public static int toOneArray(int x, int y, int n){ | |
return x * n + y; | |
} | |
} |
單字
** **
!! !!
片語 & 搭配詞
!! !!