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\n");
    }else{
        printf("impossible\n");
    }
    return 0;
}

原文地址:https://www.cnblogs.com/caomingpei/p/9695227.html

时间: 2024-11-13 08:04:11

Gym 101666K King of the Waves(dfs)的相关文章

POJ 3087 Shuffle&#39;m Up (DFS)

题目链接:Shuffle'm Up 题意:有a和b两个长度为n的字符序列,现定义操作: 将a.b的字符交叉合并到一个序列c,再将c最上面的n个归为a,最下面n个归为b 给出a,b和目标序列c,问最少多少次操作a.b转化为c 解析:将a.b放入哈希表,然后模拟操作过程直接dfs即可. AC代码: #include <cstdio> #include <iostream> #include <cstring> #include <map> using names

LeetCode Subsets (DFS)

题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. 1 class Solution { 2 public: 3 vector<vector<int>> subsets(vector<int>& nums) { 4 sort(nums.begin(),nums.end()); 5 DFS(0,nums,tmp); 6 return a

POJ 1699 Best Sequence(DFS)

題目鏈接 題意 : 將幾個片段如圖所示方法縮成一個序列,求出最短這個序列. 思路 : 其實我也不知道怎麼做.....看網上都用了DP.....但是我不會.....這個DP不錯,還有用KMP+状压DP做的 1 //1699 2 #include <iostream> 3 #include <stdio.h> 4 #include <string.h> 5 #include <string> 6 7 using namespace std; 8 9 string

LeetCode Subsets II (DFS)

题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) 1 class Solution { 2 public: 3 vector<vector<int>> subsets(vector<int>& nums) { 4 sort(nums.begin(),nums.end()); 5 DFS(0,nums,tmp); 6 ans.push_back(vector

poj A Knight&#39;s Journey(DFS)

题目链接:http://acm.hrbust.edu.cn/vj/index.php?c=problem-problem&id=190592 题意:给出p*q的棋盘,从(A,1)开始,走“日”字,问能否走完棋盘上所有的点,如果能,按字典序输出路径: 思路:DFS,并保存路径即可,注意处理走的方向顺序int dir[8][2]={{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}}; #include <stdio.h> #in

11218 - KTV(dfs)

问题 C: Repeat Number 时间限制: 1 Sec  内存限制: 128 MB 提交: 23  解决: 7 [提交][状态][论坛] 题目描述 Definition: a+b = c, if all the digits of c are same ( c is more than ten),then we call a and b are Repeat Number. My question is How many Repeat Numbers in [x,y]. 输入 There

POJ 2488-A Knight&#39;s Journey(DFS)

A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 31702   Accepted: 10813 Description Background The knight is getting bored of seeing the same black and white squares again and again and has decided to make a journey ar

poj3187Backward Digit Sums(DFS)

题目链接: huangjing 思路: 这个题目想到dfs很容易,但是纠结在这么像杨辉三角一样计算那些值,这个我看的队友的,简直厉害,用递归计算出杨辉三角顶端的值....具体实现详见代码... 题目: Language: Default Backward Digit Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4285   Accepted: 2474 Description FJ and his cows

A Knight&#39;s Journey (DFS)

Background The knight is getting bored of seeing the same black and white squares again and again and has decided to make a journey around the world. Whenever a knight moves, it is two squares in one direction and one square perpendicular to this. Th