每次操作相当于对一个十字取异或,所以每个格子最多只点一次,而且操作的顺序无关紧要。
既然是取异或,那么很容易想到联立异或方程组求解。
#include
#include
#include
#include
using namespace std;
int a[31][31];
int ans[31];
void gauss(){
for(int i=0;i<30;i++){
int t=i;
for(;t<30;t++) if(a[t][i])break;
for(int j=0;j<=30;j++) swap(a[i][j],a[t][j]);
for(int j=0;j<30;j++)if( i != j && a[j][i] )
for(int k=0;k<=30;k++)
a[j][k] = a[i][k]^a[j][k];
}
}
int main(){
int T,cnt=0;
for(scanf("%d",&T);T;T--){
memset(ans,0,sizeof(ans));
for(int i=0;i<30;i++) scanf("%d",&a[i][30]);
for(int i=0;i<30;i++){
a[i][i]=1;
if(i%6!=0)a[i-1][i]=1;
if(i%6!=5)a[i+1][i]=1;
if(i>5)a[i-6][i]=1;
if(i<24)a[i+6][i]=1;
}
gauss();
for(int i=0;i<30;i++)ans[i]=a[i][30];
printf("PUZZLE #%d\n",++cnt);
for(int i=0;i<30;i++){
printf("%d",ans[i]);
// puts(i%6==5?"":" ");
printf( i%6==5 ? "\n":" ");
}
}
}
嗯,写完了不好DEBUG?那么用下面这个乱搞就好了。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Text;
namespace LIGHTS_OUT
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//List
Button[] list = new Button[40];
int tot = 0;
Button buttons = new Button();
int[] gmap = new int[40];
int[] map = new int[40];
Random ran = new Random();
public void Readfile(string path)
{
FileStream fs = new FileStream("map.txt", FileMode.Open);
StreamReader sr = new StreamReader(fs);
string str = sr.ReadToEnd().Replace("\r\n", " ");
sr.Close();
string delimStr = " ";
string[] split = str.Split(delimStr.ToCharArray());
for (int i = 0; i < 30; i++)
int.TryParse(split[i], out gmap[i]);
}
private void Form1_Load(object sender, EventArgs e)
{
Readfile("");
int x = 0, y = 0;
for(int i = 1; i <= 30; i++)
{
AddButton(x, y, i);
list[++tot] = buttons;
x += 50;
if (i % 6 == 0)
{
y += 50; x = 0;
}
}
}
private void AddButton(int x,int y,int i)
{
buttons = new Button();
buttons.Name = string.Format("{0}", i);
buttons.Location = new System.Drawing.Point(x, y);
buttons.Size = new System.Drawing.Size(50, 50);
buttons.TabIndex = i;
buttons.Text = "";
map[i] = gmap[i-1];
if (map[i] > 0)
{
map[i] = 1;
buttons.BackColor = Color.Brown;
}
else
{
map[i] = 0;
buttons.BackColor = Color.Black;
}
buttons.Tag = i;
this.Controls.Add(buttons);
buttons.Click += new EventHandler(buttons_Click);
}
private int[] gettag = new int[] { 1, -1, 6, -6 };
private void buttons_Click(object sender,EventArgs e)
{
Button but = (Button)sender;
modify(but);
int id = (int)but.Tag;
for(int i = 0; i < 4; i++)
{
if (id <= 6 && i == 3) continue;
if (id >= 25 && i == 2) continue;
if (id % 6 == 1 && i == 1) continue;
if (id % 6 == 0 && i == 0) continue;
int n_id = id + gettag[i];
modify(list[n_id]);
}
}
private void modify(Button but)
{
if (map[(int)but.Tag] == 0)
{
map[(int)but.Tag] = 1;
but.BackColor = Color.Brown;
}
else
{
map[(int)but.Tag] = 0;
but.BackColor = Color.Black ;
}
}
private void button1_Click(object sender, EventArgs e)
{
Readfile("");
for(int i = 1; i <= 30; i++)
{
map[i] = gmap[i - 1];
if( map[i] > 0 )
{
map[i] = 1;
list[i].BackColor = Color.Brown;
} else
{
map[i] = 0;
list[i].BackColor = Color.Black;
}
}
}
}
}