(转)

感谢大神的代码,加了自己读代码时加的注释,可能更好看一点。

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
#include <stempempdio.h>const int inf=9999999;
char s[10];
int map[10][10],i,j;
int ans=inf;int panduan()
{int x=map[0][0];for (i=0; i<4; i++){for (j=0; j<4; j++){if (map[i][j]!=x)return 0;}}return 1;
}void fan (int x,int y)
{map[x][y]=!map[x][y];if (x - 1 >= 0)map[x-1][y]=!map[x-1][y];if (x + 1 < 4)map[x+1][y]=!map[x+1][y];if (y - 1 >= 0)map[x][y-1]=!map[x][y-1];if (y + 1 < 4)map[x][y+1]=!map[x][y+1];
}int dfs (int x,int y,int t)
{if (panduan())//先判断是不是完活状态,完活就退出本状态{if (ans > t)ans = t ;return 0;}if (x >= 4 || y >= 4)return 0;else
    {
        int nx,ny;      nx = (x + 1)%4;//应该是控制下一步的位移
        ny = y + ( x + 1 ) / 4;//同上      dfs (nx,ny,t);
        fan (x,y);//把自己翻了       dfs (nx,ny,t+1);
        fan (x,y);//把自己翻回来
    }return 0;
}int main ()
{for (i=0; i<4; i++){scanf ("%s",s);for (j=0; j<4; j++){if (s[j]=='b')map[i][j]=0;elsemap[i][j]=1;}}dfs (0,0,0);if (ans == inf )printf ("Impossible\n");else printf ("%d\n",ans);return 0;
}

自己打了一遍发现这个做法和普通的模板的题(油田)的不同在于这个需要的顺序非常关键,像油田那题需要定义一个走步的数组dx[],dy[],而这个则是不断往右走(模4)再往下走(y+1),这样控制结束就非常简(bao)单(li)。

自己打的:

