CF 208B Solitaire (记忆化搜索)

记忆化搜索(Mahmoud and a Message,cf 766C) 学习任何东西都是这样吧。如果长时间没有练习就会生疏和遗忘。 大概唯一的办法就是长时间的反复练习。或者专题训练+打比赛也是不错的。 学而时习之,不会忘乎。。。。。。。。。。 希望自己能越来越会学习。 官方题解是动态规划,我的是记忆化搜索,不过都大同小异了。dp[i]表示后缀i的答案,dx[i]表示后缀i的最少划分。 然后就一路记忆化搜索过去即可,最多有n个状态,时间复杂度为O(n) 阅读详情

Description

A boy named Vasya wants to play an old Russian solitaire called "Accordion". In this solitaire, the player must observe the following rules:

  • A deck of n cards is carefully shuffled, then all n cards are put on the table in a line from left to right;
  • Before each move the table has several piles of cards lying in a line (initially there are n piles, each pile has one card). Let's number the piles from left to right, from 1 to x. During one move, a player can take the whole pile with the maximum number x(that is the rightmost of remaining) and put it on the top of pile x - 1 (if it exists) or on the top of pile x - 3 (if it exists). The player can put one pile on top of another one only if the piles' top cards have the same suits or values. Please note that if pile x goes on top of pile y, then the top card of pile x becomes the top card of the resulting pile. Also note that each move decreases the total number of piles by 1;
  • The solitaire is considered completed if all cards are in the same pile.

Vasya has already shuffled the cards and put them on the table, help him understand whether completing this solitaire is possible or not.

Input

The first input line contains a single integer n(1 ≤ n ≤ 52) — the number of cards in Vasya's deck. The next line contains n space-separated strings c1, c2, ..., cn, where string ci describes the i-th card on the table. Each string ci consists of exactly two characters, the first one represents the card's value, the second one represents its suit. Cards on the table are numbered from left to right.

A card's value is specified by one of these characters: "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A". A card's suit is specified by one of these characters: "S", "D", "H", "C".

It is not guaranteed that the deck has all possible cards. Also, the cards in Vasya's deck can repeat.

Output

On a single line print the answer to the problem: string "YES" (without the quotes) if completing the solitaire is possible, string "NO" (without the quotes) otherwise.

Sample Input

Input
4
2S 2S 2C 2C
Output
YES
Input
2
3S 2C
Output
NO

Hint

In the first sample you can act like that:

  • put the 4-th pile on the 1-st one;
  • put the 3-rd pile on the 2-nd one;
  • put the 2-nd pile on the 1-st one.

In the second sample there is no way to complete the solitaire.


题意: n个由2个字符长度的集合组成, 每次最后一个集合覆盖前面第一个集合或者前面第三个集合,  覆盖操作必须符合 2个集合中至少有一个相同的字符就可以覆盖,被覆盖集合直接变成覆盖集合,问最后是否可以合并成一个集合。


分析:DFS,搜索的时候需要记录失败的状态,这里的状态保存只保存当长度一定时后三位的状态,因为只有后三位状态会不同而已。


代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <queue>
#define mem(p,k) memset(p,k,sizeof(p));
#define rep(i,j,k) for(int i=j; i<k; i++)
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define inf 0x6fffffff
#define ll long long
using namespace std;
const int mod=1e9+7;
int n,m,k,maxx,cur,x,y;
int minn;
int nex[4][2]={0,1,1,0,0,-1,-1,0};
bool dp[55][55][55][55];
char s[50][10];
int dfs(int len,int s1,int s2,int s3){
    if(len==1)return 1;
    if(dp[len][s1][s2][s3])return 0;
    if(s[s3][0]==s[s2][0]||s[s3][1]==s[s2][1]){
        if(dfs(len-1,len-3,s1,s3))return 1;
    }
    if(len>=4&&s[s3][0]==s[len-3][0]||s[s3][1]==s[len-3][1]){
        if(dfs(len-1,s3,s1,s2))return 1;
    }
    dp[len][s1][s2][s3]=1;
    return 0;
}
int main(){
    while(cin>>n){
        mem(dp,0);
        rep(i,1,n+1)scanf("%s",s[i]);
        if(dfs(n,n-2,n-1,n))cout<<"YES"<<endl;
        else cout<<"NO"<<endl;

    }

    return 0;
}



freecel 空档接龙 算法 最近很疼 研究扫雷和空当接龙 freecell 搜到几个代码, 总是没有源代码, 不爽 就写了个 本来用a* 算法, 好像出不来 然后使用深度优先, 有些可以快速得到结果.  最大移动张数计算方式:(可用单元+1)×(闲置列+1) 代码放到 https://bird-self-lib.googlecode.com/svn/branches/FreeCel 阅读详情

相关推荐

solitaire纸牌游戏

