spoj196 2010.8.2

spoj196 2010.8.2

SPOJ 196.Musketeers

 

【关键字】

DP

【摘要】

一群人围成一个圈决斗,求最后会赢得的人。

【正文】

1、题目描述

SPOJ Problem Set (classical)

196. Musketeers

Problem code: MUSKET

Description

In the time of Louis XIII and his powerfulminister cardinal Richelieu in the Full BarrelInn n musketeers had consumed their meal and were drinking wine. Wine had notrun short and therefore the musketeers were eager to quarrel, a drunken brawlbroke out, in which each musketeer insulted all the others.

 

A duel was inevitable. But who should fightwho and in what order? They decided (for the first time since the brawl theyhad done something together) that they would stay in a circle and draw lots inorder. A drawn musketeer fought against his neighbor to the right. A looser"quit the game" and to be more precise his corpse was taken away byservants. The next musketeer who stood beside the looser became the neighbor ofa winner. 

 

After years, when historians read memoriesof the winner they realized that a final result depended in a crucial extent onthe order of duels. They noticed that a fence practice had indicated, whoagainst who could win a duel. It appeared that (in mathematical language) therelation "A wins B" was not transitive! It could happen that themusketeer A fought better than B, B better than C and C better than A. Ofcourse, among three of them the first duel influenced the final result. If Aand B fight as the first, C wins eventually. But if B and C fight as the first,A wins finally. Historians fascinated by their discovery decided to verifywhich musketeers could survive. The fate of Franceand the whole civilized Europe indeed dependedon that!

 

Task

N persons with consecutive numbers from 1to n stay in a circle. They fight n-1 duels. In the first round one of thesepersons (e.g. with the number i) fights against its neighbor to the right, i.e.against the person numbered i+1 (or, if i=n, against the person numbered 1). Alooser quits the game, and the circle is tighten so that the next person inorder becomes a winner's neighbor. We are given the table with possible duelsresults, in the form of a matrix. If Ai,j = 1 then the person with the number ialways wins with the person j. If Ai,j = 0 the person i looses with j. We cansay that the person k may win the game if there exists such a series of n-1drawings, that k wins the final duel.

Write a program which:

 

reads matrix A from the standard input,

computes numbers of persons, who may winthe game,

writes them into the standard output.

Input

The number of test cases t is in the firstline of input, then t test cases follow separated by an empty line. In thefirst line of each test case integer n which satisfies the inequality3<=n<=100 is written. In each of the following n lines appears one wordconsisting of n digits 0 or 1. A digit on j-th position in i-th line denoteAi,j. Of course Ai,j = 1 - Aj,i, for i<>j. We assume that Ai,i = 1, foreach i. 

Output

For each test case in the first line thereshould be written m - the number of persons, who may win the game. In thefollowing m lines numbers of these persons should be written in ascendingorder, one number in each line.

SampleInput

1

7

1111101

0101100

0111111

0001101

0000101

1101111

0100001

SampleOutput

3

1

3

6

Source

lrj黑书

 

2、算法分析

假设需要判断x是否能赢得整场战斗,把环看成链,x点拆成2个,那编号为x的人能从所有人中胜出的充分必要条件是他能与自己相遇。酱紫,在连续的几个人的链中,只需考虑头尾两个人能否胜利会师,中间的则不予考虑。

设meet[I,j] 记录i和j能否相遇,能则为true,否则为false,则问题转化为了是否能找到一个人k使得i和k,k和j均能相遇,而i或者j能打败k

meet=true(存在i<k<j使得meet[I,k]且meet[k,j](e[I,k]或者e[j,k]))

WA因:

         1.因为是把环拆成链,所以大家的编号一次往前移了一位,酱紫%n才能配套~~就是原来1..n的编号,变成了0..n-1,在给他们的输赢关系e[i][j]赋值的时候,i,j都应该是0..n-1,我写成1..n了,一直WA

         2.Input数据是连续的01串,应该用字符串读入,而非整数。

 

3、源码

#include <cstdio>
#include <cstring>

#define MAXN 110

bool meet[MAXN*2][MAXN*2];
bool e[MAXN][MAXN];
bool flag[MAXN];
int n,sum;

int main()
{
	int T,key;
	int i,j,k,t;
	scanf("%d",&T);
	while (T--)
	{
		scanf("%d ",&n);
		memset(meet,0,sizeof(meet));
		memset(flag,0,sizeof(flag));
		getchar();
		for(i=0;i<n;i++)
		{
			char str[MAXN];
			gets(str);
			for(j=0;j<n;j++)
			{
				if (str[j]=='1')
					e[i][j]=1;
				else
					e[i][j]=0;
			}
		}

		for(int i=0;i<2*n-1;i++)
			meet[i][i+1]=1;
		sum=0;
		int i,j,k,t;
		for(k=2;k<=n;k++)//从两个人开始,有点像括号序列  i,j中间有k个人的时候,能否相遇。
		{
			for(i=0;i+k<2*n;i++)
			{
				j=i+k;
				key=0;
				for(t=i+1;t<j;t++)
				{
					if (meet[i][t]&&meet[t][j]&&(e[i%n][t%n]||e[j%n][t%n]))
					{
						key=1;
						break;
					}
				}
				if (key)
				{
					meet[i][j]=1;
					if (k==n)
					{
						sum++;
						flag[i]=1;
					}
				}
			}
		}

		printf("%d\n",sum);
		for(int i=0;i<n;i++)
			if (flag[i])
				printf("%d\n",i+1);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值