T_T ++运算符和+1不一样。(i+1)%4 忘带小括号了。bfs函数是bool 型,忘记返回false时的情况了。噢。。。。debug快哭了。。。。。。

DESCRIPTION:
求最少的步骤。使得棋盘上的棋子全黑或者全白。奇数次相当于1次。偶数次相当于不翻。

bfs用来求解最优问题。主要用来求距离初始状态路径最短的路径。思想是暴力枚举+位运算。第一次做位运算的题目。。醉醉的啦。。。。。。

附代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
#define end1 65535
#define end2 0
using namespace std;

int step[end1];
bool used[end1];
unsigned short q[end1];
int front = 0, rear = 0;

void init()
{
    char str;
    front = 0, rear = 0;
    unsigned short int begin = 0;
    for (int i=0; i<4; ++i)
    {
        for (int j=0; j<4; ++j)
        {
            cin >> str;
            if (str == 'b')
               begin |= (1 << (i*4 + j));
        }
    }
    memset(step, 0, sizeof(step));
    memset(used, 0, sizeof(used));
    q[rear++] = begin;
    used[begin] = 1;
    step[begin] = 0;
}

unsigned short int move(unsigned short int state, int i)
{
    unsigned short int temp = 0;
    temp |= (1 << i);
    if ((i + 1) % 4 != 0)
        temp |= (1 << (i + 1));
    if (i % 4 != 0)
        temp |= (1 << (i - 1));
    if (i + 4 < 16)
        temp |= (1 << (i + 4));
    if (i - 4 >= 0)
        temp |= (1 << (i - 4));
    return (state^temp);
}

bool bfs()
{
     while (front < rear)
     {
         unsigned short state = q[front++];
         for (int i=0; i<16; ++i)
         {
             unsigned short temp;
             temp = move(state, i);
            if(0 == state || 65535 == state)
            {
              cout << step[state];
              return true;
            }
             else if (!used[temp])
             {
                 q[rear++] = temp;
                 used[temp] = true;
                 step[temp] = step[state]+1;
             }
         }
     }
     return false;
}

int main()
{
    init();
    if (!bfs())
        cout << "Impossible" ;

char c;
    cin >> c;
    return 0;
}

POJ 1753 bfs+位运算的更多相关文章

  1. POJ 1166 The Clocks [BFS] [位运算]

    1.题意:有一组3*3的只有时针的挂钟阵列,每个时钟只有0,3,6,9三种状态:对时针阵列有9种操作,每种操作只对特点的几个时钟拨一次针,即将时针顺时针波动90度,现在试求从初试状态到阵列全部指向0的 ...

  2. HDU5627--Clarke and MST (bfs+位运算)

    http://www.cnblogs.com/wenruo/p/5188495.html Clarke and MST Time Limit: 2000/1000 MS (Java/Others) M ...

  3. Vijos 1206 CoVH之再破难关 [BFS] [位运算]

    1.题意:一个由01组成的4*4的矩阵,可以实现相邻元素交换位置的操作,给出初试状态和目标状态,试求最少操作数的方案: 2.输入输出:输入给出初试矩阵和目标矩阵:要求输出最小操作的次数: 3.分析:输 ...

  4. POJ 1753 Flip Game (状态压缩 bfs+位运算)

    Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 square ...

  5. poj 2965 The Pilots Brothers' refrigerator枚举(bfs+位运算)

    //题目:http://poj.org/problem?id=2965//题意:电冰箱有16个把手,每个把手两种状态(开‘-’或关‘+’),只有在所有把手都打开时,门才开,输入数据是个4*4的矩阵,因 ...

  6. hdu 1885 Key Task(bfs+位运算)

    题意:矩阵中'#'表示墙,'.'表示通路,要求从起点'*'到达终点'X',途中可能遇到一些门(大写字母),要想经过,必须有对应的钥匙(小写字母).问能否完成,若能,花费的时间是多少. 分析:同hdu ...

  7. POJ 1753 BFS

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 44450   Accepted: 19085 Descr ...

  8. 【BFS】【位运算】解药还是毒药

    [codevs2594]解药还是毒药 Description Smart研制出对付各种症状的解药,可是他一个不小心,每种药都小小地配错了一点原料,所以这些药都有可能在治愈某些病症的同时又使人患上某些别 ...

  9. POJ 1753 位运算+枚举

    题意: 给出4*4的棋盘,只有黑棋和白棋,问你最少几步可以使棋子的颜色一样. 游戏规则是:如果翻动一个棋子,则该棋子上下左右的棋子也会翻一面,棋子正反面颜色相反. 思路: 都是暴搜枚举. 第一种方法: ...

随机推荐

  1. P2894 [USACO08FEB]酒店Hotel 线段树

    题目大意 多次操作 查询并修改区间内长度==len的第一次出现位置 修改区间,变为空 思路 类似于求区间最大子段和(应该是这个吧,反正我没做过) 维护区间rt的 从l开始向右的最长长度 从r开始向左的 ...

  2. POJ 1270 Following Orders(拓扑排序)题解

    Description Order is an important concept in mathematics and in computer science. For example, Zorn' ...

  3. BZOJ1355: [Baltic2009]Radio Transmission KMP

    Description 给你一个字符串,它是由某个字符串不断自我连接形成的. 但是这个字符串是不确定的,现在只想知道它的最短长度是多少. Input 第一行给出字符串的长度,1 < L ≤ 1, ...

  4. LeetCode——Maximum Binary Tree

    Question Given an integer array with no duplicates. A maximum tree building on this array is defined ...

  5. 从nodejs到在线商城

    nodejs安装express开发框架 (开发环境:mkdir nodeshop && cd nodeshop > npm init > npm install expre ...

  6. NRF24L01 射频收发 使用方法

    在干啥 这两天在调nrf24l01,最终还是参考正点原子的例程才调通,看芯片手册太难了 还要说啥废话 废话说到这,接下来上代码 SPI协议 spi.c #include "spi.h&quo ...

  7. 为什么mongo中不能用int作为key

    为什么mongo中不能用int作为key??

  8. BZOJ 1758 【WC2010】 重建计划

    题目链接:重建计划 这道题现在已经成为一道板子题了…… 这是个非常显然的0-1分数规划,可以二分答案之后树分治判定一下.注意树分治的时候如果使用单调队列,需要把所有儿子预先按最大深度排好序,否则会被扫 ...

  9. Croc Champ 2013 - Round 1 E. Copying Data 分块

    E. Copying Data time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  10. WPF基础学习笔记整理 (五) DependencyObject & DependencyProperty

    参考资料: 1.http://www.cnblogs.com/Zhouyongh/archive/2009/10/20/1586278.html 基础知识: DependencyObject & ...