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. Java中线程出现Exception in thread "Thread-0" java.lang.IllegalMonitorStateException异常 解决方法

    代码 package thread; public class TestChongNeng { public static void main(String[] args) { Thread t1 = ...

  2. Hadoop Hive概念学习系列之hive里的分区(九)

    为了对表进行合理的管理以及提高查询效率,Hive可以将表组织成“分区”. 分区是表的部分列的集合,可以为频繁使用的数据建立分区,这样查找分区中的数据时就不需要扫描全表,这对于提高查找效率很有帮助. 分 ...

  3. Future Works on P4

    Future Works on P4 P4 and NV: MPvisor, Hyper4, HyperV, Flex4 P4 and NFV P4 and Network Cache P4 and ...

  4. js访3d上下轮播图

    js/css访3d上下轮播图 (附件) <!DOCTYPE html> <html> <head> <meta charset="utf-8&quo ...

  5. MVC ---- 如何使用Predicate以及如何自定定义Predicate委托

    微软公司提供只能返回bool值,接受一个参数的委托类型(Predicate). //Predicate委托 public static class PredicateDemo{ //内置方法 publ ...

  6. ubuntu 14.04 安装 gflags

    1.下载 git clone https://github.com/gflags/gflags 2.编译 进入源码目录(即gflags文件夹) cmake . make -j 24 sudo make ...

  7. java23种设计模式之一: 策略模式

    由于最近在研究学习设计模式,我会用自己的理解方式来表述对设计模式的学习和认识,通过最常用.好记的案例来记住和使用设计模式,希望对设计代码方面有所提高和改进. 一.应用背景     在软件开发中常常遇到 ...

  8. 小橙书阅读指南(十三)——连通性算法(union-find)

    上一章我大概说明了什么是图论以及无向图的基础概念,本章我们要研究一种更普遍的算法——连通性算法.它属于图论的分支,也是一种抽象算法.在深入算法之前,我们先提出一个具体的问题:假设在空间中存在N个点,我 ...

  9. mvn编译

    mvn clean install -pl com:boss -am -DskipTests

  10. Jedis 之 初始<一>

    package xx.jedis; import java.util.Set; import redis.clients.jedis.Jedis; import redis.clients.jedis ...