Ultraviolet A 10561-treblecross (game SG function)

Source: Internet
Author: User

Ultraviolet A 10561-treblecross

Question Link

A string with 'X' and '. ', which can be in '. 'position X. Whoever releases three 'X' wins first.

Train of Thought: SG function. If each string has an X above it, the four surrounding locations will be the Forbidden Zone (it will be defeated if it is put down ), therefore, we can divide X into several sub-games to find the exception or sum of the SG function. As for the strategy, we can enumerate each location.

Code:

# Include <stdio. h> # include <string. h ># include <algorithm> using namespace STD; const int n = 205; int T, out [N], on, Len, SG [N]; char STR [N]; bool win () {for (INT I = 0; I <len-2; I ++) {If (STR [I] = 'X' & STR [I + 1] = 'X' & STR [I + 2] = 'X ') return true;} return false;} int Mex (int x) {bool vis [N]; int I, T; If (SG [x]! =-1) return SG [X]; If (x = 0) return SG [x] = 0; memset (VIS, false, sizeof (VIS )); for (INT I = 1; I <= x; I ++) {int T = Mex (max (0, I-3) ^ Mex (max (0, x-I-2); vis [T] = true;} For (INT I = 0; I <n; I ++) {If (vis [I]) continue; return SG [x] = I ;}} bool towin () {for (INT I = 0; I <Len; I ++) {If (STR [I] = '. ') {STR [I] = 'X'; If (WIN () {STR [I] = '. '; return false;} STR [I] = '. ';} int ans = 0, num = 0; For (INT I = 0; I <Len; I ++) {If (STR [I] = 'X' | (I> = 1 & STR [I-1] = 'X ') | (I> = 2 & STR [I-2] = 'X ') | (I + 1 <Len & STR [I + 1] = 'X ') | (I + 2 <Len & STR [I + 2] = 'X') {ans ^ = Mex (Num); num = 0 ;} else num ++;} ans ^ = Mex (Num); Return ans = 0;} void solve () {on = 0; Len = strlen (STR ); for (INT I = 0; I <Len; I ++) {If (STR [I]! = '. ') Continue; STR [I] = 'X'; If (WIN () | towin () out [ON ++] = I + 1; STR [I] = '. ';}} int main () {memset (SG,-1, sizeof (SG); scanf ("% d", & T); While (t --) {scanf ("% s", STR); solve (); If (on = 0) printf ("losing \ n "); else {printf ("winning \ n % d", out [0]); For (INT I = 1; I <on; I ++) printf ("% d ", out [I]); printf ("\ n") ;}} 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.