LUGOU P1092 蟲食算

來源:互聯網
上載者:User

標籤:傳送門   include   style   size   print   open   char   iostream   eof   

傳送門

 

解題思路

剛開始按yzy神犇給的方法寫,就是每次要把能算出來的都算出來,結果因為太菜寫掛了。。後來直接爆搜水過。。

#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<cstdlib>using namespace std;const int MAXN=40;int n,a[MAXN],cnt;char s[4][MAXN];bool vis[MAXN],flag;bool check(){    for(register int j=n;j;j--){        if(a[s[1][j]-‘A‘]<0 || a[s[3][j]-‘A‘]<0 || a[s[2][j]-‘A‘]<0) continue;        if((a[s[1][j]-‘A‘]+a[s[2][j]-‘A‘])%n!=a[s[3][j]-‘A‘]         && (a[s[1][j]-‘A‘]+a[s[2][j]-‘A‘]+1)%n!=a[s[3][j]-‘A‘]) return false;    }    return true;}bool pd(){    int pre=0;    for(register int j=n;j;j--){        if((a[s[1][j]-‘A‘]+a[s[2][j]-‘A‘]+pre)%n!=a[s[3][j]-‘A‘]) return false;        pre=(a[s[1][j]-‘A‘]+a[s[2][j]-‘A‘]+pre)/n;    }    return pre?false:true;}void dfs(int x,int now){    if(now==4) {now=1;x--;if(x==0) {if(pd()) flag=1;return;}}    if(a[s[now][x]-‘A‘]>=0) {dfs(x,now+1);return;}    for(register int i=n-1;i>=0;i--){        if(vis[i]) continue;        vis[i]=1;a[s[now][x]-‘A‘]=i;        if(!check()) {vis[i]=0;a[s[now][x]-‘A‘]=-1;continue;}        dfs(x,now+1);if(flag) return;vis[i]=0;a[s[now][x]-‘A‘]=-1;    }}int main(){    memset(a,-1,sizeof(a));    scanf("%d%s%s%s",&n,s[1]+1,s[2]+1,s[3]+1);    dfs(n,1);    for(register int i=0;i<n;i++) printf("%d ",a[i]);    return 0;}/*4BADCCBDADCCC*/
View Code

 

LUGOU P1092 蟲食算

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.