Flip Game
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 40632   Accepted: 17647

Description

Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the other one is black and each piece is lying either it's black or white side up. Each round you flip 3 to 5 pieces, thus changing the color of their upper side from black to white and vice versa. The pieces to be flipped are chosen every round according to the following rules: 
  1. Choose any one of the 16 pieces.
  2. Flip the chosen piece and also all adjacent pieces to the left, to the right, to the top, and to the bottom of the chosen piece (if there are any).

Consider the following position as an example:

bwbw 
wwww 
bbwb 
bwwb 
Here "b" denotes pieces lying their black side up and "w" denotes pieces lying their white side up. If we choose to flip the 1st piece from the 3rd row (this choice is shown at the picture), then the field will become:

bwbw 
bwww 
wwwb 
wwwb 
The goal of the game is to flip either all pieces white side up or all pieces black side up. You are to write a program that will search for the minimum number of rounds needed to achieve this goal. 

Input

The input consists of 4 lines with 4 characters "w" or "b" each that denote game field position.

Output

Write to the output file a single integer number - the minimum number of rounds needed to achieve the goal of the game from the given position. If the goal is initially achieved, then write 0. If it's impossible to achieve the goal, then write the word "Impossible" (without quotes).

Sample Input

bwwb
bbwb
bwwb
bwww

Sample Output

4

Source

题目地址:http://poj.org/problem?id=1753

#include<stdio.h>
#include<string.h>
#include<iostream> #include<stdlib.h> using namespace std; #define N 7 bool mat[N][N], flag;
int deep; int dx[] = {, -, , , };
int dy[] = {, , , -, }; void Init()
{
char c;
for(int i = ; i <= ; i++)
{
for(int j = ; j <= ; j++)
{
scanf(" %c",&c);
if(c == 'b')
{
mat[i][j] = ;
}
else
{
mat[i][j] = ;
}
}
}
}
void Print()
{
for(int i = ; i <= ; i++)
{
for(int j = ; j <= ; j++)
printf("%d", mat[i][j]);
printf("\n");
}
} void Change(int x, int y)
{
int next_x, next_y;
for(int k = ; k < ; k++)
{
next_x = x + dx[k];
next_y = y + dy[k];
//if(next_x >= 1 && next_x <=4 && next_y >= 1 && next_y <=4)
//{
mat[next_x][next_y] = !mat[next_x][next_y];
//}
}
}
void Flip(int x)
{
switch(x) //1~16种case代表4*4的16个格子
{ case : Change(,); break;
case : Change(,); break;
case : Change(,); break;
case : Change(,); break; case : Change(,); break;
case : Change(,); break;
case : Change(,); break;
case : Change(,); break; case : Change(,); break;
case : Change(,); break;
case : Change(,); break;
case : Change(,); break; case : Change(,); break;
case : Change(,); break;
case : Change(,); break;
case : Change(,); break;
}
}
bool Result()
{
for(int i = ; i <= ; i++)
{
for(int j = ; j <= ; j++)
if(mat[i][j] != mat[][])
return false;
}
return true;
}
void Dfs(int x, int dp)
{ if(flag || dp > deep || x>) return;
//printf("DFS(%d, %d)\n", x, dp); Flip(x);
//Print();printf("---------\n");
if(dp == deep)
{
flag = (flag || Result());
if(flag)
{
//printf("OK!!!!!!!!!!!!!!");
//exit(0);
goto A;
} }
Dfs(x+, dp+);
Flip(x);
//Print();printf("---------\n");
Dfs(x+, dp);
A: return;
} int main()
{
//每个棋子最多翻一次(其实是奇数次,但是没意义),翻偶数次和没翻一样,所以每个棋子就两种状态,翻或者不翻
//所以一共就有2^16次方种可能,枚举这些可能就行了,DFS
//从0到16,如果16还找不到那就是Impossible
int a, b;
Init();
//Print(); flag = false;
if(Result())
{
cout<<<<endl;
return ;
} for(deep = ; deep <= ; deep++)
{
//cout<<"deep = "<<deep<<endl;
Dfs(, );
if(flag) break;
} if(flag) cout<<deep<<endl;
else cout<<"Impossible"<<endl; // while(cin>>a)
// {
// Flip(a);
// Print();
// } return ;
}
/*
bwwb
bbwb
bwwb
bwww wwww
wwww
wwww
wwww bbbw
wbww
wwww
wwww wwww
wwww
wwwb
wwbb bbww
bwww
wwww
wwww wwbw
bbww
wwww
wwww
*/

