code1064 Insect Food Calculation

Source: Internet
Author: User

DFS searches for numbers corresponding to each letter

Pruning:

1. When a column of three number a B c is known, if (a+b)%n!=c && (a+b+1)%n!=c pruning (+1 is to consider carry, attention &&)

2. Taking into account that we are pruning according to each row of data, we can change the order of the search, according to the letter from the top down, from the right to the left to appear in the order to search

3. Because we are starting from the lowest bit, and, 3 numbers are all n-bit, so the high-level array is generally small, low-large, so should be from the n-1 upside down (the high is too large and not n-bit)

Code:

#include <iostream>#include<cstdlib>#include<cstring>#defineSize 30using namespacestd;intN;intcc[3][size];intdic[size]={0,1,0,3,4,2};BOOLVis[size];BOOLRowinti) {    if(dic[cc[0][i]]!=-1&&dic[cc[1][i]]!=-1&&dic[cc[2][i]]!=-1){        intk=dic[cc[0][i]]+dic[cc[1][i]]; if(k%n!=dic[cc[2][i] && (k +1)%n!=dic[cc[2][i]])return false; }    return true;}intNext () { for(intI=1; i<=n;i++){         for(intj=0;j<3; j + +){            if(dic[cc[j][i]]==-1)returnCc[j][i]; }    }    returnn+1;}BOOLCheck () {intdd[3][size];  for(intI=0;i<3; i++){         for(intj=1; j<=n;j++) {Dd[i][j]=Dic[cc[i][j]]; }    }    intF[size]; Memset (F,0,sizeof(f));  for(intI=1; i<=n;i++) {F[i]+=dd[0][i];f[i]+=dd[1][i];  while(f[i]>=n) f[i]-=n,f[i+1]++; if(f[i]!=dd[2][i])return false; }    return true;}voidDfsintk) {    if(k>N) {        if(check ()) { for(intI=1; i<=n;i++) cout<<dic[i]<<' '; Exit (0); }    }    //Jianzhi     for(intI=1; i<=n;i++) {//1        if(!row (i))return; }         for(inti=n-1; i>=0; i--){        if(!Vis[i]) {Dic[k]=i; Vis[i]=true;            DFS (next ()); Vis[i]=false; DIC[K]=-1; }    }}intMain () {Freopen ("1064.in","R", stdin); memset (DIC,-1,sizeof(DIC)); CIN>>N; CharTemp[size];  for(intI=0;i<3; i++) {cin>>temp+1;  for(intj=1; j<=n;j++) {Cc[i][j]=temp[n-j+1]- -;    }} dfs (next ()); return 0;} 
Test point #alpha1.in Results:ACMemory usage: 256kB time consumption: 1ms
Test point #alpha10.in Results:ACMemory usage: 256kB time consumption: 1ms
Test point #alpha2.in Results:ACMemory usage: 256kB time consumption: 1ms
Test point #alpha3.in Results:ACMemory usage: 256kB time consumption: 1ms
Test point #alpha4.in Results:ACMemory usage: 256kB time consumption: 1ms
Test point #alpha5.in Results:ACMemory usage: 256kB time consumption: 1ms
Test point #alpha6.in Results:ACMemory usage: 256kB time consumption: 31ms
Test point #alpha7.in Results:ACMemory usage: 256kB time consumption: 3ms
Test point #alpha8.in Results:ACMemory usage: 256kB time consumption: 78ms
Test point #alpha9.in Results:ACMemory usage: 256kB time consumption: 1ms

code1064 Insect Food Calculation

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.