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 alln 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 aren piles, each pile has one card). Let's number the piles from left to right, from 1 tox. During one move, a player can take the whole pile with the maximum numberx (that is the rightmost of remaining) and put it on the top of pilex - 1 (if it exists) or on the top of pilex - 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 pilex goes on top of piley, 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 containsn space-separated strings c1, c2, ..., cn, where stringci describes thei-th card on the table. Each stringci 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.

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

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摊牌,每摊牌都有数字和花色,然后让他们按照 如果第n摊牌的花色或者数字与n-1或者n-3的相同,就将其合并,且n-1或n-3的花色数字变为n;问能否操作结束

合并为一摊。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char s[55][5];
int book[55][55][55][55];
int dfs(int l,int s1,int s2,int s3){//l是当前的长度,s1s2s3是后三位
   if(l==1) return 1;//如果已经是一摊牌,就成功了
   if(book[l][s1][s2][s3]==1) return 0;//如果这个状态已经标记过了,就不要走了,肯定走不通
   if(s[s2][0]==s[s3][0] || s[s2][1]==s[s3][1]){//比较n和n-1的花色和数字
    if(dfs(l-1,l-3,s1,s3)) return 1;//下个状态长度是l-1,下个状态的后三位 s3把s2覆盖掉
   }
    if(l>=4 && s[s3][0]==s[l-3][0] || s[s3][1]==s[l-3][1]){
        if(dfs(l-1,s3,s1,s2)) return 1;//下个状态长度是l-1,下个状态的后三位 s3把l-3覆盖掉
    }
    book[l][s1][s2][s3]=1;//标记走过的状态
    return 0;
}
int main(){
    int n,i;
    while(scanf("%d",&n)!=EOF){
        memset(book,0,sizeof(book));
        for(i=1;i<=n;i++) scanf("%s",s[i]);
        if(dfs(n,n-2,n-1,n)==1) printf("YES\n");
        else printf("NO\n");

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值