- P2622 关灯问题II
- ^ :按位运算符 异或 0 ^ 1 得 1 1 ^ 1 得 0 0 ^ 0 得 0 1 ^ 0 得 1同为0异为1
- | :当|和||的两端表达式同时为假时
- << :空缺补0,被移除的高位丢弃,空缺位补0,比如32位二进制 0000....00011左移3位变成了0...00011xxx,则xxx补足000即可。规律左移是基数乘以2的移位幂次方,
-
#include<bits/stdc++.h> using namespace std; #define maxn 111 int n,m,a[maxn][maxn*10]; bool vis[maxn*10000]; struct node { int num,step; } top,temp; void spfa() { queue<node>q; q.push((node) { (1<<n)-1,0 }); vis[(1<<n)-1]=1; while(!q.empty()) { top=q.front(); q.pop(); if(top.num==0) { cout<<top.step<<endl; return ; } for(int i=1; i<=m; i++) { temp.num=top.num; for(int j=1; j<=n; j++) { if(a[i][j]==1&&(temp.num&(1<<(j-1)))) temp.num^=1<<(j-1); else if(a[i][j]==-1&&!(temp.num&(1<<(j-1)))) temp.num|=1<<(j-1); } if(!vis[temp.num]) { vis[temp.num]=1; temp.step=top.step+1; q.push(temp); } } } cout<<-1<<endl; return ; } int main() { cin>>n>>m; for(int i=1; i<=m; i++) for(int j=1; j<=n; j++) cin>>a[i][j]; spfa(); return 0; }
P2622 关灯问题II - 二进制状压
猜你喜欢
转载自blog.csdn.net/BePosit/article/details/82817963
今日推荐
周排行