https://vjudge.net/problem/POJ-1753

题意

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

分析

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

#include<iostream>
#include<cmath>
#include<cstring>
#include<queue>
#include<vector>
#include<cstdio>
#include<algorithm>
#include<map>
#include<set> #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 <class T>
void test(T a){cout<<a<<endl;}
template <class T,class T2>
void test(T a,T2 b){cout<<a<<" "<<b<<endl;}
template <class T,class T2,class T3>
void test(T a,T2 b,T3 c){cout<<a<<" "<<b<<" "<<c<<endl;}
template <class T>
inline bool scan_d(T &ret){
char c;int sgn;
if(c=getchar(),c==EOF) return ;
while(c!='-'&&(c<''||c>'')) c=getchar();
sgn=(c=='-')?-:;
ret=(c=='-')?:(c-'');
while(c=getchar(),c>=''&&c<='') ret = ret*+(c-'');
ret*=sgn;
return ;
}
const int N = 1e6+;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const ll mod = ;
int T; void testcase(){
printf("Case %d:",++T);
} const int MAXN = 5e5+ ;
const int MAXM = ;
const double eps = 1e-;
const double PI = acos(-1.0);
int n;
int st[] = {0x13,0x27,0x4e,0x8c,0x131,0x272,0x4e4,0x8c8,0x1310,0x2720,0x4e40,0x8c80,0x3100,0x7200,0xe400,0xc800};
int po[]={,,,,,,,,,,,,,,,};
int g[][]; void work(){
char s[];
int state=;
for(int i=;i<;i++){
scanf("%s",s);
for(int j=;j<;j++){
if(s[j]=='b') state += po[*i+j];
}
}
int ans=inf;
for(int i=;i<(<<);i++){
int cnt = ;
int temp = state;
for(int j=;j<;j++){
if(i & po[j]){
temp ^= st[j];
cnt++;
}
}
if(temp== || temp == ){
if(ans>cnt){
ans=cnt;
}
} }
if(ans==inf) puts("Impossible");
else cout<<ans<<endl;
return;
}
int main() {
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif // LOCAL
// init();
work();
return ;
}

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

#include<iostream>
#include<cmath>
#include<cstring>
#include<queue>
#include<vector>
#include<cstdio>
#include<algorithm>
#include<map>
#include<set> #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 <class T>
void test(T a){cout<<a<<endl;}
template <class T,class T2>
void test(T a,T2 b){cout<<a<<" "<<b<<endl;}
template <class T,class T2,class T3>
void test(T a,T2 b,T3 c){cout<<a<<" "<<b<<" "<<c<<endl;}
template <class T>
inline bool scan_d(T &ret){
char c;int sgn;
if(c=getchar(),c==EOF) return ;
while(c!='-'&&(c<''||c>'')) c=getchar();
sgn=(c=='-')?-:;
ret=(c=='-')?:(c-'');
while(c=getchar(),c>=''&&c<='') ret = ret*+(c-'');
ret*=sgn;
return ;
}
const int N = 1e6+;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const ll mod = ;
int T; void testcase(){
printf("Case %d:",++T);
} const int MAXN = 5e5+ ;
const int MAXM = ;
const double eps = 1e-;
const double PI = acos(-1.0);
int n;
int g[][];
bool f;
bool check(){
int t = g[][];
for(int i=;i<;i++)
for(int j=;j<;j++)
if(t!=g[i][j])
return false;
return true;
} void flip(int x,int y){
g[x][y] = -g[x][y];
if(x->=) g[x-][y] = -g[x-][y];
if(y->=) g[x][y-] = -g[x][y-];
if(x+<) g[x+][y] = -g[x+][y];
if(y+<) g[x][y+] = -g[x][y+];
} void dfs(int x,int y,int state){
if(state==){
f = check();
return;
}
if(f||y>) return;
flip(x,y);
if(x<) dfs(x+,y,state-);
else dfs(,y+,state-);
flip(x,y);
if(x<) dfs(x+,y,state);
else dfs(,y+,state);
return;
}
void work(){
char s[];
f=false;
for(int i=;i<;i++){
scanf("%s",s);
for(int j=;j<;j++){
if(s[j]=='b') g[i][j]=;
else g[i][j]=;
}
} for(n=;n<=;n++){
dfs(,,n);
if(f) break;
}
if(f) cout<<n<<endl;
else puts("Impossible");
return;
}
int main() {
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif // LOCAL
// init();
work();
return ;
}

