Poj1222 EXTENDED LIGHTS OUT

每次操作相当于对一个十字取异或,所以每个格子最多只点一次,而且操作的顺序无关紧要。
既然是取异或,那么很容易想到联立异或方程组求解。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
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 List<Button>();
        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;
                }
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值