Codeforces 208 B Solitaire(记忆化搜索)

来源:互联网 发布:6合统计软件 编辑:程序博客网 时间:2024/05/29 18:20

Codeforces 208 B. Solitaire

Solitaire
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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 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.

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



题意:

给出n张扑克牌,开始时有n堆扑克牌,从左到右编号为1~n,每堆有一张扑克牌。

每次可以移动右边最后一堆扑克牌(设编号为x),如果第x-1堆最上面的扑克牌或者第x-3堆最上面的扑克牌与第x堆最上面的扑克牌的数值或者花色相同的话,就可以将第x堆扑克牌移动到第x-1堆扑克牌或第x-3堆扑克牌的上面。

问能不能将给出的扑克牌叠成一堆。

分析:

记忆化搜索,dd[sum][sum-3][sum-2][sum-1]表示有sum堆且后三堆为sum-3、sum-2、sum-1时的状态,搜索过不能实现的状态标记不再搜索。

#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>using namespace std;const int mod=1e9+7;int n,dd[60][60][60][60],can[60][60];char s[60][10];bool find(int a,int b,int c,int d){if(dd[a][b][c][d])return 0;if(a==3&&can[b][d]&&can[c][d])return 1;if(can[c][d]&&find(a-1,a-4,b,d))return 1;if(a>3&&can[a-4][d]&&find(a-1,d,b,c))return 1;dd[a][b][c][d]=1;return 0;}int main(){memset(dd,0,sizeof(dd));memset(can,0,sizeof(can));scanf("%d",&n);for(int i=0;i<n;i++)scanf("%s",s[i]);for(int i=0;i<n;i++){for(int j=0;j<n;j++){if(i!=j&&(s[i][0]==s[j][0]||s[i][1]==s[j][1]))can[i][j]=1;}}if(n==1)printf("YES\n");else if(find(n,n-3,n-2,n-1))printf("YES\n");else printf("NO\n");return 0;}


0 0
原创粉丝点击