scau 10306 Prison break

其实在网上查找剑客决斗也是可以找到的。这道题是动态规划,因为之前找题解的时候老是找不到题解,所以AC了以后决定发这份代码,让人们容易找到。这道题是华农的校赛题目,也是NYOJ的题目,华农的提交系统上也有这道题。记住,动态规划有九成九是玩数组。

题目:

Description

Mike was going to escape from the prison when he knew how to do it. However, many other people realized his secret and asked him for help. But, 
to avoid being caught by guards, Mike could only take one people with him. By thinking for a minute, an idea came out in his mind to choose one 
from all the people who wanted to break the prison:
Let all the n people (assuming they are numbered from 0 to n-1) gather together in a circle and play a game, the one who wins in the end might 
have a chance to escape with Mike.
Here is the rule of the game: 
Select two people adjacent to each other in the circle randomly, and then they might fight between each other. The loser in the fight has to quit 
the game and the winner can stay. Repeat this step until there is only one person left and this person can go with Mike.  
As Mike was a clever man (even though not so careful for letting others know his plan), he knew exactly who will win between i and j before the 
two people fight each other, and he draw a n*n matrix G in which G(i,j)=1 means i can win j during the game, while G(i,j)=0 in opposite. And now, 
Mike would like to know the list of the people who cannot escape with him no matter in which order the fights are arranged, can you help him?



输入格式

The first line of the input is an integer T indicating the number of the case, and in each case, the first line of the case is an integer n 
indicating the number of people. Then a n*n matrix G follows whose meaning has been shown above. (1 <= n <= 100)


输出格式

You should output the numbers of the people who might not have a chance to escape in any situations. These numbers should be printed out in 
ascending order and there is only one number in each line. If everyone might have a chance to escape, print out “none” instead. 


输入样例

2
4
0 0 0 0
1 0 0 0
1 1 0 0
1 1 1 0
4
0 0 0 1
1 0 1 0
1 0 0 1
0 1 0 0


输出样例

0
1
2
none


提示

4
0 0 0 1
1 0 1 0
1 0 0 1
0 1 0 0
Let us see this sample: 
Since 0 can beat 3, 3 beats 1 and 1 beats 2, so 0 can win the game to escape. It is also easy to find that prisoner 1, 2, 3 might have the chance to win the game. So the output is “none”.

代码:

#include<stdio.h>
#include<string.h>
int meet[220][220],fight[120][120];


main(){
    int T,n,i,j,k,flag;
    scanf("%d",&T);
    while(T--){
        memset(meet,0,sizeof(meet));
        scanf("%d",&n);
        for(i=0;i<n;i++)
        for(j=0;j<n;j++)
        scanf("%d",&fight[i][j]);
        for(i=0;i<n;i++){
            meet[i][i+1]=meet[i+1][i]=meet[i+n][i+n+1]=meet[i+n+1][i+n]=1;
        }
        for(k=2;k<=n;k++){
            for(i=0;i<n;i++)
            for(j=i+1;j<i+k;j++){
                if(!meet[i][i+k]&&meet[i][j]&&meet[j][i+k]&&(fight[i][j%n]||fight[(i+k)%n][j%n])){
                    meet[i][i+k]=meet[i+k][i]=1;
                    if(i+n<220&&i+k+n<220) meet[i+n][i+k+n]=meet[i+k+n][i+n]=1;
                    break;
                }
            }
        }
        for(i=flag=0;i<n;i++){
            if(!meet[i][i+n]) {
                printf("%d\n",i);
                flag=1;
            }
        }
        if(!flag) printf("none\n");
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值