BOJ 396

原创 2012年03月24日 19:39:34

Problem K. Alice's Piano
Description
Alice has a piano which can plays nice music, but it's different from other
pianos. It has two rows of keys, and the method of playing it is also quite
special. When Alice plays piano, she has to put her two hands on the leftside
of the two rows of keys respectively (without touching the leftmost keys of
the two rows). And then her hands move on the keys from left to right one by
one. Each time she can move one of her hands by one key, either on the
above row or the below row. She has to keep the difference between the
number of black keys and white keys she has already touched no more than
K to make sure the music is beautiful. When the music is end, her two hands
should be both on the rightmost of the piano keyboard.
Now Alice wants to know whether she can play nice music, given the
description of the piano.
Suppose the number of keys of the two rows are both N. And two strings are
given to describe the keyboard. “1” stands for black keys while “0” for white
ones.
Input
There are multiple cases, end by EOF.
For each case, the first line contains two integers N(3 <= N <= 1000) and K
(0 <= K <= 1000), with two 0-1 strings which are described above.
Output
If she can play the music, please output an answer string of length 2N which
has the minimum lexicographic order. In the answer string, “1” represents
move on the above row while “2” represents the below row. If she cannot,
just output "Poor Alice".
Sample Input
4 1
0011
0110
4 1
1100
1100
Sample Output
22121112
Poor Alice
Hint
Take the first sample for explaining:
Suppose Alice puts her left hand on the above row of keys, and right on the
below one, the answer string stands for the following process:
1. move right hand to the 1st key of the below row of keys.
2. move right hand to the 2nd key of the below row of keys.
3. move left hand to the 1st key of the above row of keys.
4. move right hand to the 3rd key of the below row of keys.
5. move left hand to the 2nd key of the above row of keys.
6. move left hand to the 3rd key of the above row of keys.
7. move left hand to the 4th key of the above row of keys.
8. move right hand to the 4th key of the below row of keys.

这个题是动态规划,代码很容易看懂

#include <stdio.h>
#include <string.h>
#define inf -10000000
char up[1010],dw[1010];
int dp[1010][1010][2];
int n,k;
int min(int a,int b){
	return a>b?b:a;
}
int main()
{
	int i,j,path[2010],x,y,dir;
	while(scanf("%d %d",&n,&k)!=EOF){
		scanf("%s%s",&up[1],&dw[1]);
		memset(dp,0,sizeof(dp));
		dp[0][0][0]=dp[0][0][1]=0;
		for(i=0;i<=n;i++)
			for(j=0;j<=n;j++){
				if(i==0 && j==0)continue;
				if(i!=0){
					if(dp[i-1][j][0]!=inf)
						dp[i][j][0]=dp[i-1][j][0];
					else
						dp[i][j][0]=dp[i-1][j][1];
					if(dp[i][j][0]!=inf){
						if(up[i]=='0')
							dp[i][j][0]--;
						else if(up[i]=='1')
							dp[i][j][0]++;
						if(dp[i][j][0]>k || dp[i][j][0]<-k)
							dp[i][j][0]=inf;
					}
					if(j==0)
						dp[i][0][1]=inf;
				}
				if(j!=0){
					if(dp[i][j-1][1]!=inf)
						dp[i][j][1]=dp[i][j-1][1];
					else
						dp[i][j][1]=dp[i][j-1][0];
					if(dp[i][j][1]!=inf){
						if(dw[j]=='0')
							dp[i][j][1]--;
						else if(dw[j]=='1')
							dp[i][j][1]++;
						if(dp[i][j][1]>k || dp[i][j][1]<-k)
							dp[i][j][1]=inf;
					}
					if(i==0)
						dp[0][j][0]=inf;
				}
		}
		memset(path,0,sizeof(path));
		if(dp[n][n][1]!=inf || dp[n][n][0]!=inf){
			x=n,y=n;
			if(dp[n][n][1]!=inf)
				dir=1;
			else
				dir=0;
			for(i=1;i<=n*2;i++){
				if(dir==0){
					x--;
					path[i]=1;
					if(dp[x][y][1]!=inf)
						dir=1;
					else
						dir=0;
				}
				else if(dir==1){
					y--;
					path[i]=2;
					if(dp[x][y][1]!=inf)
						dir=1;
					else
						dir=0;
				}
			}
			for(i=2*n;i>0;i--)
				printf("%d",path[i]);
			printf("\n");
		}
		else
			printf("Poor Alice\n");
	}
	
	return 0;
}


 

 

相关文章推荐

BOJ题目1023. Ancient Keyboard 源代码

  • 2012年10月08日 10:36
  • 561B
  • 下载

boj 0809复试模拟题答案

  • 2010年04月22日 16:18
  • 853KB
  • 下载

CF Round#396D (Div. 2)(Codeforces 766D) 简单并查集+map

并查集
  • ddjing_
  • ddjing_
  • 2017年02月08日 04:41
  • 120

AP396X系列PDF

  • 2012年01月12日 10:55
  • 267KB
  • 下载

app396.fon

  • 2014年06月20日 15:27
  • 19KB
  • 下载

396高校毕业设计选题

西南林业大学计算机毕业论文题目(任务书,论文,开题报告,源码范例).rar http://dl.vmall.com/c0d5ztjvxj 西南交通大学计算机毕业设计选题(任务书,开题报告,论文,源...

up_450528_hotfix_u396q1.rar

  • 2013年06月30日 19:33
  • 12KB
  • 下载

CF 396A On Number of Decompositions into Multipliers

组合数问题。表示现在不会。dp写个。感谢波神教会我优化。。。不然死都过不鸟。。。 每种因子在转移时数目是一样的,可以预处理。但是三重循环必然超时,因为是从前一行转移到后一行,每次可以先处理出所有情况,...

realtek_AP_A396

  • 2014年01月16日 21:25
  • 11.36MB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:BOJ 396
举报原因:
原因补充:

(最多只允许输入30个字)