当前位置: 首页 > 工具软件 > Solitaire4j > 使用案例 >

CodeForces-208B Solitaire

严信瑞
2023-12-01

题目:

Solitaire

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


      1.A deck of n cards is carefully shuffled, then all n cards are put on the table in a line from left to right;

      2.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;

      3.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.


    example:

    

Input

4

2S 2S 2C 2C

Output

YES

Input

2

3S 2C

Output

NO

大意:

      一副牌,有两种操作,一种是将最右边的牌堆移到倒数第四个上面,一种是将最右边的牌堆移到倒数第二个上面,移动的条件是两堆牌最顶上的牌花色或者数字相同,问是否能移成一堆。


思路:

      一开始想用暴力模拟,wa了几次之后发现两种行动不能分先后顺序,之前担心dfs会超时,最后用了发现可以ac。

代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<map>
#include<queue>
#include<vector>
#include<set>;
using namespace std;

map<string,bool> vis;
string sr[60];
int ok;

void dfs(int n) {
    if(n == 1) {
        ok = 1;
        return;
    }
    if(n >= 2&&(sr[n-1][0]==sr[n][0]||sr[n-1][1]==sr[n][1])) {
        string p1 = "", tmp = sr[n-1];
        sr[n-1] = sr[n];
        for(int i = 1; i < n; i++) p1 += sr[i];
        if(!vis[p1]) {
            vis[p1] = true;
            dfs(n-1);
        }
        if(ok) return;
        sr[n-1] = tmp;
    }
    if(n >= 4&&(sr[n-3][0]==sr[n][0]||sr[n-3][1]==sr[n][1])) {
        string p2 = "", tem = sr[n-3];
        sr[n-3] = sr[n];
        for(int i = 1; i < n; i++) p2 += sr[i];
        if(!vis[p2]) {
            vis[p2] = true;
            dfs(n-1);
        }
        if(ok) return;
        sr[n-3] = tem;
    }
}

int main() {
    int n;
    scanf("%d",&n);
    for(int i = 1; i <= n; i++) cin >> sr[i];
    ok = 0;
    dfs(n);
    if(ok) printf("YES");
    else printf("NO");
    return 0;
}




 类似资料:

相关阅读

相关文章

相关问答