UVa 141 - The Spot Game

來源:互聯網
上載者:User

標籤:

題目:給你一個n*n的表格,兩個人輪流進行操作,每次在一個位置放上或者拿下一個棋子;

            如果某一狀態,之前出現過,則對方獲勝,問誰獲勝,如果沒人獲勝,輸出Draw。

分析:DS,資料結構,hash函數,狀態壓縮。

            將每一行存到一個位中,用一個一維數組表示一個狀態,然後利用hash表格儲存體尋找即可。

說明:注意旋轉後的狀態認為是相同的。

#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, sizeof(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, sizeof(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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.