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. poj 2251 搜索

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13923   Accepted: 5424 D ...

  2. BZOJ 1878: [SDOI2009]HH的项链 离线树状数组

    1878: [SDOI2009]HH的项链 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/p ...

  3. 【转贴】gdb中的信号(signal)相关调试技巧

    一篇不错的帖子,讲的是gdb中的信号(signal)相关调试技巧 转自Magic C++论坛  http://www.magicunix.com/index_ch.html  http://www.m ...

  4. yarn的基本组成和工作流程

    yarn是负责资源管理的,协调各个应用程序的资源使用情况 一.基本组成 yarn主要由以下几个部分组成 1.resourcemanager 主要负责资源的调度和应用程序的管理 (1)调度器 调度器是将 ...

  5. PHP代码安全学习笔记V1.0

    PHP代码安全学习笔记V1.0http://www.docin.com/p-778369487.html

  6. 自动化分析工具PSSDIAG

    微软有个内部工具--PSSDIAG,它能收集非常多的信息,仅需要简单的配置和操作步骤. 下载地址: http://diagmanager.codeplex.com 默认安装路径: C:\Program ...

  7. 命令行界面下用户和组管理之groupmod和groupdel的使用

    NAME    groupmod - modify a group definition on the system SYNOPSIS       groupmod [options] GROUP O ...

  8. nginx查看日志

    原文:nginx日志格式及自定义日志配置 nginx的log日志分为access log 和 error log 其中access log 记录了哪些用户,哪些页面以及用户浏览器.ip和其他的访问信息 ...

  9. FIO工具常用参数

    name 可能被用于覆盖作业的名称. filename fio 通常基于该作业名称,线程编号,构成一个文件名称和位置.如果您不想让线程之间的共享文件在一个作业或作业.指定文件名都以覆盖默认的. loc ...

  10. nodejs设置NODE_ENV环境变量

    看下app.js文件中的一部分代码,如下: //开发环境错误处理 // will print stacktrace if (app.get('env') === 'development') { ap ...