POJ 3220 位运算+搜索

转载自:http://blog.csdn.net/lyhypacm/article/details/5813634

DES:相邻的两盏灯状态可以互换,给出初始状态。询问是否能在三步之内到达。如果能的话。输出不属。超出3步就输出more。

貌似典型应用是位压缩。我觉得各种按位运算用的也很巧妙。判断两盏灯是不是状态一样的时候,和交换状态的时候。先广搜一遍,保存到达各种状态的最短路径,然后查询就可以了。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<string>
 6 #include<queue>
 7 using namespace std;
 8 const int maxn=1<<16;
 9
10 int visi[maxn];
11 //两点相连,一共有32条边
12 int a[32][2]={{1,2},{3,4},{5,6},{7,8},{9,10},{11,12},{13,14},{15,16},{1,3},{2,4},{5,7},{6,8},{9,11},{10,12},{13,15},{14,16},
13        {1,5},{2,6},{3,7},{4,8},{9,13},{10,14},{11,15},{12,16},{1,9},{2,10},{3,11},{4,12},{5,13},{6,14},{7,15},{8,16}};
14
15 void BFS(int p)
16 {
17     int i;
18     visi[p]=0;
19     queue <int> mq;
20     mq.push(p);
21     while(!mq.empty())
22     {
23         int t=mq.front();
24         mq.pop();
25         if(visi[t]>=3)  continue;
26         for(i=0;i<32;i++)   //看下可以改变哪两个点
27         {
28             int p1,p2;
29             //表示a[i]灯的状态
30             p1=t&(1<<(a[i][0]-1));
31             p2=t&(1<<(a[i][1]-1));
32             if(p1==p2 || p1&&p2) continue;  //两个灯状态一样
33             p1=t^(1<<(a[i][0]-1));
34             p2=p1^(1<<(a[i][1]-1));  //两个灯交换状态
35             if(visi[p2]==-1)  //如果是其它的值,说明此时步骤不是最小的
36             {
37                 mq.push(p2);
38                 visi[p2]=visi[t]+1;
39             }
40         }
41     }
42 }
43
44 int main()
45 {
46     int tes;
47     int x,i,j;
48     x=(1<<16)-(1<<8);   //就是最终状态作为起始状态
49     memset(visi,-1,sizeof(visi));
50     BFS(x);
51     scanf("%d",&tes);
52     for(i=1;i<=tes;i++)
53     {
54         int sum=0;   //表示状态
55         for(j=0;j<16;j++)
56         {
57             int tmp;
58             scanf("%d",&tmp);
59             if(tmp==1)
60                 sum+=1<<j;
61         }
62
63         printf("Case #%d: ",i);
64         if(visi[sum]==-1)   //表示三步之内没达到
65             puts("more");
66         else printf("%d\n",visi[sum]);
67     }
68     return 0;
69 }

时间: 2024-11-05 00:22:49

POJ 3220 位运算+搜索的相关文章

[位运算] [搜索] [递推优化] [计算几何] TEST 2016.7.15

NOIP2014 提高组模拟试题 第一试试题 题目概况: 中文题目名称 合理种植 排队 科技节 源程序文件名 plant.pas/.c/.cpp lineup.pas/.c/.cpp scifest.pas/.c/.cpp 输入文件名 plant.in lineup.in scifest.in 输出文件名 plant.out lineup.out scifest.out 每个测试点时限 1s 1s 1s 测试点数目 10 10 10 每个测试点分值 10 10 10 内存上限 128MB 128

[ACM] POJ 1753 Flip Game (枚举,BFS,位运算)

Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29921   Accepted: 12975 Description Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the

POJ 2777 Count Color(线段树+位运算)

题目链接:http://poj.org/problem?id=2777 Description Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds of problems. Here, we get a new problem. There is a very long board with length L centimeter, L is a

poj 2799 IP Networks 模拟 位运算

poj链接:http://poj.org/problem?id=2799 这题实在是非常的有趣... 写的时候也非常的开心... 然后就写跪了... 刚好讲了ip地址和子网掩码的只是 整个学期的通信导论我就只有这节课有事没去结果昨晚把这方面的只是补起来了有种功德圆满的感觉 network address是前(32-n)随意 后n位全零 network mask是前(32-n)全一 后n位全零 其次是练习了各种移位操作 我发现移位操作是个非常好用的东西 因为它自填充0 所以对一个二进制数往右移8位

It&amp;#39;s not a Bug, It&amp;#39;s a Feature! (poj 1482 最短路SPFA+隐式图+位运算)

Language: Default It's not a Bug, It's a Feature! Time Limit: 5000MS   Memory Limit: 30000K Total Submissions: 1353   Accepted: 516 Description It is a curious fact that consumers buying a new software product generally do not expect the software to

POJ 2777 Count Color (线段树+位运算)

题意很简单了,对一个区间有两种操作: 1. "C A B C" Color the board from segment A to segment B with color C. //A~B涂上颜色C 2. "P A B" Output the number of different colors painted between segment A and segment B (including). //输出A~B间颜色的种类数 题目链接:http://poj.o

poj 2777 Count Color(线段树、状态压缩、位运算)

Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 38921   Accepted: 11696 Description Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds of problems. Here, we get a new problem.

POJ 1781 In Danger Joseph环 位运算解法

Joseph环,这次模固定是2.假设不是固定模2,那么一般时间效率是O(n).可是这次由于固定模2,那么能够利用2的特殊性,把时间效率提高到O(1). 规律能够看下图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQva2VuZGVuMjM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" > 具体具体解析请看大师Knuth的Concrete m

POJ 1166 The Clocks 位运算与BFS

1.题意:有一组3*3的只有时针的挂钟阵列,每个时钟只有0,3,6,9三种状态:对时针阵列有9种操作,每种操作只对特点的几个时钟拨一次针,即将时针顺时针波动90度,现在试求从初试状态到阵列全部指向0的状态所需要的最小操作数的操作方案: 2.输入输出:输入给出阵列初始状态,0,1,2,3分别表示0,3,6,9:要求输出最快方案的操作序列: 3.分析:IOI 1994的考题,BFS是比较容易想到的方法之一,关键是如何简洁的表示和改变BFS过程中的阵列状态:这里使用位运算的方法:具体如下: 首先一共9