UVA 11534-say Goodbye to Tic-Tac-Toe (NIM and)

Source: Internet
Author: User
Tags cas

Topic Link: UVA 11534-say Goodbye to Tic-Tac-Toe

Topic: Given a 1*n of the sub, each operation can be selected an unfinished sub-picture x or O, if the operation formed XX or OO, then the operator as a failure, man-made, for a given state (note the current state is counted in the steps), asked whether it can beat the computer.

Problem-solving ideas: For fixed length, the two ends may be empty, x,o, the combination of a total of 9 kinds, although some cases equivalent, but for the convenience of treatment, divided into 9 cases. The SG values of 9 cases in each length are pre-processed. Then for the given state, the enumeration position is X and O, and the Nim and the new state are judged.

#include <cstdio>#include <cstring>#include <algorithm>using namespace STD;Const intMAXN = -;ints[maxn+5][9];intSG (intLintXintY) {intvis[maxn+5];memset(Vis,0,sizeof(VIS)); for(inti =1; I <= l; i++) {if((I! =1|| X! =1) && (i! = L | | Y! =1)) {intX = s[i-1][x*3+1] ^ s[l-i][1*3+y]; VIS[X] =1; }if((I! =1|| X! =2) && (i! = L | | Y! =2)) {intO = s[i-1][x*3+2] ^ s[l-i][2*3+y]; Vis[o] =1; }    }intMV =-1; while(VIS[++MV]);returnMV;}voidInit (intN) {memset(s[0],0,sizeof(s[0])); for(inti =0; I <9; i++) s[1][i] =1; s[1][1*3+2] = s[1][2*3+1] =0; for(inti =2; I <= N; i++) { for(intx =0; X <3; X + +) { for(inty =0; Y <3; y++) s[i][x*3+y] = SG (i, x, y); }    }}Charstr[maxn+5];BOOLJudge () {intLen =strlen(str+1);intL =0, r =0, cnt =0, ans =0, k =0; for(inti =1; I <= Len; i++) {if(Str[i] = ='. ') k++;Else{r = (Str[i] = =' X '?1:2); Ans ^= s[k][l*3+R];            cnt++;            L = r; K =0; }} ans ^= s[k][l*3];if(cnt&1) ans = (ans = =0?1:0);returnAns;}intMain () {init (MAXN);intCAsscanf("%d", &cas); while(cas--) {scanf('%s ', str+1);printf("%s\n", Judge ()?"Possible.":"impossible."); }return 0;}

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.