单人纸牌游戏,牌桌上有7个堆共28张牌,第一堆1张牌,第二堆2张,。。。第7堆7张,每一堆的第一张牌朝上,其他朝下。牌桌上还有4个suitpiles,一个deck card堆和一个discard card堆,布局如下(参考windows的纸牌游戏)

codeforces208B--Solitaire (记忆化搜索)

解题思路:一开始用了一下广搜,内存超了,又转向了深搜,但是超时了,忽略了一个问题,深搜的过程中有很多状态重复访问,所以最难的是找到状态的表示唯一表示方法,记忆化搜索 #include<stdio.h> #include<iostream> #include<algorithm> #include<string.h> #include&...

小糊涂的博客 329

1006. 换个格式输出整数 (15)-PAT乙级真题

1006. 换个格式输出整数 (15)让我们用字母B来表示“百”、字母S表示“十”,用“12…n”来表示个位数字n(&lt;10),换个格式来输出任一个不超过3位的正整数。例如234应该被输出为BBSSS1234,因为它有2个“百”、3个“十”、以及个位的4。输入格式:每个测试输入包含1个测试用例,给出正整数n(&lt;1000)。输出格式:每个测试用例的输出占一行,用规定的格式输出n。输入样例1...

柳婼 の blog 2254

CodeForces-208B Solitaire

CodeForces-208B Solitaire

soyamilk233的博客 457

逆向工程实战:如何用Frida和IDA破解AppleAccount的X-MMe-Nas-Qualify签名机制

本文通过逆向工程实战,详细解析了如何利用Frida动态Hook和IDA静态分析技术,破解AppleAccount服务中关键的X-MMe-Nas-Qualify签名机制。文章从环境搭建、动态追踪NSMutableURLRequest的签名注入点开始,逐步深入到AuthKit框架的静态剖析与算法还原,最终实现独立的签名生成,为安全研究和自动化测试提供了完整的技术路径。

bush的博客 703

星宸科技SSD20x/SSD21X应用层DEMO实战指南:从入门到深度定制

本文详细介绍了星宸科技SSD20x/SSD21X系列芯片的应用层DEMO开发实战,从基础环境搭建到深度定制开发。内容涵盖DEMO资源获取、核心功能实现、第三方库集成以及性能优化技巧,帮助开发者快速掌握智能显示和多媒体处理应用的开发方法。特别适合嵌入式开发者在智能家居、工业控制等场景中应用。

weixin_42524226的博客 272

CF793D(记忆化搜索)

CF793D 每次的走一步,可以选择的区域就缩小,搜索很显然会T,此时加一个记忆化即可。搜索时以当前点,链长和可选区间为搜索状态。 AC代码 #include <bits/stdc++.h> #define yes puts("yes"); #define inf 0x3f3f3f3f #define linf 0x3f3f3f3f3f3f3f3f #define ll long long #define ull unsigned long long #define debug(x) cout&

weixin_58110654的博客 476

Ai数字人直播系统搭建,本地源码部署,源码交付--支持OEM系统贴牌

AI 数字人直播系统是利用人工智能技术创建虚拟主播,并通过数字化技术实现直播互动的系统1。以下是其相关介绍:Ai数字人直播系统搭建,本地源码部署,源码交付--支持OEM系统贴牌。

weixin_ggwwsscc的博客 623

codeforces----208B Solitaire

记忆化搜索,有思路了之后还是很好写的。 #include #include #include #define N 55 #define DP dp[n][ch1][ch2][ch3] using namespace std; const char str1[]="23456789TJQKA"; const char str2[]="SDHC"; int card[N],dp[N][

HEU_xueyan的专栏 537

单波束与多波束声呐:从原理到应用场景的全面解析

本文全面解析了单波束与多波束声呐的工作原理、核心特点及应用场景。单波束声呐如精准的狙击手,适合高精度需求;多波束声呐则像高效的散弹枪,适用于快速测绘和大面积覆盖。文章还提供了选型指南和前沿发展趋势,帮助用户根据预算和需求选择最适合的声呐技术。

weixin_30872157的博客 230

【教培管家】小程序实战(五):体验课预约——从流量到转化的闭环

本节我们通过变量状态管理和数据联动,实现了孩子选择和日期选定。下一篇我们继续讲解预约页面,根据选择的日期来筛选排课,供用户进行预约。

低代码布道师的博客 1567

cf-208B - Solitaire-记录状态DFS

int dp[n][i][j][k];长度为n,第n-2个的状态为i,第n-1个的状态为j,第n个的状态为k,能不能成功操作。 这样时间复杂度为(52*52*52*52); #include #include #include #include #include #include #define INF 1000000 using namespace std; char va[15]={'2'

青竹梦 1076

R语言caret拟合模型:train函数构建xgBoost Dart模型、模型调优、自定义设置trainControl函数和tuneLength参数

