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

时间:2021-09-03 07:06:44

题目传送门

 /*
题意:给了两堆牌,每次从首部取出一张牌,按颜色分配到两个新堆,分配过程两新堆的总数差不大于1
记忆化搜索(DFS+DP):我们思考如果我们将连续的两个操作看成一个集体操作,那么这个操作必然是1红1黑
考虑三种情况:a[]连续两个颜色相同,输出11;b[]连续两个相同,输出22;
a[x] != b[y], 输出12;否则Impossible
详细解释:http://blog.csdn.net/jsun_moon/article/details/10254417
*/
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
using namespace std; const int MAXN = 1e3 + ;
const int INF = 0x3f3f3f3f;
char a[MAXN], b[MAXN];
bool dp[MAXN][MAXN]; bool DFS(int x, int y)
{
if (!x && !y) return true;
if (!dp[x][y]) dp[x][y] = true;
else return false; if ((x-)>= && a[x] != a[x-] && DFS (x-, y))
{
printf (""); return true;
}
if ((y-)>= && b[y] != b[y-] && DFS (x, y-))
{
printf (""); return true;
}
if ((x-)>= && (y-)>= && a[x] != b[y] && DFS (x-, y-))
{
printf (""); return true;
} return false;
} int main(void) //URAL 1501 Sense of Beauty
{
//freopen ("V.in", "r", stdin); int n;
while (scanf ("%d", &n) == )
{
memset (dp, false, sizeof (dp));
scanf ("%s", a+); scanf ("%s", b+); if (!DFS (n, n)) puts ("Impossible");
else puts ("");
} return ;
} /*
Impossible
*/