剪刀石头布

来源:互联网 发布:高考后知哥哥去世 编辑:程序博客网 时间:2024/04/28 09:03
006 年百度之星程序设计大赛初赛题目 4 

2007-05-14 17:39 

剪刀石头布 

N 个小孩正在和你玩一种剪刀石头布游戏。 N 个小孩中有一个是裁判,其余小孩分成三组(不排除某些组没有任何成员的可能性),但是你不知道谁是裁判,也不知道小孩们的分组情况。然后,小孩们开始玩剪刀石头布游戏,一共玩 M 次,每次任意选择两个小孩进行一轮,你会被告知结果,即两个小孩的胜负情况,然而你不会得知小孩具体出的是剪刀、石头还是布。已知各组的小孩分别只会出一种手势(因而同一组的两个小孩总会是和局),而裁判则每次都会随便选择出一种手势,因此没有人会知道裁判到底会出什么。请你在 M 次剪刀石头布游戏结束后,猜猜谁是裁判。如果你能猜出谁是裁判,请说明最早在第几次游戏结束后你就能够确定谁是裁判。 

输入格式: 

输入文件包含多组测试数据。每组测试数据第一行为两个整数 N 和 M ( 1 ≤ N ≤ 500 , 0 ≤ M ≤ 2000 ),分别为小孩的个数和剪刀石头布游戏进行的次数。接下来 M 行,每行两个整数且中间以一个符号隔开。两个整数分别为进行游戏的两个小孩各自的编号,为小于 N 的非负整数。符号的可能值为“ = ”、“ > ”和“ < ”,分别表示和局、第一个小孩胜和第二个小孩胜三种情况。 

输出格式: 

每组测试数据输出一行,若能猜出谁是裁判,则输出身为裁判的小孩的编号,并输出在第几次游戏结束后就能够确定谁是裁判。如果无法确定谁是裁判,或者发现剪刀石头布游戏的胜负情况不合理(即无论谁是裁判都会出现矛盾),则输出相应的信息。具体输出格式请参考输出样例。 

输入样例



3 3 

0<1 

1<2 

2<0 

3 5 

0<1 

0>1 

1<2 

1>2 

0<2 

4 4 

0<1 

0>1 

2<3 

2>3 

1 0 



输出样例



Can not determine 

Player 1 can be determined to be the judge after 4 lines 

Impossible 

Player 0 can be determined to be the judge after 0 lines 



说明: 

共有 5 个测试数据集,每个测试数据集为一个输入文件,包含多组测试数据。每个测试数据集从易到难分别为 5 、 10 、 15 、 30 和 40 分,对每个测试数据集分别执行一次程序,每次必须在运行时限 3 秒内结束程序并输出正确的答案才能得分。 

所有数据均从标准输入设备( stdin/cin )读入,并写出到标准输出设备 ( stdout/cout )中。 

五个测试数据集中输入 N 分别不大于 20 、 50 、 100 、 200 和 500 ,各有 10 组测试数据。 

我引用了这篇博客的一些方法:http://www.cnitblog.com/liaoqingshan/archive/2006/05/31/11335.html
#include<iostream>using namespace std;const int MAX = 50;int main(){while (true){int N, M;cout << "please input the number of the children->N,and the number of the game->M:  " << endl;cin >> N >> M;if (N == 1)                                               //如果只有1个人,参加比赛,那么他就是裁判cout << "裁判就是  " << 0 << endl;else{int players[MAX][MAX] = { 0 };                       //记录比赛结果int *judger = new int[N];                          //记录是否可能为裁判,0表示不可能,1表示可能,2表示确定for (int i = 0; i < N; i++)judger[i] = 0;int n = 0;                        //累计比赛场数int min = n;                    //存储能够确定谁是裁判的最少场数cout << "please input the < and the > : " << endl;while (n < M)                //读入比赛结果信息{int thefirst, thesecond;char chaptr;cin >> thefirst >> chaptr >> thesecond;++n;int flag = (chaptr == '<') ? 1 : ((chaptr == '=') ? 2 : 3);          //分别用1,2,3表示负,平,胜if (players[thefirst][thesecond] == 0){players[thefirst][thesecond] = flag;players[thesecond][thefirst] = 4 - flag;}else if (players[thefirst][thesecond] != flag)//若a,b已对局过,且比赛结果不同{if (judger[thefirst] == 0) //a有可能为裁判judger[thefirst] = 1;else if (judger[thefirst] == 1)//a就是裁判{judger[thefirst] = 2;min = n;}if (judger[thesecond] == 0) //b有可能为裁判judger[thesecond] = 1;else if (judger[thesecond] == 1) //b就是裁判{judger[thesecond] = 2;min = n;}}}int temp1 = 0;                       //记录judger[i]=1出现的次数int temp2 = 0;                       //记录judger[i]=2出现的次数int answer;for (int i = 0; i < N; i++){//cout << judger[i] << ' ';if (judger[i] == 1)++temp1;if (judger[i] == 2){++temp2;answer = i;}}cout << endl;if (temp1 <= 2 && temp2 == 0)cout << "Can not determine" << endl;else  if (temp1 <= 2 && temp2 == 1)cout << "Player  " << answer << "  can be determined to be the judge after " << min << " lines" << endl;else  if (temp1 > 2)cout << "Impossible " << endl;delete[]judger;}}return 0;}


0 0