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 ...
随机推荐
- delphi webbrowser 跨域访问
procedure IterateFrames(const AWB: IWebBrowser2);var Doc: IHTMLDocument2; Container: IOleContainer; ...
- Linux系统——进程和计划任务管理
进程和计划任务管理 一.进程和程序的关系 进程:在CPU及内存中运行的程序代码:动态执行的代码:每个进程可以创建一个或多个进程 程序:保存在硬盘.光盘等介质中的可执行代码和数据:静态保存的代码 二.基 ...
- 【android内存泄露】 WebView篇
在咱的博客园app里,新闻的内容使用WebView展示的.在测试中,咱重复进入.退出某个新闻10多次,观察到 Objects一直在不断增长,反复触发GC,但是一直回收不了,占用的内存越来越高,于是警觉 ...
- 防止 IOS 和 安卓 自动锁屏
Ios代码 在文件AppController中的 didFinishLaunchingWithOptions函数中加一行代码即可: [[UIApplication sharedApplication] ...
- nmon监控及简要分析
性能测试中,各个服务器资源占用统计分析是一个很重要的组成部分,通常我们使用nmon这个工具来进行监控以及监控结果输出. 一. 在监控阶段使用类似下面的命令 ./nmon -f write_3s_20v ...
- Windows下编译live555源码
Windos下编译live555源码 环境 Win7 64位 + VS2012 步骤 1)源码下载并解压 在官网上下载最新live555源码,并对其进行解压. 2)VS下建立工程项目 新建Win32项 ...
- [.net基础]访问修饰符
标题:[.net基础]访问修饰符 一.前言 基础掌握不牢固啊,所以记录下来. 二.方法访问修饰符Internal (1).创建工程ParentAndSon (2).添加类ModelA namespac ...
- LSTM-based Encoder-Decoder for Multi-sensor Anomaly Detection
1.主要工作是将机械设备的传感器数据,LSTM-encoder-decoder模型输入正常数据时间序列训练模型,重构时间序列,然后使用异常数据进行测试,产生较高的重构错误,表明时间序列数据为异常的. ...
- 20145328 《Java程序设计》第6周学习总结
20145328 <Java程序设计>第6周学习总结 教材学习内容总结 输入/输出 InputStream与OutputStream 从应用程序角度来看,如果要将数据从来源取出,可以使用输 ...
- 深度学习:Keras入门(一)之基础篇【转】
本文转载自:http://www.cnblogs.com/lc1217/p/7132364.html 1.关于Keras 1)简介 Keras是由纯python编写的基于theano/tensorfl ...