当前位置:网站首页>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;
}
边栏推荐
- Pat class a 1030 travel plan
- Project experience sharing: handwritten Chinese character recognition based on Shengsi mindspire
- Ventuz Foundation Series "one step at the door"
- idea取消引用顯示效果
- What is definition? What is a statement? What is the difference between them?
- 【LeetCode】2. Valid parentheses · valid parentheses
- Iterm2设置
- Uniapp learning records
- [cocos creator] Click the button to switch the interface
- Oracle queries grouped by time
猜你喜欢
Oracle queries grouped by time
Go language foundation ----- 05 ----- structure
密西根大学张阳教授受聘中国上海交通大学客座教授(图)
多旅行商问题——公式和求解过程概述
【LeetCode】3. Merge two sorted lists · merge two ordered linked lists
【cocos creator】点击按钮切换界面
Go language foundation ----- 10 ----- string related operations (operation function, string conversion)
Worldview satellite remote sensing image data / meter resolution remote sensing image
Pat grade a 1027 colors in Mars
Ventuz Foundation Series "one step at the door"
随机推荐
PHP微信抢红包的算法
OSPF protocol summary
Quality blog——
Go language foundation ----- 07 ----- method
haproxy+keepalived集群搭建02
Differences between tp3.2 and tp5.0
vcs import src < ros2. Repos failed
Docker installs MySQL and successfully uses Navicat connection
密西根大学张阳教授受聘中国上海交通大学客座教授(图)
Usage of requests module
[MySQL 14] use dbeaver tool to remotely backup and restore MySQL database (Linux Environment)
Unity XR realizes interaction (grasping, moving, rotating, transmitting, shooting) -pico
PAT甲级 1027 Colors in Mars
Static keyword
Go language foundation ------ 12 ------ JSON
【cocos creator】获取资源uuid
华为交换机:配置telnet和ssh、web访问
Go language foundation ------17 ----- channel creation, read-write, security shutdown, multiplexing select
P2704 [NOI2001] 炮兵阵地(状压dp)
Go language foundation ----- 04 ----- closure, array slice, map, package