当前位置:网站首页>P2622 light off problem II (state compression search)
P2622 light off problem II (state compression search)
2022-07-03 07:58:00 【eva_ can(not)survive】
Turn off the lights II - Luogu https://www.luogu.com.cn/problem/P2622 A learning case of state compression , A classic topic .
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <set>
#include <cmath>
#include <map>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int MN = 65005;
const int MAXN = 1e6 + 10;
const int INF = 0x3f3f3f3f;
#define IOS ios::sync_with_stdio(false)
#define lowbit(x) ((x)&(-x))
using P = pair<int, int>;
int n, m;
int a[1005][1005];
bool vis[MAXN];
void bfs() {
queue<P> que;
int s = (1 << n) - 1;
vis[s] = true;
que.push(P(s, 0));
while (!que.empty()) {
P t = que.front();
que.pop();
if (t.first == 0)
return void(printf("%d", t.second));
for (int i = 1; i <= m; i++) {
int tmp = t.first;
for (int j = 1; j <= n; j++) {
if (a[i][j] == 1 && (1 << (j - 1)&tmp))
tmp ^= 1 << (j - 1);
else if (a[i][j] == -1 && !(1 << (j - 1)&tmp))
tmp |= 1 << (j - 1);
}
if (!vis[tmp]){
que.push(P(tmp, t.second + 1));
vis[tmp]=1;
}
}
}
printf("-1\n");
}
int main() {
int t;
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
scanf("%d", &a[i][j]);
}
}
bfs();
return 0;
}边栏推荐
猜你喜欢

What is a data type? What is the use of data types?

An intern's journey to cnosdb

Unity2019_ Natural ambient light_ Sky box

Pat grade a 1029 median

超限黑客认知

IP production stream is so close to me

Pat class a 1031 Hello world for u

Unity performance optimization

Project experience sharing: handwritten Chinese character recognition based on Shengsi mindspire

unity2019_ Input management
随机推荐
Pat class a 1031 Hello world for u
Harmonyos third training notes
【LeetCode】2. Valid parentheses · valid parentheses
创业团队如何落地敏捷测试,提升质量效能?丨声网开发者创业讲堂 Vol.03
一个实习生的CnosDB之旅
the installer has encountered an unexpected error installing this package
PDO and SDO concepts
Pulitzer Prize in the field of information graphics - malofiej Award
PIP uses image website to solve the problem of slow network speed
Huawei switch console password reset, device initialization, default password
My touch screen production "brief history" 1
Screenshot tool snipaste
Huawei switches are configured with SSH login remote management switches
[USACO12MAR]Cows in a Skyscraper G(状态压缩dp)
Enter three times and guess a number
[step on the pit series] MySQL failed to modify the root password
I want to do large screen data visualization application feature analysis
Unity performance optimization
Iterm2 setting
Install cross compiler arm none liunx gnueabihf