题目链接

首先bfs, 求出两两之间的距离, 然后dfs就可以。

 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <queue>
#include <vector>
#include <map>
#include <set>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define mem(a) memset(a, 0, sizeof(a))
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, a, n) for(int i = a; i<n; i++)
#define ull unsigned long long
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const int inf = ;
const double eps = 1e-;
const int mod = 1e9+;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
char c[][];
int dis[][], vis[][], n, m, cnt, ans, used[];
struct node
{
int x, y, step;
node(){}
node(int x, int y, int step):x(x), y(y), step(step){}
};
int bfs(pll s, pll e) {
mem(vis);
vis[s.first][s.second] = ;
queue <node> q;
q.push(node(s.first, s.second, ));
while(!q.empty()) {
node tmp = q.front(); q.pop();
if(tmp.x == e.first && tmp.y == e.second)
return tmp.step;
for(int i = ; i<; i++) {
int tmpx = tmp.x + dir[i][];
int tmpy = tmp.y + dir[i][];
if(tmpx>=&&tmpx<n&&tmpy>=&&tmpy<m&&!vis[tmpx][tmpy]&&c[tmpx][tmpy]!='x') {
q.push(node(tmpx, tmpy, tmp.step+));
vis[tmpx][tmpy] = ;
}
}
}
return ;
}
void dfs(int now, int num, int now_dis) {
if(now_dis>=ans)
return ;
if(num == cnt-) {
if(now_dis<ans) {
ans = now_dis;
}
return ;
}
for(int i = ; i<cnt; i++) {
if(!used[i]) {
used[i] = ;
dfs(i, num+, now_dis+dis[now][i]);
used[i] = ;
}
}
}
pll point[];
int main()
{
while(scanf("%d%d", &m, &n)) {
if(n+m==)
break;
int sx, sy;
mem(dis);
cnt = ;
for(int i = ; i<n; i++)
scanf("%s", c[i]);
for(int i = ; i<n; i++) {
for(int j = ; j<m; j++) {
if(c[i][j] == 'o') {
c[i][j] = '';
point[].first = i;
point[].second = j;
}
if(c[i][j] == '*') {
point[cnt].first = i;
point[cnt++].second = j;
}
}
}
for(int i = ; i<cnt; i++) {
for(int j = i+; j<cnt; j++) {
dis[i][j] = dis[j][i] = bfs(point[i], point[j]);
}
}
int flag = ;
for(int i = ; i<cnt; i++) {
if(dis[][i] == ) {
flag = ;
}
}
if(flag) {
puts("-1");
continue;
}
mem(used);
used[] = ;
ans = inf;
dfs(, , );
printf("%d\n", ans);
}
}

poj 2688 Cleaning Robot bfs+dfs的更多相关文章

  1. POJ 2688 Cleaning Robot

    题意: 给你一个n*m的图.你从'o'点出发,只能走路(图中的'.')不能穿墙(图中的'x'),去捡垃圾(图中的' * ')问最少走多少步能捡完所有垃圾,如有垃圾捡不了,输出-1. 思路: 有两个思路 ...

  2. Japan 2005 Domestic Cleaning Robot /// BFS 状压 二进制位运算 结构体内构造函数 oj22912

    题目大意: 输入w h,接下来输入h行w列的图 ' . ':干净的点:  ' * ' :垃圾:  ' x ' : 墙:  ' o ' : 初始位置: 输出 清理掉所有垃圾的最短路径长度 无则输出-1 ...

  3. Cleaning Robot (bfs+dfs)

    Cleaning Robot (bfs+dfs) Here, we want to solve path planning for a mobile robot cleaning a rectangu ...

  4. POJ 1426 Find The Multiple --- BFS || DFS

    POJ 1426 Find The Multiple 题意:给定一个整数n,求n的一个倍数,要求这个倍数只含0和1 参考博客:点我 解法一:普通的BFS(用G++能过但C++会超时) 从小到大搜索直至 ...

  5. HOJ 2226&POJ2688 Cleaning Robot(BFS+TSP(状态压缩DP))

    Cleaning Robot Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4264 Accepted: 1713 Descri ...

  6. POJ 2227 The Wedding Juicer (优先级队列+bfs+dfs)

    思路描述来自:http://hi.baidu.com/perfectcai_/item/701f2efa460cedcb0dd1c820也可以参考黑书P89的积水. 题意:Farmer John有一个 ...

  7. poj 2688 状态压缩dp解tsp

    题意: 裸的tsp. 分析: 用bfs求出随意两点之间的距离后能够暴搜也能够用next_permutation水,但效率肯定不如状压dp.dp[s][u]表示从0出发訪问过s集合中的点.眼下在点u走过 ...

  8. poj 3414 Pots 【BFS+记录路径 】

    //yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...

  9. POJ 2376 Cleaning Shifts(轮班打扫)

    POJ 2376 Cleaning Shifts(轮班打扫) Time Limit: 1000MS   Memory Limit: 65536K [Description] [题目描述] Farmer ...

随机推荐

  1. asp.net RadioButton控件基础

    RadioButton按钮呢,必须要设置groupname属性的值才能将多个RadioButton按钮设置为单选按钮,当AutoPostBack="true"的时候,在change ...

  2. C#根据汉字生成拼音首字母全称

    static void Main(string[] args) { string s = GetChineseSpell("周杰伦"); Console.WriteLine(s.T ...

  3. Oracle中针对中文进行排序[Z]

    在oracle 9i之前,对中文的排序,是默认按2进制编码来进行排序的. 9i时增加了几种新的选择: 按中文拼音进行排序:SCHINESE_PINYIN_M 按中文部首进行排序:SCHINESE_RA ...

  4. 相见恨晚——MarkDown

    什么是MarkDown MarkDown是一种轻量级的标记语言 MarkDown使你更加关注文章的内容 MarkDown使文章的排版变得简单直接 什么情景下使用MarkDown 在我们熟悉的githu ...

  5. Notes里OK,CANCEL按钮的设定

    message并不能达到想要的目的: If Not udoc Is Nothing Then        'MessageBox "既にデータがあります.先月のデータを削除してください.& ...

  6. Linux中的那些英文缩写和她的含义们

    系统 man: Manual 意思是手册,可以用这个命令查询其他命令的用法. pwd:Print working directory 打印工作路径. su:Swith user 切换用户,切换到roo ...

  7. 考察printf函数返回值

    最近偶然间见了这样一道题:  #include<stdio.h> int main() { ; printf("%d\n",printf("%d", ...

  8. 【Chromium中文文档】进程模型

    进程模型 转载请注明出处:https://ahangchen.gitbooks.io/chromium_doc_zh/content/zh//General_Architecture/Process_ ...

  9. Google Play Services Library update and missing symbol @integer/google_play_services_version

    转自http://stackoverflow.com/questions/19843784/google-play-services-library-update-and-missing-symbol ...

  10. mongodb remove删除文档的用法

    在看<mongoDB权威指南>中,在删除文档时,出现问题: 书中介绍:采用db.foo.remove()命令则可以删除foo集合中所有的文档,但是在执行该命令时,shell客户端却报错. ...