2006年百度之星程序设计大赛初赛题目2:蝈蝈式的记分

··题目名称:蝈蝈式的记分

··内容描述:
   蝈蝈小朋友刚刚学会了0-9这十个数字,也跟爸爸妈妈来参加百度每周进行的羽毛球活动。但是他还没有球拍高,于是大人们叫他记录分数。聪明的蝈蝈发现只要记录连续得分的情况就可以了,比如用“3 2 4”可以表示一方在这一局中连得三分后,输了两分,接着又连得到四分。可是,后来大人们发现蝈蝈只会用0-9这十个数字,所以当比赛选手得分超过9的时候,他会用一个X来表示10完成记分。但问题是,当记录为“X 3 5”的时候,蝈蝈自己也记不起来是一方连续得到十三分后,再输五分;还是先赢十分输三分再赢五分。
   因为百度内部就要开始进行羽毛球联赛了,要先摸清大家的实力才好分组比赛呢~于是,大人们想知道以前每局的比分是怎样的,以及谁获得了胜利。要是遇到了根据比赛记录无法确认比赛进程的情况,也要输出相应的提示哦。
   需要帮蝈蝈进一步说明的是,比赛是五局三胜的,每局先获得二十一分的为胜,但是胜方必须领先对手两分或以上,否则必须继续比赛直到一方超出对手两分为止,比分多的一方获胜。任何一方先获得三局的胜利后就获得胜利,比赛也相应的结束。而且蝈蝈保证是完整的无多余信息的记录了比赛。
  
··输入数据:
   以point.in为输入文件,文件中首行只有一个整数M,表示蝈蝈记录了多少场比赛的分数。每场比赛用两行记录,第一行是一个整数N(N<=1000)表示当前这个记录中有多少个字符,第二行就是具体的N个字符表示记录的分数。

··输出数据:
   相应的内容将输出到point.out文件中,对应每一个分数记录,输出相应的每局分数,每局分数都使用两个整数表示,表示两个选手的得分,中间用":"分隔开;每组分数记录间使用一个空行分隔开。如果相应的比赛结果无法预测的时候,以”Unknown“一个单词独占一行表示。
  
··输入和输出结果数据样例:
Sample Input:
3
23
9 7 3 6 2 4 7 8 3 2 7 9 X 2 2 1 2 1 X 1 X 1 1
25
9 3 8 5 4 8 3 9 8 4 X X X X 2 X X X X 2 8 4 9 2 4
43
7 7 7 7 7 3 4 5 6 7 6 5 4 2 1 3 5 7 9 7 5 3 1 3 0 9 9 3 9 3 2 1 1 1 5 1 5 1 5 1 5 5 1

Sample Output:
21:17 
24:22
21:3

Unknown

21:14
20:22
21:23
21:16
21:9


思路:深搜,类似一棵树,遇到“X”就分支,左儿子表示X下个分数加到记分牌左边,右儿子表示加到右边,搜到最后一层判断是否成功。

代码(通过例子):

#include <iostream>
#include <math.h>
using namespace std;

char record[1001];
int score[7][2];
int len,currentgame,leftscore,rightscore,total_result;
int possiblescore[100][7][2];

//检查整场比赛
bool checkmatch(int step)
{
   if(step==len-1)
   {
	    if(leftscore>=4||rightscore>=4||currentgame>5)
			return false;
		if(leftscore<=2&&rightscore<=2)
			return false;
		return true;
   }
   else
   {
		if(leftscore>=3||rightscore>=3)
			return false;
		else 
			return true;
   }
}

int checkgame()//检查该局
{
	if(score[currentgame][0]==21&&score[currentgame][1]<=19)
	{
		currentgame++;
		leftscore++;
		return 1;
	}
	if(score[currentgame][1]==21&&score[currentgame][0]<=19)
	{
		currentgame++;
		rightscore++;
		return 2;
	}
	if(score[currentgame][0]>21&&(score[currentgame][1]==score[currentgame][0]+2))
	{
		currentgame++;
		rightscore++;
		return 2;		
	}
	if(score[currentgame][0]>21&&(score[currentgame][1]==score[currentgame][0]-2))
	{
		currentgame++;
		leftscore++;
		return 1;		
	}
	if(score[currentgame][1]>21&&(score[currentgame][1]==score[currentgame][0]+2))
	{
		currentgame++;
		rightscore++;
		return 2;		
	}
	if(score[currentgame][1]>21&&(score[currentgame][1]==score[currentgame][0]-2))
	{
		currentgame++;
		leftscore++;
		return 1;		
	}
	if(score[currentgame][0]>21&&abs(score[currentgame][0]-score[currentgame][1])>2)
		return 0;
	if(score[currentgame][1]>21&&abs(score[currentgame][0]-score[currentgame][1])>2)
		return 0;
	return 3;
}

void dfs(int step,int which_toadd)
{
 
	if(step==len)
	{
		for(int i=0;i<currentgame;i++)
		{
			possiblescore[total_result][i][0]=score[i][0];
			possiblescore[total_result][i][1]=score[i][1];
		}
		possiblescore[total_result][6][0]=currentgame;//记录总局数
		total_result++;
	}
	if(record[step]=='X')
	{
		record[step]='0'+10;//":"
	}
	score[currentgame][which_toadd]+=(record[step]-'0');
	int ret=checkgame();
	if(ret&&checkmatch(step))
	{
		if(record[step]==':')
		{
			dfs(step+1,which_toadd);
			dfs(step+1,1-which_toadd);
		}
		else
		{
			dfs(step+1,1-which_toadd);
		}
	}
	
	if(ret==1)
	{
		leftscore--;
		currentgame--;
	}
	else if(ret==2)
	{
		rightscore--;
		currentgame--;
	}
	score[currentgame][which_toadd]-=(record[step]-'0');
}

int main()
{
	freopen("template.txt","r",stdin);
	int i,testcase;
	cin>>testcase;
	while(testcase--)
	{
		cin>>len;
		currentgame=0;
		leftscore=rightscore=0;
		memset(score,0,sizeof(score));
		memset(record,0,sizeof(record));
		for(i=0;i<len;i++)
			cin>>record[i];
	total_result=0;
	dfs(0,0);
	if(total_result!=1)
		cout<<"Unknown"<<endl;
	else
		for(int i=0;i<possiblescore[0][6][0];i++)
			cout<<possiblescore[0][i][0]<<":"<<possiblescore[0][i][1]<<endl;
	cout<<endl;
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值