/*
ID: chun SU
TASK: test
LANG: C++
*/
#include <fstream>
#include<stdio.h>
#include<iostream>
//#include<bits/stdc++.h>
#include<string>
//#define local
using namespace std;
int temp[4][4];
bool vis[4][4]={false};
string s;
#define inf 0x3f3f3f
int ans=inf;
int  judge()
{int x = temp[0][0];for(int i=0;i<4;i++)for(int j=0;j<4;j++){if(temp[i][j]!=x)return 0;}return 1;
}
int dx[]={-1,1,0,0};
int dy[]={0,0,1,-1};
void fan(int x,int y)
{temp[x][y]=!temp[x][y];if (x - 1 >= 0)temp[x-1][y]=!temp[x-1][y];if (x + 1 < 4)temp[x+1][y]=!temp[x+1][y];if (y - 1 >= 0)temp[x][y-1]=!temp[x][y-1];if (y + 1 < 4)temp[x][y+1]=!temp[x][y+1];
}
void dfs(int x,int y,int t)
{if(judge()){ans=min(ans,t);return ;}if(y>=4) return ;else{
//      for(int k=0;k<4;k++)
//      {int nx = (x + 1)%4;//应该是控制下一步的位移int ny = y + ( x + 1 ) / 4;//同上// int  nx=x+dx[k];//    int ny=y+dy[k];
//          if(nx>=0&&ny>=0&&nx<4&&ny<4/*&&!vis[nx][ny]*/)
//          {dfs(nx,ny,t);fan(x,y);//   vis[x][y]=true;dfs(nx,ny,t+1);fan(x,y);// vis[x][y]=false;
//          }
//      }}return ;
}
int main()
{
#ifdef local
freopen("C:\\Users\\苏淳sv\\Desktop\\输入.txt","r",stdin);
#else
ofstream fout ("gift1.out");
ifstream fin ("gift1.in");
#endif
for(int i=0;i<4;i++)
{//scanf("%s",s);cin>>s;for(int j=0;j<s.size();j++){if(s[j]=='b') temp[i][j]=1;else temp[i][j]=0;}s.clear();
}
dfs(0,0,0);
if (ans == inf )printf ("Impossible\n");else printf ("%d\n",ans);return 0;
}

POJ1753 filp game相关推荐

  1. Metro Win8风格的按钮(Filp翻转)

    Metro Win8风格的按钮(Filp翻转) 原文:Metro Win8风格的按钮(Filp翻转) 原地址->http://www.cnblogs.com/yk250/p/5661093.ht ...

  2. POJ1753 棋盘翻转(位压缩+广度优先搜索)

    http://poj.org/problem?id=1753 题目大意:有一个4*4的方格,每个方格中放一粒棋子,这个棋子一面是白色,一面是黑色.游戏规则为每次任选16颗中的一颗,把选中的这颗以及它四 ...

  3. poj1753 解题思路

    poj1753属于枚举类型的题目,思路很简单,主要把握住两个点: · 1.每个点只有两种情况,翻0次和翻1次,翻偶数次是没有意义的: · 2.哪个点先翻哪个点后翻都是一样的. 明白了以上的基本要点,就 ...

  4. POJ1753(枚举)

    POJ1753(枚举) Flip Game Description Flip game is played on a rectangular 4x4 field with two-sided piec ...

  5. POJ1753 Flip Game题解

    题目大意: 在一个4*4的方格中有16个双面棋子他们分别是黑面和白面,我们可以选择任意一颗棋子进行翻转,翻转后其上下左右的棋子也会被翻转,要求我们对棋子进行翻转求出棋子全部翻为黑色或者全部翻为白色的最 ...

  6. OpenCV学习笔记(十)——图像卷积(cv.filter2D()、矩阵旋转cv.filp())

    目录 1 图像卷积过程 2 cv.filter2D() 3 cv.filp() 卷积运算在信号处理中十分常见,而图像信息可以看成一种信号.例如,图像的每一行可以看出测量亮度变化的信号,而每一列可以看作 ...

  7. Vjudge攻略——POJ1753

    POJ1753 POJ1753是一个点灯游戏,注意每个点最多只能被点一次(多点则还原),然后难点就在如何每次从16个点中取出N个点(递归实现). #include<iostream> us ...

  8. php扩展1:filp/whoops(用于调试,方便定位错误点)

    一.composer下载filp/whoops: 1.在composer.json中添加:"filp/whoops": "*",如下所示: 2.执行compos ...

  9. matlab学习笔记11_3高维数组操作 filp, shiftdim, size, permute, ipermute

    一起来学matlab-matlab学习笔记11 11_3 高维数组处理和运算 filp, shiftdim, size, permute, ipermute 觉得有用的话,欢迎一起讨论相互学习~ fi ...

最新文章

  1. python好学-html和Python哪个好学?
  2. 【数论】GCD SUM(P2398)
  3. PyTorch | 保存和加载模型教程
  4. 【进阶篇】Vue Devtools——vue开发调试神器
  5. extjs_04_grid(弹出窗口amp;行编辑器 CRUD数据)
  6. 28 POSIX Threads
  7. 【剑指Offer学习】【面试题40:数组中仅仅出现一次的数字】
  8. 44 年前的今天,改变世界的 TA 诞生了!
  9. AutopilotSim2驾驶模拟器使用
  10. SQL语句关于数据库安全性
  11. 数据库之互联网常用架构方案一览
  12. Android-Adapter-View复用机制
  13. 匹配网页里的zip_ZIP 也能边下载边解压?流式解压技术揭秘
  14. 【渝粤题库】陕西师范大学200081中国古代文学(一)作业(高起本、高起专)
  15. 以太坊地址和公钥_《每日一课》第九课:什么是钱包、钱包地址、私钥、公钥?...
  16. python 模拟点击第三方软件_使用Python模拟腾讯第三方认证-篇2
  17. 代码练习——数组_实战
  18. 关于Java平台无关性你该知道这些
  19. 显示器还有卖的吗_显示器不够好,你确定修的片能好看? | 摄影早自习第1365天...
  20. Linux下Vim的常用命令操作大全

热门文章

  1. 微信小程序 自定义组件之父向子传递
  2. 2022年化工自动化控制仪表考试总结及化工自动化控制仪表模拟考试题
  3. 央行数据 App - 国债 逆回购 LPR M2/M1/M0 央行负债表 Shibor 票据 MLF 查询分析 央行公开市场业务交易数据
  4. 为什么我不能提前离开
  5. 大搞国产化,细看国产数据库
  6. 三菱服务器无法在线,三菱GX Works2和iQ Works常见问题
  7. tp5之学习时长排行榜
  8. Elasticsearch磁盘使用量优化
  9. 鲜花销售系统 web jspservletTomcat
  10. 2019 最新java面试题(技术面试)