当前位置:网站首页>P2622 关灯问题II(状态压缩 搜索)
P2622 关灯问题II(状态压缩 搜索)
2022-07-03 07:54:00 【eva_can(not)survive】
关灯问题II - 洛谷https://www.luogu.com.cn/problem/P2622状态压缩的一个学习案例,一道很经典的题目。
#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;
}边栏推荐
- How can entrepreneurial teams implement agile testing to improve quality and efficiency? Voice network developer entrepreneurship lecture Vol.03
- 华为交换机Console密码重置、设备初始化、默认密码
- tp3.2和tp5.0的区别
- Huawei switch console password reset, device initialization, default password
- 微软安全响应中心
- s7700设备如何清除console密码
- 华为S5700交换机初始化和配置telnet,ssh用户方法
- E: 无法定位软件包 ros-melodic-desktop-full
- Go language foundation ----- 07 ----- method
- vcs import src < ros2. Repos failed
猜你喜欢

Harmonyos third training notes

一个实习生的CnosDB之旅

Go language foundation ----- 10 ----- string related operations (operation function, string conversion)

JS common basic case sorting (continuous update)

haproxy+keepalived搭建01

Unity XR realizes interaction (grasping, moving, rotating, transmitting, shooting) -pico

Go language foundation ----- 08 ----- interface
![[cocos creator] Click the button to switch the interface](/img/b8/f0fd54a2a197cbfd788990e2806b52.png)
[cocos creator] Click the button to switch the interface

Pat grade a 1029 median

Pat grade a 1027 colors in Mars
随机推荐
Getting started with minicom
Redis profile
Technical dry goods | Bert model for the migration of mindspore NLP model - text matching task (2): training and evaluation
regular expression
static关键字
PostGIS space function
Usage of requests module
Huawei s5700 switch initialization and configuration Telnet, SSH user methods
Go language foundation ----- 01 ----- go language features
Research shows that breast cancer cells are more likely to enter the blood when patients sleep
PAT甲级 1030 Travel Plan
Go language foundation ----- 19 ----- context usage principle, interface, derived context (the multiplexing of select can be better understood here)
s7700设备如何清除console密码
PAT甲级 1031 Hello World for U
register关键字
Go language - loop statement
PAT甲级 1029 Median
idea取消引用顯示效果
Go language foundation ------ 12 ------ JSON
jsutlis