UVa 141-the Spot Game

Source: Internet
Author: User

Title: Give you a n*n form, two people take turns to operate, each time in a position to put or win a pawn;

If a state, before it appears, then the other wins, ask who wins, if no one wins, output draw.

Analysis: DS, data structure, hash function, state compression.

Each row is stored in a single bit, a one-dimensional array represents a state, and a hash table is used to store the lookup.

Description: Note that the rotated State is considered to be the same.

#include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include < Cstdio> #include <cmath>using namespace std;//hash_definetypedef struct Node0{long a[50];node0 *next;} hnode;hnode* hash_head[100];hnode hash_node[100];int hash_size;void hash_init () {hash_size = 0;memset (hash_node, 0, Siz EOF (Hash_node)); memset (hash_head, 0, sizeof (hash_head));} void copy (int a[][50], int b[][50], int n) {for (int i = 0; i < n; + + i) for (int j = 0; J < N; + + j) A[i][j] = B[i ][J];} void over (int a[][50], int. B[][50], int n) {for (int i = 0; i < n; + + i) for (int j = 0; J < N; + + j) A[j][i] = B[n -1-I][J];}  int tem1[50][50],tem2[50][50];int hash_insert (int a[][50], int n) {copy (tem1, A, n), int sum = 0;for (int k = 0; k < 4; + + K) {Over (tem2, TEM1, n); copy (Tem1, TEM2, n); sum = 0;for (int i = 0; i < n; + + i) {hash_node[hash_size]. A[i] = 0;for (int j = 0; J < N; + + j) {Hash_node[hash_size]. A[i] + = Tem2[i][j]<<j;sum = (sum+TEM2[I][J])%100;}} for (hnode* p = hash_head[sum]; p; p = p->next) {int flag = 0;for (int i = 0; i < n; + + i) if (p->a[i]! = Hash _node[hash_size]. A[i]) {flag = 1;break;} if (!flag) return 1;}} Hash_node[hash_size].next = hash_head[sum];hash_head[sum] = &hash_node[hash_size ++];return 0; }//hash_endint Maps[50][50];int Main () {int N,x,y;char ch;while (~scanf ("%d", &n) && N) {memset (maps, 0, Sizeo F (maps)); Hash_init (); int flag = 0;for (int i = 1; I <= 2*n; + + i) {scanf ("%d%d%c", &x,&y,&ch); if (ch = = ' + ') {if (maps[x-1][y-1] &&!flag) flag = i;else Maps[x-1][y-1] = 1;} else {if (!maps[x-1][y-1] &&!flag) flag = i;else Maps[x-1][y-1] = 0;} if (Hash_insert (maps, n) &&!flag) flag = i;}    if (!flag) printf ("draw\n"), Else printf ("Player%d wins on move%d\n", 1+flag%2,flag);} return 0;}


UVa 141-the Spot Game

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.