24h購物| | PChome| 登入
2012-09-10 19:15:37| 人氣3,032| 回應0 | 上一篇 | 下一篇

[UVA][模擬、旋轉] 141 - The Spot Game

推薦 0 收藏 0 轉貼0 訂閱站台

 The Spot Game 

The game of Spot is played on an NxN 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:

picture23

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 and Output

Input will consist of a series of games, each consisting of the size of the board, N (2 tex2html_wrap_inline180 N tex2html_wrap_inline180 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 will consist of one line for each game indicating which player won and on which move, or that the game ended in a draw.

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


#include <stdio.h>
#include <iostream>
#include <map>
using namespace std;
void rotate(int x[][50], int n) {
int y[50][50], i, j;
for(i = 0; i < n; i++)
for(j = 0; j < n; j++)
y[j][n-i-1] = x[i][j];
for(i = 0; i < n; i++)
for(j = 0; j < n; j++)
x[i][j] = y[i][j];
}
int main() {
int n;
while(scanf("%d", &n), n) {
map<string, int> r;
int m = 2*n, board[50][50] = {}, x, y;
int flag = -1, move;
char cmd[3];
for(int i = 0; i < m; i++) {
scanf("%d %d %s", &x, &y, cmd);
if(flag != -1) continue;
x--, y--;
if(cmd[0] == '+')
board[x][y] = 1;
else board[x][y] = 0;
string s = "";
for(int j = 0; j < n; j++)
for(int k = 0; k < n; k++)
s += (board[j][k]+'0');
if(r[s] == 1) {
flag = i&1;
move = i;
continue;
}
for(int rr = 0; rr < 4; rr++) {
string s = "";
for(int j = 0; j < n; j++)
for(int k = 0; k < n; k++)
s += (board[j][k]+'0');
r[s] = 1;
rotate(board, n);
}
}
if(flag == -1)
puts("Draw");
else
printf("Player %d wins on move %d\n", !flag+1, move+1);
}
return 0;
}

台長: Morris
人氣(3,032) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][模擬] 187 - Transaction Processing
此分類上一篇:[UVA] 10267 - Graphical Editor

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文