博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ - 1753 Flip Game(状压枚举)
阅读量:7113 次
发布时间:2019-06-28

本文共 4467 字,大约阅读时间需要 14 分钟。

题意

4*4的棋盘,翻转其中的一个棋子,会带动邻接的棋子一起动。现要求把所有棋子都翻成同一种颜色,问最少需要几步。

 

分析

同一个棋子翻偶数次等于没有翻,翻奇数次就浪费步数,因此每个棋子最多翻一次,也就是说,答案最大就是16。故总状态数就是2^16,可以直接dfs暴力。还有另一种思路就是状态压缩,把棋盘压成16位的数字,翻转时采用异或操作,我们暴力枚举每个状态,即所有选择棋子的可能情况跑一遍,对于每一个棋子,对其能影响的位置可以预处理出来,这样就通过位运算来模拟翻转过程了,更具体的看代码。

 

 

#include
#include
#include
#include
#include
#include
#include
#include
#include
#define rep(i,e) for(int i=0;i<(e);i++)#define rep1(i,e) for(int i=1;i<=(e);i++)#define repx(i,x,e) for(int i=(x);i<=(e);i++)#define X first#define Y second#define PB push_back#define MP make_pair#define mset(var,val) memset(var,val,sizeof(var))#define scd(a) scanf("%d",&a)#define scdd(a,b) scanf("%d%d",&a,&b)#define scddd(a,b,c) scanf("%d%d%d",&a,&b,&c)#define pd(a) printf("%d\n",a)#define scl(a) scanf("%lld",&a)#define scll(a,b) scanf("%lld%lld",&a,&b)#define sclll(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)#define IOS ios::sync_with_stdio(false);cin.tie(0)using namespace std;typedef long long ll;template
void test(T a){cout<
<
void test(T a,T2 b){cout<
<<" "<<
void test(T a,T2 b,T3 c){cout<
<<" "<<<" "<
<
inline bool scan_d(T &ret){ char c;int sgn; if(c=getchar(),c==EOF) return 0; while(c!='-'&&(c<'0'||c>'9')) c=getchar(); sgn=(c=='-')?-1:1; ret=(c=='-')?0:(c-'0'); while(c=getchar(),c>='0'&&c<='9') ret = ret*10+(c-'0'); ret*=sgn; return 1;}const int N = 1e6+10;const int inf = 0x3f3f3f3f;const ll INF = 0x3f3f3f3f3f3f3f3fll;const ll mod = 1000000000;int T;void testcase(){ printf("Case %d:",++T);}const int MAXN = 5e5+10 ;const int MAXM = 150;const double eps = 1e-8;const double PI = acos(-1.0);int n;int st[16] = { 0x13,0x27,0x4e,0x8c,0x131,0x272,0x4e4,0x8c8,0x1310,0x2720,0x4e40,0x8c80,0x3100,0x7200,0xe400,0xc800};int po[16]={ 1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768};int g[5][5];void work(){ char s[6]; int state=0; for(int i=0;i<4;i++){ scanf("%s",s); for(int j=0;j<4;j++){ if(s[j]=='b') state += po[4*i+j]; } } int ans=inf; for(int i=0;i<(1<<16);i++){ int cnt = 0; int temp = state; for(int j=0;j<16;j++){ if(i & po[j]){ temp ^= st[j]; cnt++; } } if(temp==0 || temp == 65535){ if(ans>cnt){ ans=cnt; } } } if(ans==inf) puts("Impossible"); else cout<
<

 

深搜的做法,规定一定的搜索顺序,递归回溯。

#include
#include
#include
#include
#include
#include
#include
#include
#include
#define rep(i,e) for(int i=0;i<(e);i++)#define rep1(i,e) for(int i=1;i<=(e);i++)#define repx(i,x,e) for(int i=(x);i<=(e);i++)#define X first#define Y second#define PB push_back#define MP make_pair#define mset(var,val) memset(var,val,sizeof(var))#define scd(a) scanf("%d",&a)#define scdd(a,b) scanf("%d%d",&a,&b)#define scddd(a,b,c) scanf("%d%d%d",&a,&b,&c)#define pd(a) printf("%d\n",a)#define scl(a) scanf("%lld",&a)#define scll(a,b) scanf("%lld%lld",&a,&b)#define sclll(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)#define IOS ios::sync_with_stdio(false);cin.tie(0)using namespace std;typedef long long ll;template
void test(T a){cout<
<
void test(T a,T2 b){cout<
<<" "<<
void test(T a,T2 b,T3 c){cout<
<<" "<<<" "<
<
inline bool scan_d(T &ret){ char c;int sgn; if(c=getchar(),c==EOF) return 0; while(c!='-'&&(c<'0'||c>'9')) c=getchar(); sgn=(c=='-')?-1:1; ret=(c=='-')?0:(c-'0'); while(c=getchar(),c>='0'&&c<='9') ret = ret*10+(c-'0'); ret*=sgn; return 1;}const int N = 1e6+10;const int inf = 0x3f3f3f3f;const ll INF = 0x3f3f3f3f3f3f3f3fll;const ll mod = 1000000000;int T;void testcase(){ printf("Case %d:",++T);}const int MAXN = 5e5+10 ;const int MAXM = 150;const double eps = 1e-8;const double PI = acos(-1.0);int n;int g[5][5];bool f;bool check(){ int t = g[0][0]; for(int i=0;i<4;i++) for(int j=0;j<4;j++) if(t!=g[i][j]) return false; return true;}void flip(int x,int y){ g[x][y] = 1-g[x][y]; if(x-1>=0) g[x-1][y] = 1-g[x-1][y]; if(y-1>=0) g[x][y-1] = 1-g[x][y-1]; if(x+1<4) g[x+1][y] = 1-g[x+1][y]; if(y+1<4) g[x][y+1] = 1-g[x][y+1];}void dfs(int x,int y,int state){ if(state==0){ f = check(); return; } if(f||y>3) return; flip(x,y); if(x<3) dfs(x+1,y,state-1); else dfs(0,y+1,state-1); flip(x,y); if(x<3) dfs(x+1,y,state); else dfs(0,y+1,state); return;}void work(){ char s[6]; f=false; for(int i=0;i<4;i++){ scanf("%s",s); for(int j=0;j<4;j++){ if(s[j]=='b') g[i][j]=0; else g[i][j]=1; } } for(n=0;n<=16;n++){ dfs(0,0,n); if(f) break; } if(f) cout<
<

 

转载于:https://www.cnblogs.com/fht-litost/p/9160723.html

你可能感兴趣的文章
详解CALayer 和 UIView的区别和联系
查看>>
eclipse中报错:java.lang.OutOfMemoryError: Java heap space
查看>>
Ubuntu 16.04 grub rescue 模式下修复 grub
查看>>
【Spring】24、<load-on-startup>0</load-on-startup>配置
查看>>
L0 Regularization
查看>>
使用JDBC向Kudu表插入中文数据乱码(转载)
查看>>
spf13-vim安装与使用
查看>>
字体颜色代码表
查看>>
hdu 2156 分数矩阵
查看>>
android SQLite数据库应用于草稿箱
查看>>
Android 无缝换肤深入了解与使用
查看>>
Cordova快速开始(安卓篇)
查看>>
ActiveMQ
查看>>
聚类算法(kmeans)详解和python实现
查看>>
四种遍历方法你选哪个?
查看>>
LeetCode41.缺失的第一个正数 JavaScript
查看>>
Java设计模式五——单件模式
查看>>
奇怪的 Ruby
查看>>
79. Word Search
查看>>
【Android】RxJava的使用(四)线程控制 —— Scheduler
查看>>