POJ - 1753 Flip Game(状压枚举)的更多相关文章

  1. POJ 1753 Flip Game(二进制枚举)

    题目地址链接:http://poj.org/problem?id=1753 题目大意: 有4*4的正方形,每个格子要么是黑色,要么是白色,当把一个格子的颜色改变(黑->白或者白->黑)时, ...

  2. POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37427   Accepted: 16288 Descr ...

  3. 枚举 POJ 1753 Flip Game

    题目地址:http://poj.org/problem?id=1753 /* 这题几乎和POJ 2965一样,DFS函数都不用修改 只要修改一下change规则... 注意:是否初始已经ok了要先判断 ...

  4. [POJ1681]Painter's Problem(高斯消元,异或方程组,状压枚举)

    题目链接:http://poj.org/problem?id=1681 题意:还是翻格子的题,但是这里有可能出现自由变元,这时候枚举一下就行..(其实这题直接状压枚举就行) /* ━━━━━┒ギリギリ ...

  5. POJ 1324 Holedox Moving (状压BFS)

    POJ 1324 Holedox Moving (状压BFS) Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 18091 Acc ...

  6. HDU2489【状压枚举】

    题意: 给你n个点的图,然后让你在图里挑m个点,达到sumedge/sumnode最小 思路: 由于数据范围小,状压枚举符合m个点的状态,我是用vactor存了结点位置,也记录了结点的sum值,然后跑 ...

  7. POJ3734【状压枚举】

    题意: 给你两个01矩阵,去掉矩阵B的某些行和某些列,问处理后的矩阵B能否变成矩阵A: 思路: 数据较小,状压枚举B矩阵列的数量=A矩阵列的数量时的状态,然后搞定了列,贪心判断B矩阵的行就好了: #i ...

  8. POJ 1753 Flip Game(高斯消元+状压枚举)

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 45691   Accepted: 19590 Descr ...

  9. POJ 3254 - Corn Fields - [状压DP水题]

    题目链接:http://poj.org/problem?id=3254 Time Limit: 2000MS Memory Limit: 65536K Description Farmer John ...

  10. POJ 3254 Corn Fields (状压dp)

    题目链接:http://poj.org/problem?id=3254 给你n*m的菜地,其中1是可以种菜的,而菜与菜之间不能相邻.问有多少种情况. 状压dp入门题,将可以种菜的状态用一个数的二进制表 ...

随机推荐

  1. Asp.net中汉字转换成为拼音

    1.应用场景 将汉字转换为拼音(eg:"我爱你"--->"WOAINI") 取各个汉字的首字母(eg:"我是中国人"--->&q ...

  2. 开源微信Http协议Sdk【实现登录/获取好友列表/修改备注/发送消息】

    基于微信Http协议封装的一个Sdk,目前实现了以下功能:. 1:扫码登录(检测二维码扫描状态) 2:获取最近联系人.群组.所有联系人 3:修改好友备注 4:给好友发送消息 暂且这么多,也没多余的时间 ...

  3. allegro对齐操作

    在placement  edit模式下 选中元件,右键对齐即可.

  4. [Latex] 所有字体embedded: Type3 PDF文档处理 / True Type转换为Type 1

    目录: [正文] Adobe Acrobat打印解决字体嵌入问题 [Appendix I] Type3转TRUE Type/Type 1 [Appendix II] TRUE Type转Type 1 ...

  5. 初识Redux Middleware

    前言 原先改变store是通过dispatch(action) = > reducer:那Redux的Middleware是什么呢?就是dispatch(action) = > reduc ...

  6. VGGNet论文翻译-Very Deep Convolutional Networks for Large-Scale Image Recognition

    Very Deep Convolutional Networks for Large-Scale Image Recognition Karen Simonyan[‡] & Andrew Zi ...

  7. 初始化Weex项目遇到的问题记录

    Weex 提供了一个命令行工具 weex-toolkit 来帮助开发者使用 Weex.它可以用来快速创建一个空项目.初始化 iOS 和 Android 开发环境.调试.安装插件等操作. 目前 weex ...

  8. 《Linux内核分析》第一周学习小结 计算机是如何工作的?

    <Linux内核分析>第一周.计算机是如何工作的? 20135204 郝智宇  一.存储程序计算机工作模型 1.      冯诺依曼体系结构: 数字计算机的数制采用二进制:计算机应该按照程 ...

  9. linux 常用命令-ps(process state)

    ps -ef | grep 端口号:查看某个端口的占用情况 ps -tunlp | grep 端口号:查看占用端口的进程名称

  10. vs安装体验

    鉴于vs都是英文,所以安装的时间实在是太长了,经过4个小时终于装完了. 首先要下载和安装Unit Test Generator.步骤为:tools->Extensions and Updates ...