poj2965(位运算压缩+bfs+记忆路径)
题意:有个4*4的开关,里面有着16个小开关
-+--
----
---- '+'表示开关是关着的,'-'表示开关是开着的,只有所有的开关全被打开,总开关才会被打开。现在有一种操作,只要改变某个开关,那么这个开关的行列所在开关都会被改变
-+-- 问,要打开总开关至少要改变多少次开关?并输出改变开关的位置。 思路: 由于每个开关只有两种状态,那么对于这16个小开关,我们可以用2进制来压缩下,如果开关是打开的那么为'0',如果是关着的,那么为'1',如此,我们就可以从下到上,从右到左给这16个开关
标记状态,如果以某个点为中心,那么这个点的行列状态都压缩进去,遇到这个点,取反与不取反,然后一次广搜过去,再记忆下路径,结果就出来了......
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
#define M 1<<16
int t[20]={
63624,62532,61986,61713,
36744,20292,12066,7953,
35064,17652,8946,4593,
34959,17487,8751,4383,
};
structnode
{
int k;
int step;
};
structnode1
{
int father;
int x,y;
}s[(1<<17)];
//bool vist[(1<<17)];
int sum;
void print(int ans)
{
if(ans==sum)
return;
print(s[ans].father);
printf("%d %d\n",s[ans].x+1,s[ans].y+1);
}
void bfs(int ans)
{
queue<node>q;
node p;
p.k=ans;
p.step=0;
s[p.k].father=-10;
q.push(p);
while(!q.empty())
{
p=q.front();
q.pop();
if(p.k==0)
{
printf("%d\n",p.step);
print(p.k);
return;
}
for(int i=0;i<16;i++)
{
node p1;
p1.k=p.k^t[i];
p1.step=p.step+1;
if(s[p1.k].father==-1)
{
s[p1.k].father=p.k;
s[p1.k].x=i/4;
s[p1.k].y=i%4;
q.push(p1);
}
}
}
}
int main()
{
int ans=0,cnt=15;
for(int i=0;i<4;i++)
{
char ch[100];
scanf("%s",ch);
for(int j=0;j<4;j++)
{
if(ch[j]=='+')
{
ans|=(1<<cnt);
}
cnt--;
}
}
//printf("%d\n",ans);
sum=ans;
for(int i=0;i<(M);i++)
{
s[i].father=-1;
}
bfs(ans);
return 0;
}
poj2965(位运算压缩+bfs+记忆路径)的更多相关文章
- HDU 3605 Escape (网络流,最大流,位运算压缩)
HDU 3605 Escape (网络流,最大流,位运算压缩) Description 2012 If this is the end of the world how to do? I do not ...
- HDU_1401——分步双向BFS,八进制位运算压缩,map存放hash
Problem Description Solitaire is a game played on a chessboard 8x8. The rows and columns of the ches ...
- poj1753(位运算压缩状态+bfs)
题意:有个4*4的棋盘,上面摆着黑棋和白旗,b代表黑棋,w代表白棋,现在有一种操作,如果你想要改变某一个棋子的颜色,那么它周围(前后左右)棋子的颜色都会被改变(白变成黑,黑变成白),问你将所有棋子变成 ...
- HDU_1401——同步双向BFS,八进制位运算压缩,map存放hash
这个速度比分步快一点,内存占的稍微多一点 Problem Description Solitaire is a game played on a chessboard 8x8. The rows an ...
- poj1077(康托展开+bfs+记忆路径)
题意:就是说,给出一个三行三列的数组,其中元素为1--8和x,例如: 1 2 3 现在,需要你把它变成:1 2 3 要的最少步数的移动方案.可以右移r,左移l,上移u,下移dx 4 6 4 5 67 ...
- poj1753-Flip Game 【状态压缩+bfs】
http://poj.org/problem?id=1753 Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- POJ 1753 bfs+位运算
T_T ++运算符和+1不一样.(i+1)%4 忘带小括号了.bfs函数是bool 型,忘记返回false时的情况了.噢....debug快哭了...... DESCRIPTION:求最少的步骤.使得 ...
- HDU5627--Clarke and MST (bfs+位运算)
http://www.cnblogs.com/wenruo/p/5188495.html Clarke and MST Time Limit: 2000/1000 MS (Java/Others) M ...
- POj 1753--Flip Game(位运算+BFS)
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 30669 Accepted: 13345 Descr ...
随机推荐
- 在Hadoop1.2.1上运行第一个Hadoop程序FileSystemCat
- perl进程管理一例
#!/usr/bin/perl -w use strict; use warnings; use DBI; #### # 这里进行服务器任务管理 ## #字符串映射函数 our %actions = ...
- PHP 将html页面导出至Word
<?php header("Content-Type: application/msword"); header("Content-Disposition: att ...
- Java – Generate random integers in a rangejava获取某个范围内的一个随机数
In this article, we will show you three ways to generate random integers in a range. java.util.Rando ...
- appium通过同级别(兄弟关系)元素找到元素
在做appium测试用例的时候,要获取金额值,用uiautomatorviewer查看该元素,该元素没有特别明显的个性特点,唯一有特点的定位是有content-desc值,但是该值是变动的,所以无法通 ...
- Hbase/Hadoop Java API编程常用语句
从scanner获取rowkey: for(Result rr : scanner){ String key =Bytes.toString(rr.getRow())} HBase API - Res ...
- Datagrid分页、排序、删除代码
<%@ Page language="c#" Codebehind="default.aspx.cs" AutoEventWireup="fal ...
- 【SSH】Hibernate关联映射
一对一关联映射 一对一主键关联映射 一句话:不加字段.主键既是主键又是外键. 一对一单向主键关联映射 配置: <span style="font-family:KaiTi_GB2 ...
- [CTCI] 双栈排序
双栈排序 题目描述 请编写一个程序,按升序对栈进行排序(即最大元素位于栈顶),要求最多只能使用一个额外的栈存放临时数据,但不得将元素复制到别的数据结构中. 给定一个int[] numbers(C++中 ...
- [IOS]开源库RegexKitLite正则表达式的使用
1.去RegexKitLite下载类库,解压出来会有一个例子包及2个文件,其实用到的就这2个文件,添加到工程中. 2.工程中添加libicucore.dylib frameworks. 友情提醒:一般 ...