URAL 1501. Sense of Beauty(记忆化搜索 dfs)

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1501

The owner of a casino for New Russians has a very refined sense of beauty. For example, after a game there remain two piles with the same number of cards on the table, and the owner likes the cards
to be arranged into two piles according to the color: one pile with red cards and the other with black cards. Of course, this is done not by the owner himself, but by a croupier. The owner just likes to watch the process. The croupier takes a card from the
top of one of the initial piles and puts it into one of the new piles; this is repeated until all the cards from the initial piles are transferred. The owner doesn‘t like it if one of the resulting piles grows faster than the other. At each moment the resulting
piles must not differ in size by more than one card; a bigger difference would contradict the owner‘s sense of beauty. Help the croupier to arrange the cards according to the tastes of his owner.

Input

The first line of the input contains the number N of cards in each of the piles (4 ≤ N ≤ 1000). Each of the next two lines contains N digits 0 or 1 describing the piles: 1
denotes a red-suit card and 0 denotes a black-suit card. The cards in a pile are described from the top to the bottom. There are in total N red and N black cards in the two piles.

Output

Output a line containing 2N digits 1 or 2, which describes the process of transferring the cards. Each number shows the number of the pile from which a card is taken. If it is impossible to
perform this task according to the given rules, output "Impossible".

Samples

input output
4
0011
0110
22121112
4
1100
1100
Impossible

题意:

把初始的两堆卡片(一共两种颜色),转移为新的两堆(每堆只能是一种颜色),并且在转移的过程中,新的两堆的个数相差不能大于一!

PS:

记忆化搜索,连续拿的卡片最多只能有三个是同一种颜色的!例如 11 10!

每次把连续拿两张卡片看为一个整体!

代码如下:

#include <cstdio>
char a[1047], b[1047];
int dp[1047][1047];
int dfs(int x, int y)
{
    if(x==0 && y==0)//找到了
    {
        return 1;
    }
    if(dp[x][y])
    {
        return 0;
    }
    if(!dp[x][y])
    {
        dp[x][y] = 1;
    }
    if(x>=2 && a[x]!=a[x-1] && dfs(x-2,y))//每次拿不同的
    {
        printf("11");//第一堆 黑色
        return 1;
    }
    if(y>=2 && b[y]!=b[y-1] && dfs(x,y-2))
    {
        printf("22");
        return 1;
    }
    if(x>=1 && y>=1 && a[x]!=b[y] && dfs(x-1,y-1))
    {
        printf("12");
        return 1;
    }
    return 0;
}
int main()
{
    int n;
    scanf("%d",&n);
    scanf("%s",a+1);
    scanf("%s",b+1);
    int flag = dfs(n,n);
    if(!flag)
    {
        printf("Impossible\n");
    }
    else
    {
        printf("\n");
//}
    }
    return 0;
}

/*
4
0011
0110
4
1100
1100
*/
时间: 2024-10-26 07:36:13

URAL 1501. Sense of Beauty(记忆化搜索 dfs)的相关文章

记忆化搜索(DFS+DP) URAL 1501 Sense of Beauty

题目传送门 1 /* 2 题意:给了两堆牌,每次从首部取出一张牌,按颜色分配到两个新堆,分配过程两新堆的总数差不大于1 3 记忆化搜索(DFS+DP):我们思考如果我们将连续的两个操作看成一个集体操作,那么这个操作必然是1红1黑 4 考虑三种情况:a[]连续两个颜色相同,输出11:b[]连续两个相同,输出22: 5 a[x] != b[y], 输出12:否则Impossible 6 详细解释:http://blog.csdn.net/jsun_moon/article/details/10254

记忆化搜索+DFS URAL 1183 Brackets Sequence

题目传送门 1 /* 2 记忆化搜索+DFS:dp[i][j] 表示第i到第j个字符,最少要加多少个括号 3 dp[x][x] = 1 一定要加一个括号:dp[x][y] = 0, x > y; 4 当s[x] 与 s[y] 匹配,则搜索 (x+1, y-1); 否则在x~y-1枚举找到相匹配的括号,更新最小值 5 */ 6 #include <cstdio> 7 #include <algorithm> 8 #include <cmath> 9 #include

[ACM] poj 1088 滑雪 (记忆化搜索DFS)

求n*m网格内矩形的数目[ACM] poj 1088 滑雪 (记忆化搜索DFS),布布扣,bubuko.com

URAL 1501 Sense of Beauty

1501. Sense of Beauty Time limit: 0.5 second Memory limit: 64 MB The owner of a casino for New Russians has a very refined sense of beauty. For example, after a game there remain two piles with the same number of cards on the table, and the owner lik

HDU 4597(记忆化搜索 dfs 参考)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4597 Problem Description Alice and Bob are playing a game. There are two piles of cards. There are N cards in each pile, and each card has a score. They take turns to pick up the top or bottom card from

POJ 1088 滑雪(记忆化搜索+dfs)

滑雪 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 83489   Accepted: 31234 Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道载一个区域中最长底滑坡.区域由一个二维数组给出.数组的每个数字代表点的高度.下面是一个例子 1 2 3 4 5 16 17

P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm 记忆化搜索dfs

题目描述 每年,在威斯康星州,奶牛们都会穿上衣服,收集农夫约翰在N(1<=N<=100,000)个牛棚隔间中留下的糖果,以此来庆祝美国秋天的万圣节. 由于牛棚不太大,FJ通过指定奶牛必须遵循的穿越路线来确保奶牛的乐趣.为了实现这个让奶牛在牛棚里来回穿梭的方案,FJ在第i号隔间上张贴了一个“下一个隔间”Next_i(1<=Next_i<=N),告诉奶牛要去的下一个隔间:这样,为了收集它们的糖果,奶牛就会在牛棚里来回穿梭了. FJ命令奶牛i应该从i号隔间开始收集糖果.如果一只奶牛回到某

地宫取宝 蓝桥杯 记忆化搜索 DFS

标题:地宫取宝 X 国王有一个地宫宝库.是 n x m 个格子的矩阵.每个格子放一件宝贝.每个宝贝贴着价值标签. 地宫的入口在左上角,出口在右下角. 小明被带到地宫的入口,国王要求他只能向右或向下行走. 走过某个格子时,如果那个格子中的宝贝价值比小明手中任意宝贝价值都大,小明就可以拿起它(当然,也可以不拿). 当小明走到出口时,如果他手中的宝贝恰好是k件,则这些宝贝就可以送给小明. 请你帮小明算一算,在给定的局面下,他有多少种不同的行动方案能获得这k件宝贝. [数据格式] 输入一行3个整数,用空

URAL 1152. False Mirrors (记忆化搜索 状压DP)

题目链接 题意 : 每一颗子弹破坏了三个邻近的阳台.(第N个阳台是与第1个相邻)射击后后的生存的怪物都对主角造成伤害- 如此,直到所有的怪物被消灭,求怎样射击才能受到最少伤害. 思路 : 状压,数据不是很大,可以爆一爆,或者DFS下去就行,枚举每一种状态. 1 //1152 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #define oo 1 << 28 6 using n