Flip Game
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 27005   Accepted: 11694

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

 
 


 
 翻来覆去看了好几遍,看不懂就比着代码狂敲,不出两个小时Ok,然后过几天再敲一遍,绝对好使
 
这道题说白了就是遍历,关键是递归实现部分,逻辑思维有点差
 
分两步:    以当前棋子为足,
               1、向下走,步数+1;
               2、向下走,步数不变;
敬爱的毛主席说过一句话:从战略上藐视对手,从战术上重视对手;
          做题时 要从战略上藐视题目,从战术上重视题目;
         只要你自己能模拟出来,剩下的就是程序实现了,So Easy!!! 与热爱程序的朋友共勉。
#include<iostream>
using namespace std;
bool chess[][]={false};
bool flag;
int step;
int r[]={-,,,,};
int c[]={,,-,,}; bool judge_all()
{
int i,j;
for(i=;i<;i++)
for(j=;j<;j++)
if(chess[i][j]!=chess[][])
return false;
return true;
}
void flip(int row,int col)
{
int i;
for(i=;i<;i++)
chess[row+r[i]][col+c[i]]=!chess[row+r[i]][col+c[i]];
}
void dfs(int row,int col,int deep)
{
if(deep==step)
{
flag=judge_all();
return ;
}
if(flag||row==) return ;
flip(row,col);
if(col<)
dfs(row,col+,deep+);
else
dfs(row+,,deep+);
flip(row,col);
if(col<)
dfs(row,col+,deep);
else
dfs(row+,,deep);
return;
}
int main()
{
char temp;
int i,j;
for(i=;i<;i++)
for(j=;j<;j++)
{
cin>>temp;
if(temp=='b')
chess[i][j]=true;
}
for(step=;step<=;step++)
{
dfs(,,);
if(flag) break;
}
if(flag)
cout<<step<<endl;
else
cout<<"Impossible"<<endl;
return ;
}

poj Flip Game 1753 (枚举)的更多相关文章

  1. POJ 1753 Flip Game DFS枚举

    看题传送门:http://poj.org/problem?id=1753 DFS枚举的应用. 基本上是参考大神的.... 学习学习.. #include<cstdio> #include& ...

  2. POJ 1753 Flip Game (DFS + 枚举)

    题目:http://poj.org/problem?id=1753 这个题在開始接触的训练计划的时候做过,当时用的是DFS遍历,其机制就是把每一个棋子翻一遍.然后顺利的过了.所以也就没有深究. 省赛前 ...

  3. POJ 1753 Flip Game【枚举】

    题目链接: http://poj.org/problem?id=1753 题意: 由白块黑块组成的4*4方格,每次换一个块的颜色,其上下左右的块也会被换成相反的颜色.问最少换多少块,使得最终方格变为全 ...

  4. POJ 1753 Flip Game (枚举)

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26492   Accepted: 11422 Descr ...

  5. poj 1753 Flip Game(暴力枚举)

    Flip Game   Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 52279   Accepted: 22018 Des ...

  6. POJ 1753 (枚举+DFS)

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

  7. Poj(2784),二进制枚举最小生成树

    题目链接:http://poj.org/problem?id=2784 Buy or Build Time Limit: 2000MS   Memory Limit: 65536K Total Sub ...

  8. POJ - 3279 Fliptile (枚举)

    http://poj.org/problem?id=3279 题意 一个m*n的01矩阵,每次翻转(x,y),那么它上下左右以及本身就会0变1,1变0,问把矩阵变成全0的,最小需要点击多少步,并输出最 ...

  9. POJ 2912 - Rochambeau - [暴力枚举+带权并查集]

    题目链接:http://poj.org/problem?id=2912 Time Limit: 5000MS Memory Limit: 65536K Description N children a ...

随机推荐

  1. VPN 部署方案

    VPN 对比: OpenVPN: 客户端连接太麻烦,放弃 PPTP VPN:版本较高的苹果手机没有 PPTP VPN 的连接方式,放弃 L2TP VPN:支持所有平台,客户端连接容易,最终选择部署 L ...

  2. [LeetCode] Partition Equal Subset Sum 相同子集和分割

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  3. [LeetCode] Reverse Linked List II 倒置链表之二

    Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...

  4. javascript中的感叹号 "!"

    JavaScript中会经常遇到一个操作符:! 这是一个布尔操作符,用于将操作的值强制转换为布尔值并取反.常用场景如下: //条件判断中使用 var a; var b=null; if(!a){ co ...

  5. ViewController的生命周期分析和使用

    iOS的SDK中提供很多原生ViewController,大大提高了我们的开发效率,下面是我的一些经验. 一.结构 按结构可以对iOS的所有ViewController分成两类:1.主要用于展示内容的 ...

  6. sql查询慢优化

    SELECT g.goods_id, g.type_id, g.user_id, g.productname, g.img, g.intro, g.attr, u.companyname, u.enl ...

  7. Error 1606 Could Not Access Network Location %SystemDrive%/inetpub/wwwroot/ 的错误解决方法

    在卸载或者重安装Infragistics NetAdvantage时候提示如标题的错误 win7下 1.打开注册表 Regedit 2.找到HKEY_LOCAL_MACHINE/SOFTWARE/Mi ...

  8. UISearchController 的用法[点击搜索框,自动到顶部]

    //在ViewDidLoad里面如下代码 self.searchViewController = [[UISearchController alloc]initWithSearchResultsCon ...

  9. EF 二级缓存 EFSecondLevelCache

    EFSecondLevelCache ======= Entity Framework .x Second Level Caching Library. 二级缓存是一个查询缓存.EF命令的结果将存储在 ...

  10. layer.open打开iframe页面的调用父页面方法及关闭

    //调用父类方法 window.parent.exportData($('#shownum').val(),$('#splitstr').val()); //关闭iframe页面var index = ...