Luo Gu P2040 "open all the lights."

Original Created: 2018-10-02 21:56:33

Yi (ruo) wisdom (zhi) of the game

Topic Link

Topic background

pmshz playing a benefit (ruo) wisdom (zhi) of the game, the purpose is to open all the lights nine lights, this game stumped pmshz. . .

Title Description

This light is odd (Fan) strange (ren), the tap will switch the status lights and around all four lamps changes. Your task now is to tell pmshz is open to all these lights.

011 e.g.

1 0 0

1 0 1

Tap the middle of the most light [2,2] becomes

0 0 1

0 1 1

1 1 1

Then tap the top left corner lights [1,1] becomes

1 1 1

1 1 1

1 1 1

goal achieved. It requires a minimum of two steps.

Output 2 can be.

Input / Output Format & Sample

Input Format

Nine digits, the format of the input 3 * 3, only one of each of two digital intermediate space, represented by the initial light switching state. (0 for off, 1 on)

Output Format

An integer indicating the minimum number of steps to open all the lights required.

SAMPLE INPUT

0  1  1
1  0  0
1  0  1

Sample Output

2

Problem-solving ideas

We have easy to prove that for a light switch, or simply press 1 0 times

You only need to consider the switch is pressed and you can not press

So we can search directly, the total number of operations will not exceed \ (9 ^ 9 \)

With (Used \) \ array record \ (used_i \) This switch has pressed the use \ (F \) array record \ (f_ {i, j} \) in the case of blinking

Code

(On this code after the wind

/* -- Basic Headers -- */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cctype>
#include <algorithm>

/* -- STL Iterator -- */
#include <vector>
#include <string>
#include <stack>
#include <queue>

/* -- Defined Functions -- */
#define For(a,x,y) for (int a = x; a <= y; ++a)
#define Bak(a,y,x) for (int a = y; a >= x; --a)
using namespace std;

namespace FastIO {
    void DEBUG(char comment[], int x) {
        cerr << comment << x << endl;
    }

    inline int getint() {
        int s = 0, x = 1;
        char ch = getchar();
        while (!isdigit(ch)) {
            if (ch == '-') x = -1;
            ch = getchar();
        }
        while (isdigit(ch)) {
            s = s * 10 + ch - '0';
            ch = getchar();
        }
        return s * x;
    }
    inline void __basic_putint(int x) {
        if (x < 0) {
            x = -x;
            putchar('-');
        }
        if (x >= 10) __basic_putint(x / 10);
        putchar(x % 10 + '0');
    }

    inline void putint(int x, char external) {
        __basic_putint(x);
        putchar(external);
    }
}

namespace Solution {
    bool used[3 + 2][3 + 2];
    int f[3 + 2][3 + 2], ans = 10;
    const int dx[5] = {0,  0, 0, -1, 1};
    const int dy[5] = {0, -1, 1,  0, 0};
    
    void modify(int x, int y) {
        for (int i = 0; i <= 4; ++i) {
            f[x + dx[i]][y + dy[i]] = !f[x + dx[i]][y + dy[i]];
        }
    }
    
    bool Check() {
        for (int i = 1; i <= 3; ++i) {
            for (int j = 1; j <= 3; ++j) {
                if (!f[i][j]) return false;
            }
        }
        return true;
    }
    
    void dfs(int steps) {
        if (steps >= ans) return;
        if (Check()) ans = std::min(ans, steps);
        for (int i = 1; i <= 3; ++i) {
            for (int j = 1; j <= 3; ++j) {
                if (!used[i][j]) {
                    used[i][j] = true;
                    modify(i, j);
                    dfs(steps+1);
                    modify(i, j);
                    used[i][j] = false;
                } 
            } 
        }
    }
}

int main(int argc, char *const argv[]) {
    #ifdef HANDWER_FILE
    freopen("testdata.in", "r", stdin);
    freopen("testdata.out", "w", stdout);
    #endif
    using namespace Solution;
    for (int i = 1; i <= 3; ++i) {
        for (int j = 1; j <= 3; ++j) {
            f[i][j] = FastIO::getint();
        }
    }
    dfs(0);
    FastIO::putint(ans, '\n');
    return 0;
}


Guess you like

Origin www.cnblogs.com/handwer/p/11745426.html