Rockwell-like pressure dp-- Valley P2622

Rockwell-like pressure dp-- Valley P2622

Title Description

N conventional lamps, and m buttons. Each button can simultaneously control the lamp n - i-button is pressed, all lamps have an effect. For the first pressed i j lamp, is one of the 3 following effects: If A [i] [j] is 1, then when this lamp when opened, shut it off, otherwise no matter; If -1 then, if this lamp is off, then open it, otherwise no matter; if it is 0, regardless of whether these lights on, do not care.

Now these lights are open, given all the switches to control the effect of all the lamps, inquire at least a few Yaoan button to switch off all.

Input Format

The first two rows of two numbers, nm

Subsequently m rows, each row number n, a [i] [j] indicates the effect of the i-th switch of j-th light.

Output Format

An integer representing the minimum number press the button. If there is no way to make it all off, output -1

Sample input and output

Input # 1

3
2
1 0 1
-1 1 0

Output # 1

2

Description / Tips

For 20% of the data, no solution can score output.

Data for 20%, n <= 5

20% to the data, m <= 20

The above data points may overlap.

Data for 100% <= n 10, m <= 100

Thinking

1 where j represents the lamp apart by bit, where 0 represents off, BFS search all cases

Binary arithmetic little thought to understand

vis record the step, since initially there is 1, the number of steps counted one more step, the answer to the previous step output vis

According to all the states of the lamp, vis a maximum size of 2 ^ n = 1024

#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <iomanip>
#include <cstdio>

using namespace std;
typedef long long LL;
typedef pair<double, double> PDD;
typedef pair<LL, LL> PLL;

const LL N = 1e5+7;
const LL MOD = 1e9+7;
const LL INF = 0x3f3f3f3f;

#define lson l, m, rt>>1
#define rson m+1, r, rt>>1|1

int a[105][15], n, m, vis[N];
void solve()
{
    int now = (1<<n)-1, next;
    vis[now] = 1;
    queue<int> q;
    q.push(now);
    while(!q.empty())
    {
        now = q.front();q.pop();
        for(int i = 1;i <= m;++i)
        {
            next = now;
            for(int j = 1;j <= n;++j)
            {
                if(a[i][j] == 1 && next&(1<<(j-1)))//将1变为0
                    next ^= 1<<(j-1);
                else if(a[i][j] == -1)//将0变为1
                    next |= 1<<(j-1);
            }
            if(!next)
            {
                cout << vis[now] << endl;
                return ;
            }
            else if(!vis[next])
            {
                vis[next] = vis[now]+1;
                q.push(next);
            }
        }
    }
    puts("-1");
}

int main()
{
    scanf("%d%d", &n, &m);
    for(int i = 1;i <= m;++i)
        for(int j = 1;j <= n;++j)
        scanf("%d", &a[i][j]);
    solve();
    return 0;
}

Guess you like

Origin www.cnblogs.com/shuizhidao/p/11268943.html