zoukankan      html  css  js  c++  java
  • sdut2405Strange Square

    参考着cz的写的

    判重问题 :保存每一层的找过的节点 再在这一层找的时候 要保证与之前没有相同的

    View Code
     1 #include<stdio.h>
     2 #include<string.h>
     3 long count,f[11],s,x[11],a[11];
     4 void dfs(long v)
     5 {
     6     long j,flag = 0,g,w,q[11];    
     7     if(v>9)
     8     {    
     9         if(x[1]+x[2]+x[3]==x[4]+x[5]+x[6]&&x[4]+x[5]+x[6]==x[7]+x[8]+x[9]&&x[1]+x[4]+x[7]==x[2]+x[5]+x[8]&&x[2]+x[5]+x[8]==x[3]+x[6]+x[9])    
    10                 count++;
    11         return ;
    12     }
    13     if(v>6)
    14     {
    15         if(x[1]+x[2]+x[3]!=x[4]+x[5]+x[6])
    16             return;
    17     }
    18     if(v ==9)
    19     {
    20         if(x[1]+x[4]+x[7]!=x[2]+x[5]+x[8])
    21             return;
    22     }
    23     w = 1;
    24     for(j = 1 ; j <= 9 ; j++)
    25     {
    26         flag = 1;
    27         for(g = 1 ; g < w ; g++)
    28             if(a[j] == q[g])
    29             {
    30                 flag = 0;
    31                 break;
    32             }
    33         if(f[j]==0&&flag)
    34         {
    35             f[j] = 1;            
    36             x[v] = a[j];
    37             q[w] = a[j];
    38             w++;
    39             dfs(v+1);
    40             f[j] = 0;            
    41         }
    42     }
    43 
    44 }
    45 int main()
    46 {
    47     long i,k = 0,t;
    48     scanf("%ld", &t);
    49     while(t--)
    50     {
    51         k++;
    52         memset(f,0,sizeof(f));
    53         count = 0;
    54         for(i = 1; i <= 9;  i++)
    55             scanf("%ld",&a[i]);
    56         printf("Case %ld: ",k);
    57         dfs(1);
    58         printf("%ld\n",count);
    59     }
    60     return 0;
    61 }
  • 相关阅读:
    POJ 3253 Fence Repair
    POJ 2431 Expedition
    NYOJ 269 VF
    NYOJ 456 邮票分你一半
    划分数问题 DP
    HDU 1253 胜利大逃亡
    NYOJ 294 Bot Trust
    NYOJ 36 最长公共子序列
    HDU 1555 How many days?
    01背包 (大数据)
  • 原文地址:https://www.cnblogs.com/shangyu/p/2597007.html
Copyright © 2011-2022 走看看