• poj1753 枚举


    http://poj.org/problem?id=1753

     1 #include<stdio.h>
     2 int f[4][4],k,flag=0;
     3 void turn(int a,int b)
     4 {
     5     f[a][b]=!f[a][b];
     6     if(a>0)
     7         f[a-1][b]=!f[a-1][b];
     8     if(a<3)
     9         f[a+1][b]=!f[a+1][b];
    10     if(b>0)
    11         f[a][b-1]=!f[a][b-1];
    12     if(b<3)
    13         f[a][b+1]=!f[a][b+1];
    14 }
    15 int range()
    16 {
    17     int i,j;
    18     for(i=0;i<4;i++)
    19     {
    20         for(j=0;j<4;j++)
    21             if(f[i][j]!=f[0][0])
    22             return 0;
    23     }
    24     return 1;
    25 }
    26 int dfs(int a,int b,int c)
    27 {
    28     if(c==k)
    29     {
    30         flag=range();
    31         return 0;
    32     }
    33     if(flag||a==4) return 1;
    34     turn(a,b);
    35     if(b<3)
    36         dfs(a,b+1,c+1);
    37     else 
    38         dfs(a+1,0,c+1);
    39     turn(a,b);
    40     if(b<3)
    41         dfs(a,b+1,c);
    42     else 
    43         dfs(a+1,0,c);
    44     return 0;
    45 }
    46 int main()
    47 {
    48     int i,j;
    49     char s;
    50     for(i=0;i<4;i++)
    51     {
    52         for(j=0;j<4;j++)
    53         {
    54             scanf("%c",&s);
    55             if(s=='b')
    56                 f[i][j]=0;
    57             else 
    58                 f[i][j]=1;
    59         }
    60         getchar();
    61     }
    62     for(k=0;k<16;k++)
    63     {
    64         flag=0;
    65         dfs(0,0,0);
    66         if(flag)break;
    67     }
    68     if(flag)
    69         printf("%d
    ",k);
    70     else
    71         printf("Impossible
    ");
    72     return 0;
    73 }
    View Code
  • 相关阅读:
    Count_sort C++
    Priority_Queue C++
    HeapSort C++
    Randomized_QuickSort C++
    QuickSort c++
    我的开发环境
    github and eclipse
    centos 7 install eclipse cdt and use github
    github commands
    文化-梁晓声
  • 原文地址:https://www.cnblogs.com/sdutmyj/p/3239251.html
Copyright © 2020-2023  润新知