【高斯消元】Poj 1222:EXTENDED LIGHTS OUT

 


 

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.

  翻译:给你一个5*6的初始状态,要求你给出一个5*6的操作矩阵,要求:当操作是1时,代表把棋盘的对应位置和它相邻的地方的状态改变(1变为0,0变为1),0则不进行操作。要求操作矩阵满足操作后棋盘状态全部为0.保证有解且唯一
  提示:按两下和不按是一样的,所以只有按不按,没有按几下的区别。也没有先按后按的区别。
  就是解异或方程。。
  每个点按不按^周围的点按不按^最开始情况=0
  转换一下。。
  周围的点^每个点按不按=最开始情况
  枚举每个点,之后就是喜闻乐见的高斯消元时间了。。(ps.如果消i元素,但是你的f[i][i]=0的话,需要找一行不等于0的行swap一下)
  代码:
  
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cmath>
 4 #include<algorithm>
 5 
 6 using namespace std;
 7 
 8 int f[6][7],ans[6][7],ga[101][101],cnt=0;
 9 
10 void print()
11 {
12     cnt++;
13     printf("PUZZLE #%d\n",cnt);
14     for(int i=1;i<=5;i++)
15     {
16         for(int j=1;j<=6;j++)
17             printf("%d ",ans[i][j]);
18         printf("\n");
19     }
20 }
21 
22 void Solve()
23 {
24     for(int i=30;i>=1;i--)
25     {
26         int x=i/6+1,y=i%6;
27         if(!y)    y+=6,x--;
28         ans[x][y]=ga[i][31];
29         for(int j=i+1;j<=30;j++)
30             if(ga[i][j]){
31                 int x1=j/6+1,y1=j%6;
32                 if(!y1) y1+=6,x1--;
33                 ans[x][y]=ans[x][y]^ans[x1][y1];
34             }
35     }
36 }
37 
38 void swapp(int l,int r)
39 {
40     for(int i=1;i<=31;i++)
41         swap(ga[l][i],ga[r][i]);
42 }
43 
44 void find(int n)
45 {
46     for(int i=n+1;i<=30;i++)
47         if(ga[i][n]){swapp(i,n);return;}
48 }
49 
50 void Guass()
51 {
52     for(int i=2;i<=30;i++)//消第几个元 
53     {
54         if(!ga[i-1][i-1])find(i-1);
55         if(!ga[i-1][i-1])continue;
56         for(int j=i;j<=30;j++)//第几个方程 
57         {
58             if(!ga[j][i-1])continue;
59             for(int k=i;k<=31;k++)//方程的第几项 
60                 ga[j][k]=ga[j][k]^ga[i-1][k];
61         }
62     }
63     Solve();
64 }
65 
66 void set()
67 {
68     for(int i=1;i<=5;i++)
69         for(int j=1;j<=6;j++){
70             ga[(i-1)*6+j][31]=f[i][j];
71             ga[(i-1)*6+j][(i-1)*6+j]=1;                //自己和上下左右是对自己有影响的点 
72             if(j!=1) ga[(i-1)*6+j][(i-1)*6+j-1]=1;
73             if(j!=6) ga[(i-1)*6+j][(i-1)*6+j+1]=1;
74             if(i!=5) ga[(i-1)*6+j][i*6+j]=1;
75             if(i!=1) ga[(i-1)*6+j][(i-2)*6+j]=1;
76         }
77     return;
78 }
79 
80 int main()
81 {
82     int T;
83     scanf("%d",&T);
84     while(T--)
85     {
86         for(int i=1;i<=5;i++)
87             for(int j=1;j<=6;j++)
88                 scanf("%d",&f[i][j]);
89         set();
90         Guass();
91         print();
92         memset(f,0,sizeof(f));
93         memset(ga,0,sizeof(ga));
94         memset(ans,0,sizeof(ans));
95     }
96     return 0;
97 }
View Code

 

  
  

转载于:https://www.cnblogs.com/tuigou/p/4635443.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值