POJ 1753 (枚举+DFS)的更多相关文章

  1. POJ 3050 枚举+dfs+set判重

    思路: 枚举+搜一下+判个重 ==AC //By SiriusRen #include <set> #include <cstdio> using namespace std; ...

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

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

  3. [ACM训练] 算法初级 之 基本算法 之 枚举(POJ 1753+2965)

    先列出题目: 1.POJ 1753 POJ 1753  Flip Game:http://poj.org/problem?id=1753 Sample Input bwwb bbwb bwwb bww ...

  4. 枚举 POJ 1753 Flip Game

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

  5. POJ 2965 The Pilots Brothers' refrigerator【枚举+dfs】

    题目:http://poj.org/problem?id=2965 来源:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=26732#pro ...

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

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

  7. poj 1753 2965

    这两道题类似,前者翻转上下左右相邻的棋子,使得棋子同为黑或者同为白.后者翻转同行同列的所有开关,使得开关全被打开. poj 1753 题意:有一4x4棋盘,上面有16枚双面棋子(一面为黑,一面为白), ...

  8. POJ 1222 POJ 1830 POJ 1681 POJ 1753 POJ 3185 高斯消元求解一类开关问题

    http://poj.org/problem?id=1222 http://poj.org/problem?id=1830 http://poj.org/problem?id=1681 http:// ...

  9. POJ.3172 Scales (DFS)

    POJ.3172 Scales (DFS) 题意分析 一开始没看数据范围,上来直接01背包写的.RE后看数据范围吓死了.然后写了个2^1000的DFS,妥妥的T. 后来想到了预处理前缀和的方法.细节以 ...

随机推荐

  1. 第002弹:Java 中的值传递和引用传递

    在 Java 的代码开发过程中,为了尽可能提高方法的复用性,明确方法的作用,同时防止一个方法内部过于臃肿的问题,往往会创建许多方法,那么不可避免地会涉及到参数传递的问题.通常来说,我们将 Java 中 ...

  2. iOS开发中六种手势识别

    iOS开发中手势识别有六种: 轻击手势(TapGestureRecognizer), 轻扫手势 (SwipeGestureRecognizer), 长按手势(LongPressGestureRecog ...

  3. 配置 MySQL 服务器容器

    本文介绍在单一宿主机上如何配置自动备份.建议使用两个容器,其中一个容器作为 MySQL 的服务器,用来处理数据:另一个容器用于自动备份.这样保证隔离,避免备份的容器影响到 MySQL Server 的 ...

  4. bzoj1566【Noi2009】管道取珠

    题意:http://www.lydsy.com/JudgeOnline/problem.php?id=1566 两个栈不断pop,共C(n+m,n)种,ai表示每个相同序列的方案数,求∑(ai^2) ...

  5. docker (centOS 7) 使用笔记1

    1. docker配置 初次在安装完docker后,初始化配置 copy默认的docker.service后,重启服务,会在/etc/systemd/system/multi-user.target. ...

  6. Java24种设计模式的优点、缺点和适用环境总结

    一.7个常用的面向对象设计原则 1.单一职责原则: 它是实现高内聚.低耦合的指导方针:一个对象应该只包含单一的职责,并且该职责被完整的封装在一个类中: 2.开闭原则: 指软件实体应尽量在不改变原有的代 ...

  7. [BZOJ2045]双亲数(莫比乌斯反演)

    双亲数 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 959  Solved: 455[Submit][Status][Discuss] Descri ...

  8. LOJ#2304. 「NOI2017」泳池

    $n \leq 1e9$底边长的泳池,好懒啊泥萌自己看题吧,$k \leq 1000$.答案对998244353取膜. 现在令$P$为安全,$Q$为危险的概率.刚好$K$是极其不好算的,于是来算$\l ...

  9. 模块化开发(seajs)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. 在 .Net Core xUnit test 项目中使用配置文件

    在对项目做集成测试的时候,经常会需要用到一些参数比如用户名密码等,这些参数不宜放在测试代码中.本文介绍一种方法:使用配置文件. 添加配置文件 在集成测试项目目录下新建文件:Configuration. ...