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. Runtime 10种用法

    来源:haojingxue_iOS 链接:http://www.jianshu.com/p/3182646001d1 阅读了多篇运行时的文章,感觉都很不错,从几篇文章里面提取一些个人认为比较重要的,偏 ...

  2. java异步任务处理

    1.场景 最近做项目的时候遇到了一个小问题:从前台提交到服务端A,A调用服务端B处理超时,原因是前端一次请求往db插1万数据,插完之后会去清理缓存.发送消息. 服务端的有三个操作 a.插DB b.清理 ...

  3. Oracle DataGuard数据备份方案详解

    Oracle DataGuard是一种数据库级别的HA方案,最主要功能是冗灾.数据保护.故障恢复等. 在生产数据库的"事务一致性"时,使用生产库的物理全备份(或物理COPY)创建备 ...

  4. 【36】绝不重新定义继承而来的non-virtual方法

    1.绝不重新定义继承而来的non-virtual方法,为什么? 首先想想,non-virtual方法是干什么的?也就是说,它的使用场景.父类的non-virtual方法,其实就是告诉子类,继承实现,子 ...

  5. 从零开始学android开发-Json转换利器Gson之实例

    Gson 是 Google 提供的用来在 Java 对象和 JSON 数据之间进行映射的 Java 类库.可以将一个 JSON 字符串转成一个 Java 对象,或者反过来. jar和源码下载地址: h ...

  6. 操作BLOB、CLOB、BFILE

    BFILE        二进制文件,存储在数据库外的操作系统文件,只读的.把此文件当二进制处理. BLOB        二进制大对象.存储在数据库里的大对象,一般是图像声音等文件. CLOB    ...

  7. js整理常用方法

    javascript对象合并或追加属性的方法 function objMerger(obj1, obj2){ for(var r in obj2){ //eval("obj1."+ ...

  8. multi-threads synchronization use conditional mutex

    #include <pthread.h> int thread_flag; pthread_cond_t thread_flag_cv; pthread_mutex_t thread_fl ...

  9. JAVA白盒安全测试需要关注的API

    JAVA白盒安全测试需要关注的APIhttp://blog.csdn.net/testing_is_believing/article/details/19502167

  10. java基础学习总结一(java语言发展历史、jdk的下载安装以及配置环境变量)

    最近一段时间计划复习一下java基础知识,使用的视频课程是尚学堂高淇老师的,上课过程中的心得体会直接总结一下,方便以后复习. 一:计算机语言的发展 1:机器语言,最原始的语言,主要有“01”构成,最早 ...