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. jQuery对象和DOM对象之间的转换

    jQuery对象不能使用DOM对象的任何方法,DOM对象也不能使用jQuery对象的任何方法.在需要使用时需要对其进行转换. jQuery对象前使用"$",这不是必须的,不这么使用 ...

  2. 配置支持Basler的API函数的开发环境

    第一步:文件说明 使用默认路径安装Basler pylon x86 4.2.1.4845.exe 以后生产的文件如下: 文件说明: apps为用于配置ip和调试相机的软件 bin为驱动程序 CLPro ...

  3. SQL Server创建视图——视图的作用

    视图简介: 视图可以看作定义在SQL Server上的虚拟表.视图正如其名字的含义一样,是另一种查看数据的入口. 常规视图本身并不存储实际的数据,而仅仅是由SELECT语句组成的查询定义的虚拟表 . ...

  4. bootstrap模态框嵌套、tabindex属性、去除阴影

    模态框嵌套 在开发中,遇到需要通过点击事件触发第一个模态框,触发后通过事件唤起第二个模态框,并且通过事件触发第三个模态框:即模态框嵌套. 模态框嵌套需要用一个模态框包裹所涉及嵌套的模态框,从而点击触发 ...

  5. web前端基础——初识CSS

    1 CSS概要 CSS(Cascading Style Sheets)称为层叠样式表,用于美化页面(单纯HTML写的页面只是网页框架和内容的组合,相当于赤裸的人,而CSS则是给赤裸的人穿上华丽的外衣) ...

  6. hadoop 一些命令

    关闭访问墙  service iptables stop hadoop dfs -mkdir input hadoop dfs -copyFromLocal conf/* input hadoop j ...

  7. GIT使用—提交的查找与变更

    一.查找提交 (1)git bisect(二分搜索法) 基于任意搜索条件查找特定的错误提交.在排查某个提交版本导致的错误时非常有用. [root@localhost public_html]# git ...

  8. Thinkphp5.0实战开发一------命名空间详解

    序言 ThinkPHP是一个快速.兼容而且简单的轻量级国产PHP开发框架,使用ThinkPHP框架可以极大简化我们的开发过程,节省时间.这个专题我将记录自己学习使用ThinkPHP5.0的进行实战开发 ...

  9. OAuth Implementation for ASP.NET Web API using Microsoft Owin.

    http://blog.geveo.com/OAuth-Implementation-for-WebAPI2 OAuth is an open standard for token based aut ...

  10. tornado之WebSocket

    WebSocket WebSocket是HTML5规范中新提出的客户端-服务器通讯协议,协议本身使用新的ws://URL格式. WebSocket 是独立的.创建在 TCP 上的协议,和 HTTP 的 ...