SPOJ-CLEANRBT-状压dp
CLEANRBT - Cleaning Robot
Here, we want to solve path planning for a mobile robot cleaning a rectangular room floor with furniture.
Consider the room floor paved with square tiles whose size fits the cleaning robot (1 × 1). There are 'clean tiles' and 'dirty tiles', and the robot can change a 'dirty tile' to a 'clean tile' by visiting the tile. Also there may be some obstacles (furniture) whose size fits a tile in the room. If there is an obstacle on a tile, the robot cannot visit it. The robot moves to an adjacent tile with one move. The tile onto which the robot moves must be one of four tiles (i.e., east, west, north or south) adjacent to the tile where the robot is present. The robot may visit a tile twice or more.
Your task is to write a program which computes the minimum number of moves for the robot to change all 'dirty tiles' to 'clean tiles', if ever possible.
Input
IThe input consists of multiple maps, each representing the size and arrangement of the room. A map is given in the following format.
w h
c11 c12 c13 ... c1w
c21 c22 c23 ... c2w
...
ch1 ch2 ch3 ... chw
The integers w and h are the lengths of the two sides of the floor of the room in terms of widths of floor tiles. w and h are less than or equal to 20. The character cyx represents what is initially on the tile with coordinates (x, y) as follows.
'.' : a clean tile
'*' : a dirty tile
'x' : a piece of furniture (obstacle)
'o' : the robot (initial position)
In the map the number of 'dirty tiles' does not exceed 10. There is only one 'robot'.
The end of the input is indicated by a line containing two zeros.
Output
For each map, your program should output a line containing the minimum number of moves. If the map includes 'dirty tiles' which the robot cannot reach, your program should output -1.
Example
Input:
7 5
.......
.o...*.
.......
.*...*.
.......
15 13
.......x.......
...o...x....*..
.......x.......
.......x.......
.......x.......
...............
xxxxx.....xxxxx
...............
.......x.......
.......x.......
.......x.......
..*....x....*..
.......x.......
10 10
..........
..o.......
..........
..........
..........
.....xxxxx
.....x....
.....x.*..
.....x....
.....x....
0 0 Output:
8
49
-1 先把所有垃圾之间的距离处理出来,将初始点视为一个特殊的垃圾点即可。
f[cur][S][j],表示已经处理了cur个垃圾,集合表示为S,且处理的最后一个垃圾是j的最小花费.
#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
int fx[][]={,,,,-,,,-};
int e[][];
int n,m;
char p[][];
int f[][(<<)+][];
struct Point{
int x,y;
bool operator==(Point t){
return x==t.x&&y==t.y;
}
}P[];
#define ppi pair<Point,int>
bool vis[][];
int bfs(int a,int b)
{
memset(vis,,sizeof(vis));
queue<ppi>q;
q.push(make_pair(P[a],));
while(!q.empty()){
ppi u=q.front();
q.pop();
if(u.first==P[b]) return u.second;
if(vis[u.first.x][u.first.y]) continue;
vis[u.first.x][u.first.y]=;
for(int i=;i<;++i){
int dx=u.first.x+fx[i][];
int dy=u.first.y+fx[i][];
if(dx<||dy<||dx>m||dy>n||p[dx][dy]=='x'||vis[dx][dy]) continue;
q.push(make_pair(Point{dx,dy},u.second+));
}
}
return -;
}
int main()
{
int i,j,k;
while(cin>>n>>m&&n&&m){
int tot=;
int rx,ry;
for(i=;i<=m;++i){
for(j=;j<=n;++j){
cin>>p[i][j];
if(p[i][j]=='o'){
P[].x=i;
P[].y=j;
rx=i;
ry=j;
}
if(p[i][j]=='*'){
tot++;
P[tot].x=i;
P[tot].y=j;
}
}
}
for(i=;i<=tot;++i){
for(j=i;j<=tot;++j){
if(i==j) e[i][j]=;
else{
e[i][j]=e[j][i]=bfs(i,j);
//cout<<i<<' '<<j<<' '<<e[i][j]<<endl;
}
}
} memset(f,inf,sizeof(f));
f[][][]=;
int cur=;
for(int w=;w<=tot;++w)
{
for(i=;i<(<<tot);++i){
for(j=;j<=tot;++j){
if(f[cur][i][j]!=inf){
for(k=;k<=tot;++k){
if(e[j][k]!=- && (i&(<<(k-)))==){
f[cur^][i|(<<(k-))][k]=min(
f[cur^][i|(<<(k-))][k],f[cur][i][j]+e[j][k]);
}
}
}
}
}
for(i=;i<(<<tot);++i){
for(j=;j<=tot;++j){
if(f[cur^][i][j]==inf) continue;
//cout<<i<<' '<<j<<' '<<f[cur^1][i][j]<<endl;
}
}
memset(f[cur],inf,sizeof(f[cur]));
cur^=;
} int ans=inf;
for(i=;i<=tot;++i) ans=min(ans,f[cur][(<<tot)-][i]);
if(ans==inf) ans=-;
cout<<ans<<endl;
}
return ;
}
SPOJ-CLEANRBT-状压dp的更多相关文章
- BZOJ 1087: [SCOI2005]互不侵犯King [状压DP]
1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3336 Solved: 1936[Submit][ ...
- nefu1109 游戏争霸赛(状压dp)
题目链接:http://acm.nefu.edu.cn/JudgeOnline/problemShow.php?problem_id=1109 //我们校赛的一个题,状压dp,还在的人用1表示,被淘汰 ...
- poj3311 TSP经典状压dp(Traveling Saleman Problem)
题目链接:http://poj.org/problem?id=3311 题意:一个人到一些地方送披萨,要求找到一条路径能够遍历每一个城市后返回出发点,并且路径距离最短.最后输出最短距离即可.注意:每一 ...
- [NOIP2016]愤怒的小鸟 D2 T3 状压DP
[NOIP2016]愤怒的小鸟 D2 T3 Description Kiana最近沉迷于一款神奇的游戏无法自拔. 简单来说,这款游戏是在一个平面上进行的. 有一架弹弓位于(0,0)处,每次Kiana可 ...
- 【BZOJ2073】[POI2004]PRZ 状压DP
[BZOJ2073][POI2004]PRZ Description 一只队伍在爬山时碰到了雪崩,他们在逃跑时遇到了一座桥,他们要尽快的过桥. 桥已经很旧了, 所以它不能承受太重的东西. 任何时候队伍 ...
- bzoj3380: [Usaco2004 Open]Cave Cows 1 洞穴里的牛之一(spfa+状压DP)
数据最多14个有宝藏的地方,所以可以想到用状压dp 可以先预处理出每个i到j的路径中最小权值的最大值dis[i][j] 本来想用Floyd写,无奈太弱调不出来..后来改用spfa 然后进行dp,这基本 ...
- HDU 1074 Doing Homework (状压dp)
题意:给你N(<=15)个作业,每个作业有最晚提交时间与需要做的时间,每次只能做一个作业,每个作业超出最晚提交时间一天扣一分 求出扣的最小分数,并输出做作业的顺序.如果有多个最小分数一样的话,则 ...
- 【BZOJ1688】[Usaco2005 Open]Disease Manangement 疾病管理 状压DP
[BZOJ1688][Usaco2005 Open]Disease Manangement 疾病管理 Description Alas! A set of D (1 <= D <= 15) ...
- 【BZOJ1725】[Usaco2006 Nov]Corn Fields牧场的安排 状压DP
[BZOJ1725][Usaco2006 Nov]Corn Fields牧场的安排 Description Farmer John新买了一块长方形的牧场,这块牧场被划分成M列N行(1<=M< ...
- 【BZOJ1087】 [SCOI2005]互不侵犯King 状压DP
经典状压DP. f[i][j][k]=sum(f[i-1][j-cnt[k]][k]); cnt[i]放置情况为i时的国王数量 前I行放置情况为k时国王数量为J #include <iostre ...
随机推荐
- oracle dataguard参数
在整个dg配置中,最复杂的也许就是参数的配置了,并且有许多参数都可以延伸出去讲很多,所以今天我们来看看dg的参数配置,顺便加上一点dataguard进程相关的信息,帮助理解. 在配置dg的过程中,我们 ...
- 使用JS实现页面倒数计数
<script type="text/javascript"> var number = 5; function showNum(){ if(number==1){ w ...
- Django-session中间件源码简单分析
Django-session中间件源码简单分析 settings里有关中间件的配置 MIDDLEWARE = [ 'django.middleware.security.SecurityMiddlew ...
- Ajax跨域请求 同源策略与Jsonp
同源策略 同源策略(Same origin policy)是一种约定,它是浏览器最核心也最基本的安全功能,如果缺少了同源策略,则浏览器的正常功能可能都会受到影响.可以说Web是构建在同源策略基础之上的 ...
- Oracle DDL+DML+DCL实例
SQL语言共分为四大类: 数据查询语言DQL: 数据查询语言DQL基本结构是由SELECT子句,FROM子句,WHERE子句组成的查询块. 数据操纵语言DML: DML是“数据操纵语言”( Data ...
- String类型 在底层剖析,并比较 与StringBuilding 的区别
1.string和 stringbuilder的区别: String在任何语言中,都有它的特殊性,在.NET中也是如此.它属于基本数据类型,也是基本数据类型中唯一的引用类型.字符串可以声明为常量,但是 ...
- [Deep Learning]任意层cnn的matlab版本实现
之前实现了一层的卷积神经网络,又实现了两层的卷积神经网络,接下来把cnn扩展到任意层就不难了. 这难道就是传说中的“道生一,一生二,二生三,三生万物”么?=.= 代码还是在github上. 比较有趣的 ...
- Python面试题之Python正则表达式re模块
一.Python正则表达式re模块简介 正则表达式,是一门相对通用的语言.简单说就是:用一系列的规则语法,去匹配,查找,替换等操作字符串,以达到对应的目的:此套规则,就是所谓的正则表达式.各个语言都有 ...
- java第四周学习总结
学号20145336 <Java程序设计>第4周学习总结 教材学习内容总结 继承 继承符合(Don't Repeat Yourself)原则,即在自己的代码中不要重复自己编写的信息,这在多 ...
- 批处理命令 For循环命令详解!
批处理for命令详解FOR这条命令基本上都被用来处理文本,但还有其他一些好用的功能!看看他的基本格式(这里我引用的是批处理中的格式,直接在命令行只需要一个%号)FOR 参数 %%变量名 IN (相关文 ...