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. 用HTML5 Canvas 做擦除及扩散效果

    2013年的时候曾经使用canvas实现了一个擦除效果的需求,即模拟用户在模糊的玻璃上擦除水雾看到清晰景色的交互效果.好在2012年的时候学习HTML5的时候研究过canvas了,所以在比较短的时间内 ...

  2. HDU 5493 Queue 树状数组

    Queue Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5493 Des ...

  3. 05---JSON学习(Java)

    一.简介         JSON:JavaScript对象表示法(JavaScript object Notation)         JSON是存储和文本交换信息的语法         JSON ...

  4. node.js 针对不同的请求路径(url) 做出不同的响应

    边看这个边写的: http://wenku.baidu.com/link?url=C4yLe-TVH6060u_x4t34H3Ze8tjoL7HjJaKgH-TvHnEYl-T_gAMYwhmrCeM ...

  5. [008]C---gcc环境下的一个编译器版本问题

    最开始纯粹为了好玩,写了这样一段程序: ; i < ; i++); 然后欢乐的运行: gcc practise.c -o practise 我擦咧,出现了这个: practise.c:: err ...

  6. com.velocity.servlet

    package com.velocity.servlet; import java.io.IOException; import java.util.ArrayList; import java.ut ...

  7. Android(java)学习笔记86:案例短信发送器

    1.一般我们第一步都是先创建这个main.xml布局文件,这是良好的习惯: <?xml version="1.0" encoding="utf-8"?&g ...

  8. Hibernate学习笔记--------4.查询

    一.Get/Load Get方法是立即检索,而load是延迟检索,他们都是根据主键进行查询.在<class>标签中,若把lazy属性改为false,load方法就会立即检索,class中的 ...

  9. 使用urllib进行网页爬取

    # coding=gbk # 抓取开奖号码 # url:http://datachart.500.com/dlt/zoushi/jbzs_foreback.shtml ''' 对网页逐行迭代,找到目标 ...

  10. mapping 详解4(mapping setting)

    mapping type 映射设置一般发生在: 1. 增加新的 index 的时候,添加 mapping type,对 fields 的映射进行设置 PUT twitter { "mappi ...