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的更多相关文章

  1. BZOJ 1087: [SCOI2005]互不侵犯King [状压DP]

    1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3336  Solved: 1936[Submit][ ...

  2. nefu1109 游戏争霸赛(状压dp)

    题目链接:http://acm.nefu.edu.cn/JudgeOnline/problemShow.php?problem_id=1109 //我们校赛的一个题,状压dp,还在的人用1表示,被淘汰 ...

  3. poj3311 TSP经典状压dp(Traveling Saleman Problem)

    题目链接:http://poj.org/problem?id=3311 题意:一个人到一些地方送披萨,要求找到一条路径能够遍历每一个城市后返回出发点,并且路径距离最短.最后输出最短距离即可.注意:每一 ...

  4. [NOIP2016]愤怒的小鸟 D2 T3 状压DP

    [NOIP2016]愤怒的小鸟 D2 T3 Description Kiana最近沉迷于一款神奇的游戏无法自拔. 简单来说,这款游戏是在一个平面上进行的. 有一架弹弓位于(0,0)处,每次Kiana可 ...

  5. 【BZOJ2073】[POI2004]PRZ 状压DP

    [BZOJ2073][POI2004]PRZ Description 一只队伍在爬山时碰到了雪崩,他们在逃跑时遇到了一座桥,他们要尽快的过桥. 桥已经很旧了, 所以它不能承受太重的东西. 任何时候队伍 ...

  6. bzoj3380: [Usaco2004 Open]Cave Cows 1 洞穴里的牛之一(spfa+状压DP)

    数据最多14个有宝藏的地方,所以可以想到用状压dp 可以先预处理出每个i到j的路径中最小权值的最大值dis[i][j] 本来想用Floyd写,无奈太弱调不出来..后来改用spfa 然后进行dp,这基本 ...

  7. HDU 1074 Doing Homework (状压dp)

    题意:给你N(<=15)个作业,每个作业有最晚提交时间与需要做的时间,每次只能做一个作业,每个作业超出最晚提交时间一天扣一分 求出扣的最小分数,并输出做作业的顺序.如果有多个最小分数一样的话,则 ...

  8. 【BZOJ1688】[Usaco2005 Open]Disease Manangement 疾病管理 状压DP

    [BZOJ1688][Usaco2005 Open]Disease Manangement 疾病管理 Description Alas! A set of D (1 <= D <= 15) ...

  9. 【BZOJ1725】[Usaco2006 Nov]Corn Fields牧场的安排 状压DP

    [BZOJ1725][Usaco2006 Nov]Corn Fields牧场的安排 Description Farmer John新买了一块长方形的牧场,这块牧场被划分成M列N行(1<=M< ...

  10. 【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 ...

随机推荐

  1. Apple Pay编程指导

    1.About Apple PayApple Pay是一种移动支付技术,让使用者把它们对真实的物品和服务的支付信息以一种方便和安全的方式给你. 对于在app中给出的数字物品和服务,可查看In-App ...

  2. PL/SQL 表约束

    1. 表相关 a. 主键:constraint [主键的约束名] primary key b. 外键:constraint [外键约束名] foreign key( ) references []() ...

  3. PKU 1573 Robot Motion(简单模拟)

    原题大意:原题链接 给出一个矩阵(矩阵中的元素均为方向英文字母),和人的初始位置,问是否能根据这些英文字母走出矩阵.(因为有可能形成环而走不出去) 此题虽然属于水题,但是完全独立完成而且直接1A还是很 ...

  4. 《mysql必知必会》读书笔记--触发器及管理事务处理

    触发器 触发器是MySQL响应DELETE,INSERT,UPDATE而自动执行的一条MySQL语句,其他语句不支持触发器. 创建触发器时,需要4个条件: 唯一的触发器名 触发器关联的表 触发器应该响 ...

  5. 【android】activity、fragment传值例子

    1:Activity篇 1.1向Activity传值 关键点在于putExtra.如果传递类的话,记得类实现Serializable接口 Intent intent = new Intent(Firs ...

  6. netty8---自定义编码解码器

    package com.cn.codc; import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.channel.Cha ...

  7. LVM2逻辑卷创建及扩容

    LVM是Logical Volume Manager(逻辑卷管理器)的简写,又译为逻辑卷宗管理器.逻辑扇区管理器.逻辑磁盘管理器.是Linux核心所提供的逻辑卷管理(Logical Volume Ma ...

  8. 关于STM8S使用硬件SPI收发问题

    源: 关于STM8S使用硬件SPI收发问题

  9. 20145216史婧瑶《Java程序设计》第一周学习总结

    20145216 <Java程序设计>第1周学习总结 教材学习内容总结 第一章 Java平台概论 1.1 Java不只是语言 1.Java三大平台:Java SE.Java EE与Java ...

  10. Jclemo_ CTF_WEEK1~2学习总结

    Jclemo_ CTF_WEEK1~2学习总结 纯属因为感觉有趣,加入了ForDKYCTF小组学习,心得体会就不说了,总结一下最近的学习知识点(不全,参考我自己的940133658@qq.com的印象 ...