当前位置:网站首页>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;
}边栏推荐
- Harmonyos third training notes
- Structure of golang
- Microsoft Security Response Center
- [end of 2021] National Meteorological Short Video (Kwai, Tiktok) influence list in December
- 【LeetCode】3. Merge two sorted lists · merge two ordered linked lists
- 华为交换机Console密码重置、设备初始化、默认密码
- P1896 [SCOI2005] 互不侵犯(状压dp)
- Pycharm remote ssh pyenv error: pydev debugger: warning: trying to add breakpoint to file that does
- *p++、*++p、++*p、(*p)++
- 在浏览器输入url后执行什么
猜你喜欢

LwIP learning socket (application)

Ventuz Foundation Series "one step at the door"
![[end of 2021] National Meteorological Short Video (Kwai, Tiktok) influence list in December](/img/51/81ceaf8746ec7455ea8abf9f038e81.jpg)
[end of 2021] National Meteorological Short Video (Kwai, Tiktok) influence list in December

I want to do large screen data visualization application feature analysis

Redis batch startup and shutdown script
![[MySQL 11] how to solve the case sensitive problem of MySQL 8.0.18](/img/9b/db5fe1a37e0de5ba363f9e108310a5.png)
[MySQL 11] how to solve the case sensitive problem of MySQL 8.0.18

freetype库的移植

璞华PLM为全场景产品生命周期管理赋能,助力产品主线的企业数字化转型

一个实习生的CnosDB之旅

haproxy+keepalived搭建01
随机推荐
haproxy+keepalived搭建01
华为S5700交换机初始化和配置SSH和TELNET远程登录方法
I want to do large screen data visualization application feature analysis
[MySQL 12] MySQL 8.0.18 reinitialization
Huawei s5700 switch initialization and configuration Telnet, SSH user methods
PDO and SDO concepts
华为S5700交换机初始化和配置telnet,ssh用户方法
Redis批量启停脚本
Pat class a 1031 Hello world for u
2020-12-12
An intern's journey to cnosdb
yarn link 是如何帮助开发者对 NPM 包进行 debug 的?
go语言-循环语句
How to configure GDAL under idea
华为交换机基础配置(telnet/ssh登录)
多旅行商问题——公式和求解过程概述
Pycharm remote ssh pyenv error: pydev debugger: warning: trying to add breakpoint to file that does
An article for you to understand - Manchester code
Register keyword
【LeetCode】4. Best time to buy and sell stock