日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HDU1425 A Chess Game

發(fā)布時間:2024/6/30 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDU1425 A Chess Game 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題目:http://acm.hdu.edu.cn/showproblem.php?pid=1524

思路:題目就是給你一個拓撲圖,然后指定點的位置放棋子,然后兩人輪流移動棋子(題目中的邊的關(guān)系),直到一方不能移動。

SG函數(shù)裸題,之前接觸的兩道一個是推的關(guān)系,一個是取石子的。這個比較明顯的就是出度為0的點,sg值為0。然后深搜得到其他點的sg值,棋子的異或和為0 則P必敗,否則N必勝

由于遞歸寫的很不好,導(dǎo)致沒過。最開始竟然用隊列隨便寫了一個。(TLE),后來嘗試用dfs寫了,WA,迫于無奈只好看題解了(果真就是dfs)? 后面附有錯誤代碼

AC代碼:

#include <iostream> #include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #include <string> #include <vector> #include <set> #include <map> #include <queue> #include <algorithm> #include <sstream> #include <stack> using namespace std; #define in freopen("in.txt", "r", stdin); typedef long long ll; const int inf = 0x3f3f3f3f;vector<int> G[1010]; int sg[1010]; int n, xi, u, v, m, x; int dfs(int id) { //為什么感覺不是太難寫,自己就是寫不出來。 T_T !!!if(sg[id] != -1) return sg[id];//已經(jīng)有了直接返回 bool vis[1010];//標(biāo)記,求mex memset(vis, false, sizeof(vis));for(int i = 0; i < G[id].size(); i++) {sg[G[id][i]] = dfs(G[id][i]);//遞歸求出后繼sg值 vis[sg[G[id][i]]]= true;//標(biāo)記后繼的sg值 }for(int i = 0; ; i++) {if(!vis[i]) {return sg[id] = i;//求自己的sg值 }} }int main() {while(~scanf("%d", &n)) {memset(G, 0, sizeof(G));for(int i = 0; i < n; i++) {scanf("%d", &xi);while(xi--) {//存圖 scanf("%d", &v);G[i].push_back(v);}}memset(sg, -1, sizeof(sg));while(scanf("%d", &m) && m) {int sum = 0;while(m--) {//異或和 scanf("%d", &x);sum ^= dfs(x);}if(sum)printf("WIN\n");elseprintf("LOSE\n");}} }

瞎寫的隊列代碼(TLE)

#include <iostream> #include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #include <string> #include <vector> #include <set> #include <map> #include <queue> #include <algorithm> #include <sstream> #include <stack> using namespace std; #define in freopen("in.txt", "r", stdin); typedef long long ll; const int inf = 0x3f3f3f3f;vector<int> G[1010]; int sg[1010]; int n, xi, u, v, m, x;void SG() {queue<int> q;for(int i = 0; i < n; i++) {if(G[i].size() == 0)sg[i] = 0;else q.push(i);//需要求的點i }bool vis[1010];while(!q.empty()) memset(vis, false, sizeof(vis));int u = q.front();bool flag = false;for(int j = 0; j < G[u].size(); j++) { if(sg[G[u][j]] != -1) {//如果后繼有一個不知道的就換 vis[sg[G[u][j]]] = true;} else {flag = true;q.push(u);q.pop();}}if(flag == false) {//說明這個點的sg值可以求 for(int j = 0; j <= 1010; j++) {if(vis[j] == false) {sg[u] = j;break;}}q.pop();}} }int main() {while(~scanf("%d", &n)) {memset(G, 0, sizeof(G));for(int i = 0; i < n; i++) {scanf("%d", &xi);if(xi == 0)continue;else {while(xi--) {scanf("%d", &v);G[i].push_back(v);}}}memset(sg, -1, sizeof(sg));SG();while(~scanf("%d", &m) && m) {int sum = 0;while(m--) {scanf("%d", &x);sum ^= sg[x];}if(sum)printf("WIN\n");elseprintf("LOSE\n");}} }

自己寫的錯誤DFS代碼:

#include <iostream> #include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #include <string> #include <vector> #include <set> #include <map> #include <queue> #include <algorithm> #include <sstream> #include <stack> using namespace std; #define in freopen("in.txt", "r", stdin); typedef long long ll; const int inf = 0x3f3f3f3f;vector<int> G[1010]; int sg[1010], indegree[1010]; int n, xi, u, v, m, x, topu; vector<int> GB[1010];//該點的 后繼值 void SG(int id) {for(int i = 0; i < G[id].size(); i++) {//我剛開始也寫的bool vis[maxn] 但是我以為這個vis的狀態(tài)會被破壞 T_T if(sg[G[id][i]] != -1)GB[id].push_back(sg[G[id][i]]);else {SG(G[id][i]);//瞎寫我以為這樣求下去,sg[G[id][i]]的值就有了 但是我還是感覺就有了。。。。 GB[id].push_back(sg[G[id][i]]);}}sg[id] = *min_element(GB[id].begin(), GB[id].end()) - 1;//直接求sg值 }int main() {while(~scanf("%d", &n)) {memset(G, 0, sizeof(G));memset(GB, 0, sizeof(GB));memset(indegree, 0, sizeof(indegree));for(int i = 0; i < n; i++) {scanf("%d", &xi);if(xi == 0)continue;else {while(xi--) {scanf("%d", &v);G[i].push_back(v);indegree[v]++;//因為是一個圖,我就打算從根節(jié)點開始往下搜 }}}memset(sg, -1, sizeof(sg));for(int i = 0; i < n; i++) {if(G[i].size() == 0)sg[i] = 0;else if(indegree[i] == 0)topu = i;//根節(jié)點 }SG(topu);while(~scanf("%d", &m) && m) {int sum = 0;while(m--) {scanf("%d", &x);sum ^= sg[x];}if(sum)printf("WIN\n");elseprintf("LOSE\n");}} }

?

?

轉(zhuǎn)載于:https://www.cnblogs.com/ACMerszl/p/9572947.html

總結(jié)

以上是生活随笔為你收集整理的HDU1425 A Chess Game的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。