UVA10561 Treblecross
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                UVA10561 Treblecross
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                Treblecross
?題目大意:給定一個帶有.和X的字符串作為初始局面,兩人輪流游戲,將.修改為X,當一個人放下X后,出現三個連續的X,游戲接觸,放下X的人獲勝。判斷先手必勝還是必敗,并給出第一步到達必勝局面的所有放法。
?
對于X.X或..XX..的情況:特殊判斷有解并輸出即可
?
其他情況:每個X旁邊都有四個禁區(左右各兩個),禁區用i表示,即..iiXii...,顯然放到i這個地方是先手必敗的
把整張圖的禁區全部標記出來,問題變成了往沒被標記的區域放X,沒放一個X將有iiXii中的i被標記,被標記的格子不能走,問先手必勝/必敗
每一段沒被標記的區域都是一個子游戲,sg[i]表示長度為i的子游戲sg值
預處理即可
方案只需要枚舉第一步走到哪里,重新判斷該局面的SG函數是否為0即可
?
被空格和換行各種卡PE或WA。。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <algorithm> 6 #include <queue> 7 #include <vector> 8 #include <cmath> 9 #define min(a, b) ((a) < (b) ? (a) : (b)) 10 #define max(a, b) ((a) > (b) ? (a) : (b)) 11 #define abs(a) ((a) < 0 ? (-1 * (a)) : (a)) 12 inline void swap(int &a, int &b) 13 { 14 int tmp = a;a = b;b = tmp; 15 } 16 inline void read(int &x) 17 { 18 x = 0;char ch = getchar(), c = ch; 19 while(ch < '0' || ch > '9') c = ch, ch = getchar(); 20 while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar(); 21 if(c == '-') x = -x; 22 } 23 const int INF = 0x3f3f3f3f; 24 const int MAXN = 1000; 25 int t,sg[MAXN + 10],n,b[MAXN + 10],x[MAXN + 10],tot; 26 char s[MAXN + 10]; 27 void make_sg() 28 { 29 sg[0] = 0;sg[1] = sg[2] = sg[3] = 1; 30 for(register int i = 4;i <= MAXN;++ i) 31 { 32 memset(b, 0, sizeof(b)); 33 for(register int j = 3;j <= i;++ j) 34 b[sg[max(0, j - 5)] ^ sg[max(i - j, 0)]] = 1; 35 for(register int j = 0;j <= i;++ j) 36 if(!b[j]) 37 { 38 sg[i] = j; 39 break; 40 } 41 } 42 } 43 //檢驗特殊情況 44 bool check() 45 { 46 //單獨處理x坐標 47 memset(b, 0, sizeof(b)); 48 tot = 0; 49 for(register int i = 1;i <= n;++ i) if(s[i] == 'X') x[++tot] = i; 50 int flag = 0; 51 for(register int i = 2;i <= tot;++ i) 52 if(x[i] == x[i - 1] + 1) 53 { 54 if(x[i] >= 3) b[x[i] - 2] = 1; 55 b[x[i] + 1] = 1, flag = 1; 56 } 57 else if(x[i] == x[i - 1] + 2) 58 b[x[i] - 1] = 1, flag = 1; 59 if(flag) 60 { 61 printf("WINNING\n"); 62 int pre = -1; 63 for(register int i = 1;i <= n;++ i) 64 { 65 if(b[i]) 66 { 67 if(pre != -1) printf("%d ", pre); 68 pre = i; 69 } 70 } 71 if(pre != -1) printf("%d", pre); 72 putchar('\n'); 73 return 1; 74 } 75 return 0; 76 } 77 int main() 78 { 79 read(t);make_sg(); 80 for(;t;--t) 81 { 82 scanf("%s", s + 1);n = strlen(s + 1); 83 int sum = 0; 84 if(check()) 85 continue; 86 int l = 0; 87 for(register int i = 1;i <= tot;++ i) 88 { 89 if(x[i] - 3 - l >= 0) sum ^= sg[x[i] - 3 - l]; 90 l = x[i] + 2; 91 } 92 if(n - l >= 0) sum ^= sg[n - l]; 93 if(!sum) 94 { 95 printf("LOSING\n\n"); 96 continue; 97 } 98 printf("WINNING\n"); 99 memset(b, 0, sizeof(b)); 100 for(register int i = 1;i <= tot;++ i) 101 { 102 if(x[i] - 1 > 0) b[x[i] - 1] = 1; 103 if(x[i] - 2 > 0) b[x[i] - 2] = 1; 104 b[x[i]] = b[x[i] + 1] = b[x[i] + 2] = 1; 105 } 106 int pre = -1; 107 for(register int i = 1;i <= n;++ i) 108 { 109 if(b[i]) continue; 110 //在i這個位置放X 111 if(i - 1 > 0) ++ b[i - 1]; 112 if(i - 2 > 0) ++ b[i - 2]; 113 ++ b[i]; ++ b[i + 1]; ++ b[i + 2]; 114 115 int nsum = 0; 116 int l = 0; 117 b[n + 1] = 1; 118 for(register int j = 1;j <= n + 1;++ j) 119 { 120 if(!b[j] && !l) l = j; 121 else if(b[j] && l) nsum ^= sg[j - l], l = 0; 122 } 123 if(!nsum) 124 { 125 if(pre != -1) 126 printf("%d ", pre); 127 pre = i; 128 } 129 if(i - 1 > 0) -- b[i - 1]; 130 if(i - 2 > 0) -- b[i - 2]; 131 -- b[i]; -- b[i + 1]; -- b[i + 2]; 132 } 133 if(pre != -1) printf("%d", pre); 134 putchar('\n'); 135 } 136 return 0; 137 } UVA10561?
轉載于:https://www.cnblogs.com/huibixiaoxing/p/8310558.html
總結
以上是生活随笔為你收集整理的UVA10561 Treblecross的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: css scale 缩放基准点
 - 下一篇: MYSQL数据库同步工具