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 ...
随机推荐
- Get started on your own KD 8 custom colorway
The 2009 Summer time Nike Basketball revealed the Cheap KD 8 and revealed three MVP-inspired colors ...
- 进程 、进程组、会话、控制终端之间的关系 (转载 http://blog.csdn.net/yh1548503342/article/details/41891047)
一个进程组可以包含多个进程 进程组中的这些进程之间不是孤立的,他们彼此之间或者存在者父子.兄弟关系,或者在功能有相近的联系. 那linux为什么要有进程组呢?其实提供进程组就是方便管理这些进程.假设要 ...
- PHP下使用Redis消息队列发布微博(复制)
phpRedisAdmin :github地址 图形化管理界面 git clone https://github.com/ErikDubbelboer/phpRedisAdmin.git cd ph ...
- Java设计原则—迪米特法则(转)
定义: 迪米特法则(Law of Demeter,LoD)也称为最少知识原则(Least Knowledge Principle,LKP). 一个对象应该对其他对象有最少的了解.通俗地讲,一个类应该对 ...
- [Python]返回函数,装饰器拾遗
def lazy_print(*args): def pr(): print(args) return pr 当我们调用lazy_print()时,返回的并不是求和结果,而是求和函数: >> ...
- Oracle trunc()函数,decode()函数,substr函数,GREATEST函数,java中substring函数的用法
--Oracle trunc()函数的用法/**************日期********************/1.select trunc(sysdate) from dual --2013- ...
- Vue组件里面data为什么必须是个函数
在创建或注册模板的时候,传入一个data属性用来绑定数据,但是在组件中,data必须是一个函数,而不能直接把对象赋值给它. export default { name:'app', data(){ r ...
- 理解 Dubbo SPI 扩展机制
写在前面 最近接触了 gRPC 体会到虽然众多 RPC 框架各有各的特点但是他们提供的特性和功能有很多的相似之处 , 这就说明他们面对同样的分布式系统带来的问题.从 2016 年左右开始接触到 dub ...
- Java-性能调优-理解GC日志
[ ~]# cat gc.log.0 | grep 'Full GC' 1.652: [Full GC (System) 1.652: [CMS: 0K->21718K(262144K), 0. ...
- 照着官网来安装openstack pike之environment设置
安装openstack前的准备环境: 两个centos7系统的环境:192.168.101.10 node1,192.168.101.11 node2 控制节点node1,计算节点node2 1.统一 ...