R语言caret拟合模型:train函数构建xgBoost Dart模型、模型调优、自定义设置trainControl函数和tuneLength参数

statistics+insight+vista+power 533

Codeforces 208 B. Solitaire

记忆化搜索 dp【还剩下几个】【倒1】【倒2】【倒3】 CF JAVA中 public static class 前面好像不能再开类了,连注释掉的类也不可以,好像是个BUG??? B. Solitaire time limit per test 2 seconds memory limit per test 256 megabyte

码代码的猿猿 1517

【Codecs系列】x265编码器(十二):子像素运动搜索

在基于块运动补偿的视频编码框架中,运动搜索是最为重要的环节之一,同时也是编码端最为耗时的模块。运动搜索包括整像素运动搜索和子像素运动搜索,其中整像素运动搜索已经有很多快速算法,比如经典的三步搜索法、菱形搜索、六边形搜索以及TZSearch等等;但子像素搜索的快速算法却比较少,本文主要讲解x265编码器中的子像素搜索方法以及相关的快速搜索方法。

CSDN明星博主,认证博客专家,视频、Matlab领域优质创作者。 755

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

Codeforces 208B Solitaire

题意: 给你n堆扑克,每次从后往前,最后一堆扑克可以放在它前面一堆或者前面三堆的花色一样或者数字一样的那堆扑克上面,问最后能否把扑克都变为一堆。 思路: 状态记忆化搜索,即深搜过程中,不断记录不能成功的扑克状态,当再次搜索到此状态时,不往下搜索。时间复杂度感觉不知道怎么算。。。。 #include #include #include using namespace std; const

w446506278的博客 445

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

Codeforces Round #130 (Div. 2)

转载请注明出处,谢谢 http://blog.csdn.net/ACM_cxlove?viewmode=contents           by---cxlove 偶尔找场CF做做。 http://codeforces.com/contest/208 A. Dubstep 单词之间用若干个”WUB“隔开了,现在恢复原来的句子,字符串基本处理。 每次判断3位是否是指定字符串,

窝不是爱酱,喵~~~~ 1119

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

CodeForces - 208B Solitaire

A boy named Vasya wants to play an old Russian solitaire called "Accordion". In this solitaire, the player must observe the following rules: A deck of n cards is carefully shuffled, then all n car

qqchanmingdexiaji的博客 422

CF 118D Caesar‘s Legions(记忆化搜索)

记搜大法好

Jacob0824的博客 320

CodeForces - 208B Solitaire 【记忆化搜索】

点击打开链接 题意:   给你那张牌,牌的花色和数字只要任意一样一样就能合并。每次只能从最右边开始操作,要么覆盖n-1位置,要么覆盖 n-3位置。 问你最后能否全部合并成一堆。 题解:   首先想到的是bfs 但是不行,超限了。 那只能dp ,最后一个状态只能是前1个或是前3个合并而来, dp【n】=dp【n+1】||dp【n+3】; 标记用map直接映射。 如果走了这一

zoro_n的博客 395

CF 295div2 B dp(记忆化搜索)

http://codeforces.com/contest/520/problem/B B. Two Buttons time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard

ACM,再见! 1609

CodeForces 208B Solitaire

A boy named Vasya wants to play an old Russian solitaire called "Accordion". In this solitaire, the player must observe the following rules: A deck of n cards is carefully shuffled, then all n card

CS33sun的博客 424

Windows纸牌维加斯式计分法

 [维加斯式"选项的使用方式如下: 每次开始游戏时.负债为 52 美元.这代表您的赌注. 每移一张牌到花色叠将赢得 5 美元. 游戏的目标是挣到比赌注更多的钱. 如果选的是[翻三张".则只能三次翻完发牌叠. 如果选的是[翻一张".则只能一次翻完发牌叠.  无 [无"选项的使用方式如下:  在没有赌注和钱的情况下开始游戏. 在计时器打开或关掉的情况下都可以玩游戏. 游戏的目的是玩所有的牌.您不会赢或...

weixin_34087503的博客 1104

CodeForces - 208B Solitaire 记忆化搜索

A boy named Vasya wants to play an old Russian solitaire called "Accordion". In this solitaire, the player must observe the following rules: A deck of n cards is carefully shuffled, then all n cards ...

还我笑颜 280

HDU1401 Solitaire 双向搜索

/* 很显然的BFS,没有悬念,只是实现起来有点麻烦 写完才发现,本题中,起点和终点都很明确,可以用双搜 不过我的单搜也没有超时 */ #include #include #include #include using namespace std; //状态 struct node { char ch[4];//用char类型是为了省空间 int ceil; }; //判断两个状态是

幸福在路上 2846
上一篇: POJ 2828 Buy Tickets(线段树 )
下一篇: poj 3484 Showstopper(二分)
know_heng
博客等级 码龄11年 32粉丝 156原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值