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. 20145127 《Java程序设计》第四次实验报告

    在本周,我们进行了Andirod部分的学习,这一次实验是使用Andirod Studio来运行简单的Andirod小程序,并在自己的手机虚拟机上显示自己的学号,为了达到这一效果,我在Andirod S ...

  2. JS ——DOM,BOM(包含盒模型,动画)总结

    JS盒模型 content: 通过计算后样式获取padding + content: box.clientWidth | box.clientHeightborder + padding + cont ...

  3. C语言宏定义中的#和##的作用【转】

    本文转载自:http://my.oschina.net/shelllife/blog/123202 在宏定义中#和##的作用是:前者将宏定义的变量转化为字符串:后者将其前后的两个宏定义中的两个变量无缝 ...

  4. Python跨平台打包

    对于pyinstaller,可以完成在windows,linux,和mac下的python脚本编译,生成exe,elf,.app文件: 1.使用方法: 在pyinstaller的官网上下载,一般是源码 ...

  5. The destination you provided is not a full refname (i.e., starting with "refs/")

    $ git push v5 v5/hotfix/5.1:hotfix/5.1-quartzerror: The destination you provided is not a full refna ...

  6. 【第二十一章】 springboot + 定时任务

    1.application.properties #cron job.everysecond.cron=0/1 * * * * * job.everytensecond.cron=0/10 * * * ...

  7. get-post区别

    原文链接:http://www.techweb.com.cn/network/system/2016-10-11/2407736.shtml GET和POST是HTTP请求的两种基本方法,要说它们的区 ...

  8. Windows 2003 server下载

    http://www.downza.cn/soft/182837.html或http://www.imsdn.cn/operating-systems/windows-server-2003/

  9. ADO.NET 批量插入

    在.Net1.1中无论是对于批量插入整个DataTable中的所有数据到数据库中,还是进行不同数据源之间的迁移,都不是很方便.而 在.Net2.0中,SQLClient命名空间下增加了几个新类帮助我们 ...

  10. Phpstorm配置scss不生成缓存

    --no-cache 加上这个,就不会生成 .sass-cache 文件夹了.