zoukankan      html  css  js  c++  java
  • POJ 1222 EXTENDED LIGHTS OUT(反转)

    EXTENDED LIGHTS OUT
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 12616   Accepted: 8002

    Description

    In an extended version of the game Lights Out, is a puzzle with 5 rows of 6 buttons each (the actual puzzle has 5 rows of 5 buttons each). Each button has a light. When a button is pressed, that button and each of its (up to four) neighbors above, below, right and left, has the state of its light reversed. (If on, the light is turned off; if off, the light is turned on.) Buttons in the corners change the state of 3 buttons; buttons on an edge change the state of 4 buttons and other buttons change the state of 5. For example, if the buttons marked X on the left below were to be pressed,the display would change to the image on the right. 

    The aim of the game is, starting from any initial set of lights on in the display, to press buttons to get the display to a state where all lights are off. When adjacent buttons are pressed, the action of one button can undo the effect of another. For instance, in the display below, pressing buttons marked X in the left display results in the right display.Note that the buttons in row 2 column 3 and row 2 column 5 both change the state of the button in row 2 column 4,so that, in the end, its state is unchanged. 

    Note: 
    1. It does not matter what order the buttons are pressed. 
    2. If a button is pressed a second time, it exactly cancels the effect of the first press, so no button ever need be pressed more than once. 
    3. As illustrated in the second diagram, all the lights in the first row may be turned off, by pressing the corresponding buttons in the second row. By repeating this process in each row, all the lights in the first 
    four rows may be turned out. Similarly, by pressing buttons in columns 2, 3 ?, all lights in the first 5 columns may be turned off. 
    Write a program to solve the puzzle.

    Input

    The first line of the input is a positive integer n which is the number of puzzles that follow. Each puzzle will be five lines, each of which has six 0 or 1 separated by one or more spaces. A 0 indicates that the light is off, while a 1 indicates that the light is on initially.

    Output

    For each puzzle, the output consists of a line with the string: "PUZZLE #m", where m is the index of the puzzle in the input file. Following that line, is a puzzle-like display (in the same format as the input) . In this case, 1's indicate buttons that must be pressed to solve the puzzle, while 0 indicate buttons, which are not pressed. There should be exactly one space between each 0 or 1 in the output puzzle-like display.

    Sample Input

    2
    0 1 1 0 1 0
    1 0 0 1 1 1
    0 0 1 0 0 1
    1 0 0 1 0 1
    0 1 1 1 0 0
    0 0 1 0 1 0
    1 0 1 0 1 1
    0 0 1 0 1 1
    1 0 1 1 0 0
    0 1 0 1 0 0

    Sample Output

    PUZZLE #1
    1 0 1 0 0 1
    1 1 0 1 0 1
    0 0 1 0 1 1
    1 0 0 1 0 0
    0 1 0 0 0 0
    PUZZLE #2
    1 0 0 1 1 1
    1 1 0 0 0 0
    0 0 0 1 0 0
    1 1 0 1 0 1
    1 0 1 1 0 1

    Source

     

    题意分析

    给出t组数据,每组数据为一个5×6的矩阵,矩阵中元素为0或1。在矩阵中选择每次点击一个点,可以将其相邻的(上下左右)的元素异或,即1变为0,变为1。求如何点击才能使得矩阵全部变为0。 
    输出一个5×6的矩阵,其中1表示点击该位置,0表示不点击。

     1 #include <iostream>
     2 #include <cstring>
     3 #include <string>
     4 #include <algorithm>
     5 #define inf 0x3f3f3f3f
     6 using namespace std;
     7 int a[100003][7][7];
     8 int d[5][2] = { {0,0},{0,-1},{-1,0},{0,1},{1,0} };
     9 int b[7][7], c[7][7];
    10 int map[7][7];
    11 
    12 int get(int x, int y)//获得周围的转了几次+原本的数
    13 {
    14     int i;
    15     int s = map[x][y];
    16     for (i = 0; i < 5; i++)
    17     {
    18         int xx = x + d[i][0];
    19         int yy = y + d[i][1];
    20         if (xx < 0 || y < 0 || x >= 5 || y >= 6) continue;
    21         s += b[xx][yy];
    22     }
    23     return s % 2;
    24 }
    25 
    26 int cal()//返回总共需要转几次
    27 {
    28     int i, j;
    29     int res = 0;
    30     for (i = 1; i < 5; i++)
    31     {
    32         for (j = 0; j < 6; j++)
    33         {
    34             if (get(i - 1, j) == 1)//如果返回的是奇数,说明要通过i层来改变i-1层
    35             {
    36                 res++;
    37                 b[i][j] = 1;
    38             }
    39         }
    40     }
    41     for (j = 0; j < 6; j++)
    42     {
    43         if (get(4, j) == 1) return -1;
    44     }
    45     return res;
    46 }
    47 
    48 int solve(int t)
    49 {
    50     int i,j;
    51     int ans = inf;
    52     for (i = 0; i < 1 << 6; i++)//二进制枚举这一层第一排的每一种情况
    53     {
    54         memset(b, 0, sizeof(b));
    55         for (j = 0; j < 6; j++)
    56         {
    57             b[0][j] = i >> j & 1;
    58         }
    59         int res = cal();
    60         if (res != -1 && ans > res)
    61         {
    62             ans =res;
    63             memcpy(c, b, sizeof(b));
    64         }
    65     }
    66     printf("PUZZLE #%d
    ", t);
    67     for (int i = 0; i<5; i++)
    68         for (int j = 0; j<6; j++)
    69             printf("%d%c", c[i][j], j == 5 ? '
    ' : ' ');
    70 }
    71 
    72 int main()
    73 {
    74     int n;
    75     scanf("%d", &n);
    76     int i,j,k;
    77     for (i = 1; i <=n; i++)
    78         for (j = 0; j < 5; j++)
    79             for (k = 0; k < 6; k++)
    80                 scanf("%d", &a[i][j][k]);
    81     for (i = 1; i <= n; i++)//一层层来解决
    82     {
    83         for (j = 0; j < 5; j++)
    84             for (k = 0; k < 6; k++)
    85                 map[j][k] = a[i][j][k];
    86         solve(i);
    87     }
    88 }

    高斯消元的

      1 #include <iostream>  
      2 #include<cstring>  
      3 #include<cstdio>  
      4 using namespace std;  
      5 const int maxn=230;  
      6 int a[maxn][maxn+1],x[maxn];//a 是系数矩阵和增广矩阵,x 是最后存放的解  
      7 // a[][maxn]中存放的是方程右面的值(bi)  
      8 int equ,var;//equ 是系数阵的行数,var 是系数矩阵的列数(变量的个数)  
      9 int free_num,ans=100000000;  
     10   
     11 int abs1(int num) //取绝对值  
     12 {  
     13     if (num>=0) return num;  
     14     else  
     15         return -1*num;  
     16 }  
     17 void Debug(void)  
     18 {  
     19     int i, j;  
     20     for (i = 0; i < equ; i++)  
     21     {  
     22         for (j = 0; j < var + 1; j++)  
     23         {  
     24             cout << a[i][j] << " ";  
     25         }  
     26         cout << endl;  
     27     }  
     28     cout << endl;  
     29 }  
     30 //调试输出,看消元后的矩阵值,提交时,就不用了  
     31 inline int gcd(int a, int b) //最大公约数  
     32 {  
     33     int t;  
     34     while (b != 0)  
     35     {  
     36         t = b;  
     37         b = a % b;  
     38         a = t;  
     39     }  
     40     return a;  
     41 }  
     42   
     43 inline int lcm(int a, int b) //最小公倍数  
     44 {  
     45     return a * b / gcd(a, b);  
     46 }  
     47   
     48 int dfs(int p) //枚举自由解,只能取0-1,枚举完就回带,找到最小的  
     49 {  
     50     if (p<=free_num-1) //深入到了主对角线元素非0 的行了  
     51     {  
     52         //下面就是回带的代码啊  
     53         for(int i = free_num-1; i >= 0; --i)  
     54         {  
     55             int tmp = a[i][var] % 2;  
     56             for(int j = i+1; j < var; ++j) //x[i]取决于x[i+1]--x[var]啊,所以后面的解对前面的解有影  
     57 //响。  
     58                 if(a[i][j] != 0)  
     59                     tmp = ( tmp - (a[i][j]*x[j])%2 + 2 ) % 2;  
     60             x[i] = (tmp/a[i][i]) % 2; //上面的正常解  
     61         } //回带完成了  
     62 //计算解元素为1 的个数;  
     63         int sum=0;  
     64         for(int i=0; i<var; i++) sum+=x[i];  
     65         if (ans>sum) ans=sum;  
     66         return 0;  
     67     }  
     68     x[p]=0;  
     69     dfs(p-1);  
     70     x[p]=1;  
     71     dfs(p-1);  
     72 }  
     73 void swap(int &a,int &b)  
     74 {  
     75     int temp=a;    //交换 2 个数  
     76     a=b;  
     77     b=temp;  
     78 }  
     79 int Gauss()  
     80 {  
     81     int k,col = 0;  
     82 //当前处理的列  
     83     for(k = 0; k < equ && col < var; ++k,++col)  
     84     {  
     85         int max_r = k;  
     86         for(int i = k+1; i < equ; ++i)  
     87             if(a[i][col] > a[max_r][col])  
     88                 max_r = i;  
     89         if(max_r != k)  
     90         {  
     91             for(int i = k; i < var + 1; ++i)  
     92                 swap(a[k][i],a[max_r][i]);  
     93         }  
     94         if(a[k][col] == 0)  
     95         {  
     96             k--;  
     97             continue;  
     98         }  
     99         for(int i = k+1; i < equ; ++i)  
    100         {  
    101             if(a[i][col] != 0)  
    102             {  
    103                 int LCM = lcm(a[i][col],a[k][col]);  
    104                 int ta = LCM/a[i][col], tb = LCM/a[k][col];  
    105                 if(a[i][col]*a[k][col] < 0)  
    106                     tb = -tb;  
    107                 for(int j = col; j < var + 1; ++j)  
    108                     a[i][j] = ( (a[i][j]*ta)%2 - (a[k][j]*tb)%2 + 2 ) % 2;  
    109                 // 0 和 1 两种状态  
    110             }  
    111         }  
    112     }  
    113 //a[i][j]只有  
    114   
    115 //上述代码是消元的过程,行消元完成  
    116 //解下来 2 行,判断是否无解  
    117 //注意 K 的值,k 代表系数矩阵值都为 0 的那些行的第 1 行  
    118     for(int i = k; i < equ; ++i)  
    119         if(a[i][col] != 0)  
    120             return -1;  
    121 //Debug();  
    122   
    123 //唯一解或者无穷解,k<=var  
    124 //var-k==0 唯一解;var-k>0 无穷多解,自由解的个数=var-k  
    125 //能执行到这,说明肯定有解了,无非是 1 个和无穷解的问题。  
    126 //下面这几行很重要,保证秩内每行主元非 0,且按对角线顺序排列,就是检查列  
    127     for(int i = 0; i <equ; ++i)//每一行主元素化为非零  
    128         if(!a[i][i])  
    129         {  
    130             int j;  
    131             for(j = i+1; j<var; ++j)  
    132                 if(a[i][j])  
    133                     break;  
    134             if(j == var)  
    135                 break;  
    136             for(int k = 0; k < equ; ++k)  
    137                 swap(a[k][i],a[k][j]);  
    138         }  
    139 // ----处理保证对角线主元非 0 且顺序,检查列完成  
    140   
    141     free_num=k;  
    142     if (var-k>0)  
    143     {  
    144         dfs(var-1);  
    145         return ans;  
    146         //无穷多解,先枚举解,然后用下面的回带代码进行回带;  
    147 //这里省略了下面的回带的代码;不管唯一解和无穷解都可以回带,只不过无穷解  
    148 //回带时,默认为最后几个自由变元=0 而已。  
    149     }  
    150     if(var-k<0)  
    151         return -1;  
    152 // 无解返回 -1  
    153     if (var-k==0)//唯一解时  
    154     {  
    155 //下面是回带求解代码,当无穷多解时,最后几行为 0 的解默认为 0;  
    156         for(int i = k-1; i >= 0; --i) //从消完元矩阵的主对角线非 0 的最后 1 行,开始往  
    157 //回带  
    158         {  
    159             int tmp = a[i][var] % 2;  
    160   
    161             for(int j = i+1; j < var; ++j) //x[i]取决于 x[i+1]--x[var]啊,所以后面的解对前面的解有影响。  
    162                 if(a[i][j] != 0)  
    163                     tmp = ( tmp - (a[i][j]*x[j])%2 + 2 ) % 2;  
    164             //if (a[i][i]==0) x[i]=tmp;//最后的空行时,即无穷解得  
    165             //else  
    166             x[i] = (tmp/a[i][i]) % 2; //上面的正常解  
    167         }  
    168         int sum=0;  
    169         for(int i=0; i<var; i++)  
    170             sum+=x[i];  
    171         return sum;  
    172   
    173         //回带结束了  
    174     }  
    175 }  
    176   
    177   
    178 int main(void)  
    179 {  
    180 // freopen("Input.txt", "r", stdin);  
    181     int i, j,t,t1;  
    182     cin>>t;  
    183     t1=t;  
    184     equ=30;  
    185     var=30;  
    186     while (t--)  
    187     {  
    188         memset(a, 0, sizeof(a));  
    189         memset(x, 0, sizeof(x));  
    190 //memset(free_x, 1, sizeof(free_x)); // 一开始全是不确定的变元.  
    191 //下面要根据位置计算a[i][j];  
    192         for (i = 0; i < 5; i++)  
    193         {  
    194             for (j = 0; j < 6; j++)  
    195             {  
    196                 /* for(int k=0;k<4;k++) 
    197                 { 
    198                 int ni=i+di[k]; 
    199                 int nj=j+dj[k]; 
    200                 if(inlim(ni,nj)) 
    201                 { 
    202                 a[i*6+j][ni*6+nj]=1; 
    203                 } 
    204                 } 
    205                 */  
    206                 if (i-1>=0) a[i*6+j][(i-1)*6+j]=1; //计算上面的位置  
    207                 if (i+1<=4) a[i*6+j][(i+1)*6+j]=1;//计算下面的位置  
    208                 if (j-1>=0) a[i*6+j][i*6+j-1]=1;//计算左面的位置  
    209                 if (j+1<=5) a[i*6+j][i*6+j+1]=1; //计算右面的位置  
    210                 a[i*6+j][i*6+j]=1;//别忘了计算自己  
    211                 cin>>a[i*6+j][30];  
    212 //scanf("%d", &a[i][j]);  
    213             }  
    214         }  
    215 //Debug();  
    216 //free_num = Gauss();  
    217         free_num=Gauss();  
    218         if (free_num == -1) printf("无解!
    ");  
    219         else if (free_num >= 0)  
    220         {  
    221             int na_num=0;  
    222             printf("PUZZLE #%d
    ",t1-t);  
    223             for (i = 0; i < var; i++)  
    224             {  
    225                 na_num++;  
    226                 if (na_num%6==0)  
    227                 {  
    228                     printf("%d
    ",x[i]);  
    229                 }  
    230                 else  
    231                     printf("%d ",x[i]);  
    232             }  
    233         }  
    234 // printf("
    ");  
    235     }  
    236     return 0;  
    237 }
    View Code
  • 相关阅读:
    JS日期选择器
    兼容多种浏览器的Ctrl+Enter提交兼容firefox、ie、opera
    在线播放器 在网页中插入MediaPlayer 兼容IE和FF的代码调试
    Android客户端开发即WebView组件的使用详解
    web开发时碰到的问题以及心得经验
    Select的OnChange()事件
    iPhone 浏览器开发之关键 UIWebView
    获取css样式表内样式的js函数currentStyle(IE),defaultView(FF)
    利用XMLHTTP无刷新添加数据之Get篇
    客户端表单通用验证checkForm(oForm) js版
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/8461208.html
Copyright © 2011-2022 走看看