当前位置:网站首页>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;
}
边栏推荐
- Microsoft Security Response Center
- Unity XR实现交互(抓取,移动旋转,传送,射击)-Pico
- [cocos creator] get the resource UUID
- How can entrepreneurial teams implement agile testing to improve quality and efficiency? Voice network developer entrepreneurship lecture Vol.03
- [MySQL 11] how to solve the case sensitive problem of MySQL 8.0.18
- STM32F103 SPI (pit Diary)
- Go language foundation ----- 04 ----- closure, array slice, map, package
- Getting started with minicom
- Pat grade a 1029 median
- PostGIS space function
猜你喜欢
[MySQL 12] MySQL 8.0.18 reinitialization
How to configure GDAL under idea
Go language foundation ----- 01 ----- go language features
什么是数据类型?数据类型有什么用?
PAT甲级 1027 Colors in Mars
JS common basic case sorting (continuous update)
haproxy+keepalived集群搭建02
[MySQL 11] how to solve the case sensitive problem of MySQL 8.0.18
What is a data type? What is the use of data types?
Ventuz Foundation Series "one step at the door"
随机推荐
Static keyword
OSPF protocol summary
PHP common sorting algorithm
PAT甲级 1031 Hello World for U
Technical dry goods | Bert model for the migration of mindspore NLP model - text matching task (2): training and evaluation
Go language foundation ------17 ----- channel creation, read-write, security shutdown, multiplexing select
Pat grade a 1029 median
Uniapp learning records
Go language foundation ----- 13 ----- file
Redis batch startup and shutdown script
MaxCompute字符串分割函数-SPLIT_PART
E: 无法定位软件包 ros-melodic-desktop-full
Usage of (case, when) in PostgreSQL
【cocos creator】点击按钮切换界面
static关键字
PHP常用排序算法
[cocos creator] get the resource UUID
Getting started with minicom
Technology dry goods | Roberta of the migration of mindspore NLP model - emotion analysis task
多旅行商问题——公式和求解过程概述