Codeforces 208 B. Solitaire

Source: Internet
Author: User

Codeforces 208 B. Solitaire


Memory-based search

Dp [a few more] [1] [2] [3]


In cf java, it seems that the public static class cannot be opened again, and the commented out class cannot be used. It seems like a BUG ???

B. Solitairetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

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

  • A deckNCards is carefully shuffled, then allNCards 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 areNPiles, each pile has one card). Let's number the piles from left to right, from 1X. 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 pileXGoes on top of pileY, Then the top card of pileXBecomes 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 integerN(1? ≤?N? ≤? 52)-the number of cards in Vasya's deck. The next line containsNSpace-separated stringsC1 ,?C2 ,?...,?CN, Where stringCIDescribesI-Th card on the table. Each stringCIConsists 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 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 test (s) input
    42S 2S 2C 2C
    Output
    YES
    Input
    23S 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.



      /** * Created by ckboss on 14-9-5. */import java.util.*;public class Solitaire {    static int[][][][] dp = new int[60][60][60][60];    static boolean[][][][] vis = new boolean[60][60][60][60];    static char[] c1=new char[60];    static char[] c2=new char[60];    static boolean check(int a,int b){        if((c1[a]==c1[b])||(c2[a]==c2[b]))            return true;        else return false;    }    static int dfs(int cur,int a,int b,int c){        if(vis[cur][a][b][c]==true)            return dp[cur][a][b][c];        vis[cur][a][b][c]=true;        if(cur==1)            return dp[cur][a][b][c] = 1;        if(cur==2){            if(check(a,b)==true){                return dp[cur][a][b][c] = 1;            }            else{                return dp[cur][a][b][c] = 0;            }        }        int ret=0;        if(check(a,b)==true){            ret=dfs(cur-1,a,c,cur-3);        }        if(ret==1){            return dp[cur][a][b][c] = 1;        }        if(check(a,cur-3)==true){            ret=dfs(cur-1,b,c,a);        }        return dp[cur][a][b][c]=ret;    }    public static void main(String[] args){        Scanner in=new Scanner(System.in);        int n=in.nextInt();        for(int i=1;i<=n;i++){            String temp=in.next().trim();            c1[i]=temp.charAt(0);            c2[i]=temp.charAt(1);        }        if(n==1){            System.out.println("YES");        }        else if(dfs(n,n,n-1,n-2)==1){            System.out.println("YES");        }        else{            System.out.println("NO");        }    }}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.