您的位置:首页 > 大数据 > 人工智能

CF 208B Solitaire (记忆化搜索)

2017-04-13 09:07 351 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: