Poj1222 EXTENDED LIGHTS OUT

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Elemmir/article/details/51559322

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

#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;
                }
            }
        }
    }
}
阅读更多

Extended Lights Out

03-11

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.nnnnThe 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.nnNote 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.nnnNote:nn1. It does not matter what order the buttons are pressed.nn2. 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.nnn3. 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.nnWrite a program to solve the puzzle.nnnInputnnThe 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's or 1's separated by one or more spaces. A 0 indicates that the light is off, while a 1 indicates that the light is on initially. nnOutputnnFor 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's indicate buttons, which are not pressed. There should be exactly one space between each 0 or 1 in the output puzzle-like display.nnSample Inputnn2n0 1 1 0 1 0n1 0 0 1 1 1n0 0 1 0 0 1n1 0 0 1 0 1n0 1 1 1 0 0n0 0 1 0 1 0n1 0 1 0 1 1n0 0 1 0 1 1n1 0 1 1 0 0n0 1 0 1 0 0nnSample OutputnnPUZZLE #1n1 0 1 0 0 1n1 1 0 1 0 1n0 0 1 0 1 1n1 0 0 1 0 0n0 1 0 0 0 0nPUZZLE #2n1 0 0 1 1 1n1 1 0 0 0 0n0 0 0 1 0 0n1 1 0 1 0 1n1 0 1 1 0 1

没有更多推荐了,返回首页