uva-321-暴力枚举-隐式图搜索
题意:给你n个房间,有许多灯的控制开关,i房间灯的开关在j房间,未开灯的房间不能进,i房间和j房间之间如果没有门,也不能从i进入到j,开始房间是1,并且灯是开着的,问你是否能够走到最后一个房间n,并且此时其他房间的灯都是关着的.如果存在多个解,输出操作步数最小的操作序列.
范围:n<=10,
解题思路:设状态为,当前的房间编号+当前其他房间灯的状态.所以总的状态为N*2^N,最大值10*1024,裸枚举.
注意,这道题目每次枚举都必须从房间编码1-N,要不然会wa,而且,PE也wa.使用了位标志房间灯的状态.
#include <iostream>
#include<map>
#include<memory.h>
#include<stdio.h>
#include<string>
#include<queue>
#include<vector>
using namespace std;
const int MAXN = ; class Node
{
public:
vector<string>step;
int cur;
int curStepNum;
int lights;
Node()
{
//step.reserve(65535);
}
bool operator <(const Node& node) const
{
return curStepNum > node.curStepNum;
};
};
int conn[MAXN][MAXN];
int contro[MAXN][MAXN];
int r, d, s;
int states[][];
priority_queue<Node>q;
int perLights[] = {
<< , << , << , << , << , << ,
<< , << , << , << }; void read()
{
int ss, ee;
for (int i = ;i < d;i++)
{
cin >> ss >> ee;
//start with 0
conn[ss - ][ee - ] = ;
conn[ee - ][ss - ] = ;
}
for (int i = ;i < s;i++)
{
cin >> ss >> ee;
contro[ss - ][ee - ] = ;
}
}
Node bfs()
{
Node node;
node.cur = ;
node.curStepNum = ;
node.lights = ;
q.push(node);
//init node 只有0房间是亮的
states[][] = ;
while (q.empty() == false)
{
node = q.top();
q.pop();
int curIndex = node.cur;
//cur lights state
int curLights = node.lights;
int curStepNum = node.curStepNum;
//判断是不是终点
if ((curIndex == r - ) && curLights == perLights[r - ])
{
return node;
} //枚举灯的状态
for (int i = ;i < ; i++)
{
if (contro[curIndex][i] == )
continue;
if (i == curIndex)
continue;
//控制的灯
int nextControLightIndex = i;
string desc = "";
int nextLight = ;
if ((curLights & perLights[nextControLightIndex]) == perLights[nextControLightIndex])
{
//关闭next房间的灯
nextLight = (curLights ^ perLights[nextControLightIndex]);
if (states[curIndex][nextLight] == )
//repeat
continue;
desc = "- Switch off light in room " + std::to_string(nextControLightIndex + );
desc += ".";
}
else
{
//打开next房间的灯
nextLight = curLights | perLights[nextControLightIndex];
if (states[curIndex][nextLight] == )
//repeat
continue;
desc = "- Switch on light in room " + std::to_string(nextControLightIndex + );
desc += ".";
}
Node newNode;
newNode.curStepNum = curStepNum + ;
newNode.cur = curIndex;
newNode.lights = nextLight;
newNode.step = node.step;
newNode.step.push_back(desc);
//push to queue
q.push(newNode);
states[curIndex][nextLight] = ;
}
//end curIndex enum lights state
//start door
for (int i = ;i < ;i++)
{
if (curIndex == i)
continue;
if (conn[curIndex][i] == )
continue;
int nextDoor = i;
if ((perLights[nextDoor] & curLights) == )
//灯是灭的,不能进
continue;
if (states[nextDoor][curLights] == )
//已经看到过,不能进
continue;
//灯是开着的,能进
Node newNode;
newNode.cur = nextDoor;
newNode.curStepNum = curStepNum + ;
newNode.lights = curLights;
newNode.step = node.step;
string desc = "- Move to room " + std::to_string(nextDoor + );
desc += ".";
newNode.step.push_back(desc);
states[nextDoor][curLights] = ;
q.push(newNode);
}
}
node.cur = -;
return node;
}
int main()
{
int t = ;
while (cin >> r >> d >> s)
{
if (r == d && d == s && r == )
break;
memset(conn, , sizeof(conn));
memset(contro, , sizeof(conn));
memset(states, , sizeof(states));
while (q.empty() == false)
q.pop();
read(); cout << "Villa #";
cout << t + << endl;
if (r == )
{
cout << "The problem can be solved in " << << " steps:" << endl; }
else
{
Node node = bfs();
//cout << "test" << endl;
if (node.cur == -)
{
cout << "The problem cannot be solved." << endl;
}
else
{
cout << "The problem can be solved in " << node.curStepNum << " steps:" << endl;
vector<string> b = node.step;
for (int i = ;i <= b.size() - ;i++)
cout << b[i] << endl;;
}
}
t++;
cout << endl;
} }
uva-321-暴力枚举-隐式图搜索的更多相关文章
- uva10603-倒水问题-暴力枚举-隐式图搜索
题意: 给你三个杯子,a,b,c,没有刻度,刚开始c杯是满的,倒水的要求,要么倒出水的杯子倒空,要么倒入杯子倒满. 结果: 要求某个杯子内有d水量,并且倒出的水量最少,如果倒不出d水量,那么倒出d1( ...
- UVa 658 - It's not a Bug, it's a Feature!(Dijkstra + 隐式图搜索)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- 【uva 658】It's not a Bug, it's a Feature!(图论--Dijkstra或spfa算法+二进制表示+类“隐式图搜索”)
题意:有N个潜在的bug和m个补丁,每个补丁用长为N的字符串表示.首先输入bug数目以及补丁数目.然后就是对M个补丁的描述,共有M行.每行首先是一个整数,表明打该补丁所需要的时间.然后是两个字符串,第 ...
- uva 10274 Fans and Gems(隐式图搜索+模拟)
Fans and Gems Input: Standard Input Output: Standard Output Tomy's fond of a game called 'Fans and G ...
- 紫书 例题 11-6 UVa 658 (状态压缩+隐式图搜索+最短路)
这道题用到了很多知识点, 是一道好题目. 第一用了状态压缩, 因为这里最多只有20位, 所以可以用二进制来储存状态 (要对数据范围敏感), 然后 涉及到了一些位运算. 第二这里是隐式 ...
- [HNOI2006]最短母串问题 --- AC自动机 + 隐式图搜索
[HNOI2006]最短母串问题 题目描述: 给定n个字符串(S1,S2.....,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2,......,Sn)都是T的子串. 输入格式: 第 ...
- 洛谷 P2622 关灯问题II【状压DP;隐式图搜索】
题目描述 现有n盏灯,以及m个按钮.每个按钮可以同时控制这n盏灯--按下了第i个按钮,对于所有的灯都有一个效果.按下i按钮对于第j盏灯,是下面3中效果之一:如果a[i][j]为1,那么当这盏灯开了的时 ...
- UVA 658 状态压缩+隐式图+优先队列dijstla
不可多得的好题目啊,我看了别人题解才做出来的,这种题目一看就会做的实在是大神啊,而且我看别人博客都看了好久才明白...还是对状态压缩不是很熟练,理解几个位运算用了好久时间.有些题目自己看着别人的题解做 ...
- uva 11088 暴力枚举子集/状压dp
https://vjudge.net/problem/UVA-11088 对于每一种子集的情况暴力枚举最后一个三人小组取最大的一种情况即可,我提前把三个人的子集情况给筛出来了. 即 f[S]=MAX{ ...
随机推荐
- require 与 include 的区别
require 的英文意思是 需要,有赖于.如果使用了这条语句,也就是告诉PHP内核,我这个程序需要这个文件,有赖于这个文件.或者通俗点儿讲就是:我要她!所以,PHP如果发现require参数中的文件 ...
- Docker容器 暴露多个端口
1.创建容器是指定 docker run -p <host_port1>:<container_port1> -p <host_port2>:<contain ...
- java线程实现的四种方式
java多线程的实现可以通过以下四种方式 1.继承Thread类,重写run方法 2.实现Runnable接口,重写run方法 3.通过Callable和FutureTask创建线程 4.通过线程池创 ...
- 【linux】之Centos6.x升级glibc
因为Centos比较保守依赖的glibc最高版本是2.12 rpm -qa|grep glibc strings /lib64/libc.so. |grep GLIBC_ 但是经常我们安装一些源码包, ...
- Qt QListWidget实现图片缩略图列表
转载:v_xchen_v 目标: 将本机中的多张图片以缩略图的形式显示在列表中 环境: 我们已经做好了菜单栏和文件选择对话框.参考:http://blog.csdn.net/v_xchen_v/art ...
- Ubuntu 14.10 下安装伪分布式hive-0.14.0
本地独立模式,MySQL作为元数据库 1 安装环境准备 1.1 安装JDK,在安装hadoop时候已经安装了,参考http://www.cnblogs.com/liuchangchun/p/40972 ...
- Requests+正则表达式 爬取猫眼电影
代码: import re import json from multiprocessing import Pool import requests from requests.exceptions ...
- PHP7.1扩展开发入门
第1步: 首先从官网下载了PHP源码http://am1.php.net/distributions/php-7.1.3.tar.bz2 第2步: 解压后可以看到根目录下面的ext文件夹里有ext_s ...
- go语言学习--go中闭包
Go语言支持匿名函数,即函数可以像普通变量一样被传递或使用. 使用方法如下: package main import ( "fmt" ) func main() { var v f ...
- MySQL学习----多版本并发mvcc
MySQL中的大多数事务性存储引擎实现的都不是简单的行级锁.基于提升并发性能的考虑,他们一般实现了多版本并发控制(mvcc).不仅是mysql,包括oracle,postgresql等其他数据库也实现 ...