Question link:
Http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & page = show_problem & problem = 77
Type: Hash weight + Simulation
Original question:
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:
If the first pattern had been produced earlier, then any of the following three patterns (plus one other not shown) wocould terminate the game, whereas the last one wocould not.
Input and Output
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 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
Question:
A game played on the N * n Board is called a spot. It allows two players to take turns placing or removing pawns on the board. Once one of the players places or removes the pawns, the display status of the Board or the turntable is rotated 90 degrees, 180 degrees is previously seen, then he will lose.
Analysis and Summary:
I have read the record of my submission for this question, and Wa has performed 17 times in total.
What's the reason for this not difficult question? Let's go to science.
Reasons for making Wa:
1. the chessboard is not 4*4, but N * n. Wa n times.
2. Rotation problems. The question is actually a bit vague, but it is about Rotating 90 degrees and 180 degrees. I thought there were four States to determine whether they had appeared before, but I also mentioned (plus one other not shown) in the question. That is to say, there is still a state that does not provide an image. What else is it? So I printed the image of the question through the rotation function,
It is found that the last image is different, and it is not obtained through the rotation direction. This is actually the first mirror reflection. So there is another image that is not displayed, that is, the image that rotates 180 degrees. For this reason, we wa n more times.
This error indicates that I am still too impetuous to read the question, so I can't take it easy to understand the meaning of the question. I did it directly after reading the principle and idea, you must get rid of this habit in the future.
After the two errors are corrected, the AC is successful.
/** Ultraviolet A 141-the spot game * Time: 0.044 S (ultraviolet A) * Author: d_double **/# include <iostream> # include <cstdio> # include <cstring> using namespace STD; typedef int State [52] [52]; State Board, que [105]; const int hashsize = (1 <16); int N, nstate, flag, head [hashsize], next [hashsize]; // simulate the inline void rotate (State & next, State & S, int DIR) {If (! DIR) return; If (DIR = 1) {// clockwise 90 degrees for (INT I = 0, x = 0; I <n; ++ I, ++ X) {for (Int J = N-1, y = 0; j> = 0; -- J, ++ y) next [x] [Y] = s [J] [I];} else if (DIR = 2) {// 90 degrees for (INT I = N-1, X = 0; I> = 0; -- I, ++ X) {for (Int J = 0, y = 0; j <n; ++ J, ++ y) next [x] [Y] = s [J] [I];} else if (DIR = 3) {// 180 degrees for (INT I = N-1, X = 0; I> = 0; -- I, ++ X) {for (Int J = N-1, y = 0; j> = 0; -- J, ++ y) next [x] [Y] = s [I] [J] ;}} else if (DIR = 4) {// mirror Reflection for (INT I = 0, x = 0; I <n; ++ I, ++ X) {for (Int J = N-1, y = 0; j> = 0; -- J, ++ y) next [x] [Y] = s [I] [J] ;}} inline int Hash (State & S) {int V = 0; For (INT I = 0; I <n; ++ I) for (Int J = 0; j <n; ++ J) V = (v <1) + s [I] [J]) % hashsize; return v % hashsize;} int search (State & S) {int H = hash (s); int u = head [H]; while (u) {If (memcmp (que [u], S, sizeof (s )) = 0) return 1; u = next [u];} return 0;} inline void insert (INT s) {int H = H Ash (que [s]); int u = head [H]; while (u) {u = next [u];} next [s] = head [H]; head [H] = s;} bool add_hash (INT s) {state State [5]; memcpy (State [0], que [s], sizeof (que [s]); For (INT I = 0; I <= 4; ++ I) {rotate (State [I], que [s], i); If (search (State [I]) return false;} insert (s); Return true;} inline void Init () {nstate = 2; memset (Head, 0, sizeof (head); memset (board, 0, sizeof (board); memcpy (que [1], board, Sizeof (board); insert (1); flag = 0;} int main () {int A, B, winner, step; char ch; while (scanf ("% d", & N), n) {Init (); For (INT I = 0; I <2 * n; ++ I) {scanf ("% d % C", & A, & B, & Ch); If (CH = '+ ') board [A-1] [b-1] = 1; else board [A-1] [b-1] = 0; memcpy (que [nstate], board, sizeof (board); If (! Flag &&! Add_hash (nstate) {Winner =! (I & 1); step = I + 1; flag = 1 ;}++ nstate;} If (FLAG) printf ("Player % d wins on move % d \ n", winner + 1, step); else printf ("Draw \ n") ;}return 0 ;}
-- The meaning of life is to give it meaning.
Original
Http://blog.csdn.net/shuangde800
, By d_double(For details, refer)