当前位置:网站首页>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;
}
边栏推荐
- idea取消引用显示效果
- HDMI2.1与HDMI2.0的区别以及转换PD信号。
- How to clear the console password for s7700 device
- idea取消引用顯示效果
- STM32F103 SPI (pit Diary)
- What is definition? What is a statement? What is the difference between them?
- go语言-循环语句
- P1896 [SCOI2005] 互不侵犯(状压dp)
- Open the influence list of "National Meteorological Short Videos (Kwai, Tiktok) in November" in an interactive way“
- Redis batch startup and shutdown script
猜你喜欢
[step on the pit series] MySQL failed to modify the root password
Oracle queries grouped by time
My touch screen production "brief history" 2
Technical dry goods Shengsi mindspire dynamic transformer with variable sequence length has been released!
【LeetCode】3. Merge two sorted lists · merge two ordered linked lists
Pat grade a 1027 colors in Mars
一篇文章让你读懂-曼彻斯特编码
Pat class a 1032 sharing
Install cross compiler arm none liunx gnueabihf
Pulitzer Prize in the field of information graphics - malofiej Award
随机推荐
freetype库的移植
一篇文章让你读懂-曼彻斯特编码
Idea unreference Display Effect
在浏览器输入url后执行什么
华为S5700交换机初始化和配置SSH和TELNET远程登录方法
Getting started with minicom
HDMI2.1与HDMI2.0的区别以及转换PD信号。
RM delete file
Differences between tp3.2 and tp5.0
PIP uses image website to solve the problem of slow network speed
Technical dry goods | Bert model for the migration of mindspore NLP model - text matching task (2): training and evaluation
My touch screen production "brief history" 2
Install cross compiler arm none liunx gnueabihf
E: 无法定位软件包 ros-melodic-desktop-full
Precautions for opensips and TLS SIP trunk docking
Are you still watching the weather forecast on TV?
C language learning notes (mind map)
【踩坑系列】mysql 修改root密码失败
LwIP learning socket (API)
【cocos creator】获取资源uuid