1.链接地址:

http://bailian.openjudge.cn/practice/1753/

http://poj.org/problem?id=1753

2.题目:

总时间限制:
1000ms
内存限制:
65536kB
描述
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.

输入
The input consists of 4 lines with 4 characters "w" or "b" each that denote game field position.
输出
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).
样例输入
bwwb
bbwb
bwwb
bwww
样例输出
4
来源
Northeastern Europe 2000

3.思路:

4.代码:

 #include "assert.h"
#include <iostream>
#include <queue>
#include <cstring>
#include <cstdlib>
using namespace std; const int MAX_STATE = ;
const int ALL_WHILE_STATE = ;
const int ALL_BLACK_STATE = ;
const int WIDTH_OF_BOARD = ;
const int SIZE_OF_BOARD = WIDTH_OF_BOARD * WIDTH_OF_BOARD; // 4 * 4 int ConvertPieceColorToInt(char color)
{
switch(color)
{
case 'b':
return ;
case 'w':
return ;
}
} int FlipPiece(int state_id, int position)
{
state_id ^= ( << position); // up
if(position - >= )
state_id ^= ( << (position - ));
// down
if(position + < SIZE_OF_BOARD)
state_id ^= ( << (position + ));
// left
if(position % != )
state_id ^= ( << (position - ));
// right
if(position % != )
state_id ^= ( << (position + )); return state_id;
} int main()
{
int current_state_id = ;
int state[MAX_STATE];
queue<int> search_queue; memset(state, -, sizeof(state)); char color; for(int i = ; i < SIZE_OF_BOARD; ++i)
{
cin >> color;
current_state_id += ConvertPieceColorToInt(color) << i;
} if(current_state_id == ALL_WHILE_STATE
|| current_state_id == ALL_BLACK_STATE)
{
cout << "" << endl;
return ;
} state[current_state_id] = ;
search_queue.push(current_state_id); int next_state_id; while(!search_queue.empty())
{
current_state_id = search_queue.front();
search_queue.pop(); for(int i = ; i < SIZE_OF_BOARD; ++i)
{
next_state_id = FlipPiece(current_state_id, i);
if(next_state_id == ALL_WHILE_STATE
|| next_state_id == ALL_BLACK_STATE)
{
cout << state[current_state_id] + << endl;
return ;
}
assert(next_state_id < MAX_STATE);
if(state[next_state_id] == - /* not visited */)
{
state[next_state_id] = state[current_state_id] + ;
search_queue.push(next_state_id);
}
}
} cout << "Impossible" << endl;
return ;
}

OpenJudge/Poj 1753 Flip Game的更多相关文章

  1. 枚举 POJ 1753 Flip Game

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

  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(高斯消元+状压枚举)

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

  4. POJ 1753 Flip Game DFS枚举

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

  5. POJ 1753 Flip Game(状态压缩+BFS)

    题目网址:http://poj.org/problem?id=1753 题目: Flip Game Description Flip game is played on a rectangular 4 ...

  6. poj 1753 Flip Game 枚举(bfs+状态压缩)

    题目:http://poj.org/problem?id=1753 因为粗心错了好多次……,尤其是把1<<15当成了65535: 参考博客:http://www.cnblogs.com/k ...

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

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

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

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

  9. POJ 1753 Flip Game(bfs+位压缩运算)

    http://poj.org/problem?id=1753 题意:一个4*4的棋盘,只有黑和白两种棋子,每次翻转一个棋子,并且其四周的棋子也跟着翻转,求棋盘全为黑或全为白时所需的最少翻转次数. 思路 ...

随机推荐

  1. cocos2d-x 手势之简单实现

    转自:http://blog.sina.com.cn/s/blog_61ece099010187tl.html 手势之前也发过一篇,但是我感觉那个还不够轻巧. 而且大多数游戏里面不会有那么复杂的手势, ...

  2. nape.dynamics.InteractionGroup

    (转载http://tomyail.com/blog/1123) 说明: Filter只是Shape的属性,Nape为Interactor类提供了group属性,这个属性是一个InteractionG ...

  3. 从零开始学android开发-四大组件之一 Activity

    1.Activity是Android四大组件(Application Components)之一,简单来说Activity就是平常所见到的用户界面,一般情况下,一个Activity所占的窗口是满屏的, ...

  4. 用JAVA写一个函数,功能例如以下: 随意给定一组数, 找出随意数相加之后的结果为35(随意设定)的情况

    用JAVA写一个函数.功能例如以下:随意给定一组数,比如{12,60,-8,99,15,35,17,18},找出随意数相加之后的结果为35(随意设定)的情况. 能够递归算法来解: package te ...

  5. hibernate 使用C3P0数据源

    1.导入jar包: hibernate-release-4.3.5.Final/lib/optional/*.jar 2.增加配置: <!-- 配置 C3P0 数据源 --> <pr ...

  6. 【《Objective-C基础教程 》笔记ch05】(六)OC中的复合机制Composition

     1.复合通过包括作为实例变量的的对象指针实现的.        @interface Unicycle : NSObject        {           Pedal*pedal;     ...

  7. ios中UIButton选中状态切换

    关于UIButton的事件枚举有许多,平时用的少所以很多的都不是很清楚,今天了解了下,看了以前的代码,觉得在UIButton选中时操作写了许多冗余代码,而忽略了UIButton一个很重要的属性,如下: ...

  8. 管道技巧-while read line

    http://blog.csdn.net/hunanchenxingyu/article/details/9998089

  9. [Effective C++ --018]让接口容易被正确使用,不易被误用

    □第一节 什么是接口?什么是接口?百度百科的解释:两个不同系统(或子程序)交接并通过它彼此作用的部分.接口的概念贯穿于整个软件开发过程中,本文讨论的接口概念仅局限于以C++实现的class,funct ...

  10. 文件和目录之stat、fstat和lstat函数

    #include <sys/stat.h> int stat( const char *restrict pathname, struct stat *restrict buf ); in ...