# BOJ 396

589人阅读 评论(0)

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;
}

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：494954次
• 积分：8475
• 等级：
• 排名：第2378名
• 原创：359篇
• 转载：28篇
• 译文：0篇
• 评论：56条
最新评论