POJ 1753 bfs+位运算
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+位运算的更多相关文章
- POJ 1166 The Clocks [BFS] [位运算]
1.题意:有一组3*3的只有时针的挂钟阵列,每个时钟只有0,3,6,9三种状态:对时针阵列有9种操作,每种操作只对特点的几个时钟拨一次针,即将时针顺时针波动90度,现在试求从初试状态到阵列全部指向0的 ...
- HDU5627--Clarke and MST (bfs+位运算)
http://www.cnblogs.com/wenruo/p/5188495.html Clarke and MST Time Limit: 2000/1000 MS (Java/Others) M ...
- Vijos 1206 CoVH之再破难关 [BFS] [位运算]
1.题意:一个由01组成的4*4的矩阵,可以实现相邻元素交换位置的操作,给出初试状态和目标状态,试求最少操作数的方案: 2.输入输出:输入给出初试矩阵和目标矩阵:要求输出最小操作的次数: 3.分析:输 ...
- 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 ...
- poj 2965 The Pilots Brothers' refrigerator枚举(bfs+位运算)
//题目:http://poj.org/problem?id=2965//题意:电冰箱有16个把手,每个把手两种状态(开‘-’或关‘+’),只有在所有把手都打开时,门才开,输入数据是个4*4的矩阵,因 ...
- hdu 1885 Key Task(bfs+位运算)
题意:矩阵中'#'表示墙,'.'表示通路,要求从起点'*'到达终点'X',途中可能遇到一些门(大写字母),要想经过,必须有对应的钥匙(小写字母).问能否完成,若能,花费的时间是多少. 分析:同hdu ...
- POJ 1753 BFS
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 44450 Accepted: 19085 Descr ...
- 【BFS】【位运算】解药还是毒药
[codevs2594]解药还是毒药 Description Smart研制出对付各种症状的解药,可是他一个不小心,每种药都小小地配错了一点原料,所以这些药都有可能在治愈某些病症的同时又使人患上某些别 ...
- POJ 1753 位运算+枚举
题意: 给出4*4的棋盘,只有黑棋和白棋,问你最少几步可以使棋子的颜色一样. 游戏规则是:如果翻动一个棋子,则该棋子上下左右的棋子也会翻一面,棋子正反面颜色相反. 思路: 都是暴搜枚举. 第一种方法: ...
随机推荐
- 20145322 Exp5 利用nmap扫描
20145322 Exp5 利用nmap扫描 实验过程 使用命令创建一个msf所需的数据库 service postgresql start msfdb start 使用命令msfconsole开启m ...
- TableView,自定义TableViewCell
自定义Table 原理: http://blog.jobbole.com/67272/ http://www.cnblogs.com/wangxiaofeinin/p/3532831.html 补充: ...
- 使用volley来json解析
我对网络请求get和post的理解: 1.get只是从某网址获得固定数据,如我访问百度,返回就是百度的html语句: 2.post是我在访问的时候加了某些参数,如我访问某个服务器,访问的时候加了一些语 ...
- 使用CLR Profiler查看C#运行程序的内存占用情况
http://blog.csdn.net/wy3552128/article/details/8158938 https://msdn.microsoft.com/en-us/library/ff65 ...
- 【Coursera】Sixth Week(2)
DNS:Domain Name System The Domain Name System convert user-friendly names,like www.umich.edu, to net ...
- 贪心算法-Best cow line-字典序问题
代码: #include<cstdio> #include<iostream> #include<stdlib.h> #include<string> ...
- 04_kafka python客户端_Producer模拟
使用的python库: kafka-python 安装方式: pip install kafka-python 简单的模拟Producer """ Kafka Produ ...
- Codeforces Round #323 (Div. 2) D. Once Again... 乱搞+LIS
D. Once Again... time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- django模型和字段
一个模型(model)就是一个单独的.确定的数据的信息源,包含了数据的字段和操作方法.通常,每个模型映射为一张数据库中的表. 基本的原则如下: 每个模型在Django中的存在形式为一个Python类 ...
- Windows 2008 更改网卡绑定顺序
用 ncpa.cpl 或者用鼠标右键点网上邻居进去也好. 来到网卡列表画面. 然后,你会发觉没有菜单去操作[高级设置], 这里,最高级的步骤来了, 就是你需要按一个 [Alt]把菜单给显示出来,太神奇 ...