1. 程式人生 > >【動態規劃】Codeforces Round #406 (Div. 2) C.Berzerk

【動態規劃】Codeforces Round #406 (Div. 2) C.Berzerk

[1] space node sca 一個 for 隊列 ber 動態規劃

有向圖博弈問題。

能轉移到一個必敗態的就是必勝態。

能轉移到的全是必勝態的就是必敗態。

轉移的時候可以用隊列維護。

可以看這個 http://www.cnblogs.com/quintessence/p/6618640.html

#include<cstdio>
#include<queue>
using namespace std;
struct Node{
	int who,pos;
};
queue<Node>q;
int n,len[2],to[2][7010],f[2][7010],cant[2][7010];
int main(){
	scanf("%d",&n);
	for(int i=0;i<2;++i){
		scanf("%d",&len[i]);
		for(int j=1;j<=len[i];++j){
			scanf("%d",&to[i][j]);
		}
	}
	f[0][0]=f[1][0]=2;
	q.push((Node){0,0});
	q.push((Node){1,0});
	while(!q.empty()){
		Node U=q.front(); q.pop();
		if(f[U.who][U.pos]==2){
			for(int i=1;i<=len[U.who^1];++i){
				if(f[U.who^1][(U.pos-to[U.who^1][i]+n)%n]==0){
					f[U.who^1][(U.pos-to[U.who^1][i]+n)%n]=1;
					q.push((Node){U.who^1,(U.pos-to[U.who^1][i]+n)%n});
				}
			}
		}
		else{
			for(int i=1;i<=len[U.who^1];++i){
				if(f[U.who^1][(U.pos-to[U.who^1][i]+n)%n]==0){
					++cant[U.who^1][(U.pos-to[U.who^1][i]+n)%n];
					if(cant[U.who^1][(U.pos-to[U.who^1][i]+n)%n]==len[U.who^1]){
						f[U.who^1][(U.pos-to[U.who^1][i]+n)%n]=2;
						q.push((Node){U.who^1,(U.pos-to[U.who^1][i]+n)%n});
					}
				}
			}
		}
	}
	for(int i=0;i<2;++i){
		for(int j=1;j<n;++j){
			if(f[i][j]==0){
				printf("Loop ");
			}
			else if(f[i][j]==1){
				printf("Win ");
			}
			else{
				printf("Lose ");
			}
		}
		puts("");
	}
	return 0;
}

【動態規劃】Codeforces Round #406 (Div. 2) C.Berzerk