Cf-208B-Solitaire-record status DFS

Source: Internet
Author: User

I didn't even think about recording the status during the competition. It's so embarrassing...

Int dp [n] [I] [j] [k]; the length is n, the state of the n-2 is I, the state of the n-1 is j, the status of n is k. Can the operation be successful.

In this way, the time complexity is (52*52*52*52 );

#include
 
  #include#include
  
   #include
   
    #include
    
     #include
     
      #define INF 1000000using namespace std;char va[15]={'2','3','4','5','6','7','8','9','T','J','Q','K','A'};char su[5]={'S','D','H','C'};int dp[60][60][60][60];int a[60];int pan(int x,int y){    if(x%4==y%4)return 1;    if(x/4==y/4)return 1;    return 0;}int dfs(int ns,int x,int y,int z){    if(dp[ns][x][y][z]!=-1)return dp[ns][x][y][z];    if(ns<=3)    {        if(pan(x,z)&&pan(y,z))        {            dp[ns][x][y][z]=1;        }        else dp[ns][x][y][z]=0;        return dp[ns][x][y][z];    }    int leap=0;    if(pan(y,z)&&      dfs(ns-1,a[ns-3],x,z))leap=1;    if(pan(a[ns-3],z)&&dfs(ns-1,z      ,x,y))leap=1;    dp[ns][x][y][z]=leap;    return leap;}int main(){    int n,m,i,j,k;    char str[11];    while(~scanf("%d",&n))    {        memset(dp,-1,sizeof(dp));        for(i=1;i<=n;i++)        {            scanf("%s",str);            for(j=0;j<13;j++)                if(str[0]==va[j])a[i]=j;            for(j=0;j<4;j++)                if(str[1]==su[j])a[i]=a[i]*4+j;        }        if(n<3)        {            if(n==1)cout<<"YES"<
      
       

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.