zoukankan      html  css  js  c++  java
  • Gym 101666K King of the Waves(dfs)

    Gym 101666K King of the Waves

    Description

    You are organising a king of the hill tournament, the Buenos Aires Paddleboarding Competition (BAPC), with n participants. In a king of the hill tournament, one person starts as a “king” and is then challenged by another person, the winning person becomes the new king. This is repeated until all participants have challenged exactly once (except for the starting person). In a paddleboarding match, there are no draws. The person which ends up as king, wins the tournament. Since you are the organiser, you get to choose the starting person and the order in which they challenge the king.
    Someone is offering you a substantial amount of money in case one of the participants, Henk, ends up winning the tournament. You happen to know, for any two participants x and y, which of the two would win if they were to match during the tournament. Consequently, you choose to do the unethical: you will try to rig the game. Can you find a schedule that makes Henk win the tournament ?

    Input

    • The first line contains an integer 1 ≤ n ≤ 1000, the number of participants. The
    participants are numbered 0, . . . , n − 1, where Henk is 0.
    • Then n lines follow, where each line has exactly n characters (not counting the newline
    character). These lines represent the matrix with the information of who beats who, as
    follows. On line i the jth character is (note that 0 ≤ i, j < n):
    – ’1’ if person i will win against person j.
    – ’0’ if person i will lose against person j.
    – ’X’ if i = j.

    Output

    Print a sequence of participants, such that the first person starts as king and the consequent
    participants challenge the king. If there is no way to rig the game such that Henk wins, print
    “impossible”.

    Sample Input 1

    3
    X10
    0X1
    10X
    

    Sample Output 1

    1 2 0
    

    Sample Input 2

    3
    X10
    0X0
    11X
    

    Sample Output 2

    impossible
    

    题解

    题意

    每个人都必须参加一场挑战,若挑战King成功,则成为King。问能否存在这样一种方式,使得0号选手最终是King。如果有,输出这个序列;否则,输出impossible

    思路

    巧妙地dfs,实际上,从0开始,dfs他能够战胜的对手,如此进行下去。直到最后返回时,检查是否所有人都参加过挑战。相当于dfs遍历了全部选手,这种情况下,反向输出就是结果。

    代码

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<iostream>
    #include<string>
    #include<vector>
    #include<stack>
    #include<bitset>
    #include<cstdlib>
    #include<cmath>
    #include<set>
    #include<list>
    #include<deque>
    #include<map>
    #include<queue>
    using namespace std;
    typedef long long ll;
    const double PI = acos(-1.0);
    const double eps = 1e-6;
    const int INF = 0x3f3f3f3f;
    int N;
    
    const int MAXN = 1e3+10;
    char gra[MAXN][MAXN];
    int pro[MAXN][MAXN];
    int ans[MAXN];
    int tot = 0;
    int vis[MAXN];
    int flag = 0;
    
    void init(){
    	memset(vis,0,sizeof(vis));
    }
    
    void dfs(int u){
    	if(tot == N-1){
    		flag = 1;
    		return ;
    	}
    	for(int i=0;i<N;i++){
    		if(pro[u][i]==1&&vis[i]==0){
    			vis[i] = 1;
    			ans[tot++] = i;
    			dfs(i);
    		}
    	}
    	return ;
    }
    
    int main() {
    	init();
        scanf("%d",&N);
        for(int i=0;i<N;i++){
    		scanf("%s",&gra[i]);
    	}
    	for(int i=0;i<N;i++){
    		for(int j=0;j<N;j++){
    			if(gra[i][j]=='X')	pro[i][j]=0;
    			if(gra[i][j]=='1')	pro[i][j]=1;
    			if(gra[i][j]=='0')	pro[i][j]=0;
    		}
    	}
    	vis[0]=1;
    	dfs(0);
    	if(flag==1){
    		for(int i=tot-1; i>=0; i--) printf("%d ",ans[i]);
            printf("0
    ");
    	}else{
    		printf("impossible
    ");
    	}
    	return 0;
    }
    
    
  • 相关阅读:
    第十四章:(2)Spring Boot 与 分布式 之 Dubbo + Zookeeper
    第十四章:(1)Spring Boot 与 分布式 之 分布式介绍
    第九章:Redis 的Java客户端Jedis
    第十三章:(2)Spring Boot 与 安全 之 SpringBoot + SpringSecurity + Thymeleaf
    第八章:(1)Redis 的复制(Master/Slave)
    java学习
    周末总结4
    java
    Cheatsheet: 2012 12.17 ~ 12.31
    Cheatsheet: 2012 10.01 ~ 10.07
  • 原文地址:https://www.cnblogs.com/caomingpei/p/9695227.html
Copyright © 2011-2022 走看看