UVA - 141 The Spot Game(hash)

来源:互联网 发布:淘宝我的发票灰色的 编辑:程序博客网 时间:2024/06/05 07:05


 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

21 1 +2 2 +2 2 -1 2 +21 1 +2 2 +1 2 +2 2 -0

Sample output

Player 2 wins on move 3Draw




题意:
放石头游戏(The game of Spot)在一块NxN的板子上进行,如下图为N=4的板子。
游戏的玩法是两个玩家轮流放一块石头在空的格子上,或是可以从板子上拿一块石头起来,游戏的进行中可以发现,板子上石头的布局会不断变化,当一玩家排出已重复出现过的布局时,他就算输了这一局(一种布局如果将之旋转90度、180度、270度,镜像 亦视为相同的布局)。
若在2N步内未出现过相同的布局就算和局。

解析:主要用到hash函数+set,进行判断。

注意:4种情况要一起判断完,再存入hash表中

#include <stdio.h>#include <string.h>#include <set>using namespace std;const int N = 60;struct Node {bool grid[N][N];}board[4];int n;int hash(int s) {int v = 0;for(int i = 0; i < n; i++) {for(int j = 0; j < n; j++) {v = v*3 + board[s].grid[i][j] + 1;}}return v;}int main() {set<int> vis;int r,c;char add;bool pace;while(scanf("%d",&n) != EOF && n) {memset(board,0,sizeof(board));vis.clear();bool win = false;for(int i = 1; i <= 2*n; i++) {scanf("%d %d %c",&r,&c,&add);if(win) {continue;}r--,c--;if(add == '+') {pace = true;}else {pace = false;}board[0].grid[c][n-r-1] = pace;board[1].grid[n-r-1][n-c-1] = pace;board[2].grid[n-c-1][r] = pace;board[3].grid[r][n-c-1] = pace;int v[4];for(int j = 0; j < 4; j++) {v[j] = hash(j);if(vis.count(v[j]) && !win) {win = true;printf("Player %d wins on move %d\n",i%2+1,i);}}for(int j = 0; j < 4; j++) {vis.insert(v[j]);}}if(!win) {printf("Draw\n");}}return 0;}

0 0
原创粉丝点击