poj 2688 Cleaning Robot bfs+dfs
首先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的更多相关文章
- POJ 2688 Cleaning Robot
题意: 给你一个n*m的图.你从'o'点出发,只能走路(图中的'.')不能穿墙(图中的'x'),去捡垃圾(图中的' * ')问最少走多少步能捡完所有垃圾,如有垃圾捡不了,输出-1. 思路: 有两个思路 ...
- Japan 2005 Domestic Cleaning Robot /// BFS 状压 二进制位运算 结构体内构造函数 oj22912
题目大意: 输入w h,接下来输入h行w列的图 ' . ':干净的点: ' * ' :垃圾: ' x ' : 墙: ' o ' : 初始位置: 输出 清理掉所有垃圾的最短路径长度 无则输出-1 ...
- Cleaning Robot (bfs+dfs)
Cleaning Robot (bfs+dfs) Here, we want to solve path planning for a mobile robot cleaning a rectangu ...
- POJ 1426 Find The Multiple --- BFS || DFS
POJ 1426 Find The Multiple 题意:给定一个整数n,求n的一个倍数,要求这个倍数只含0和1 参考博客:点我 解法一:普通的BFS(用G++能过但C++会超时) 从小到大搜索直至 ...
- HOJ 2226&POJ2688 Cleaning Robot(BFS+TSP(状态压缩DP))
Cleaning Robot Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4264 Accepted: 1713 Descri ...
- POJ 2227 The Wedding Juicer (优先级队列+bfs+dfs)
思路描述来自:http://hi.baidu.com/perfectcai_/item/701f2efa460cedcb0dd1c820也可以参考黑书P89的积水. 题意:Farmer John有一个 ...
- poj 2688 状态压缩dp解tsp
题意: 裸的tsp. 分析: 用bfs求出随意两点之间的距离后能够暴搜也能够用next_permutation水,但效率肯定不如状压dp.dp[s][u]表示从0出发訪问过s集合中的点.眼下在点u走过 ...
- poj 3414 Pots 【BFS+记录路径 】
//yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...
- POJ 2376 Cleaning Shifts(轮班打扫)
POJ 2376 Cleaning Shifts(轮班打扫) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] Farmer ...
随机推荐
- html系列教程--embed fieldset legend figure figurecaption
<embed> 标签:定义嵌入的内容 <embed src="" type="" /> embed属性: 1.src:嵌入内容地址 2. ...
- dp状态压缩-铺砖问题
题目:有一个n行m列的地板,需要用 1*2小砖铺盖,小砖之间互相不能重叠,问有多少种不同的铺法? 示范: 解法:用F[i][j]存放第i行的第j状态(j为十进制,转为二进制即是状态)有多少种方案. 用 ...
- php7 install memcached extension
#download source code package from git $ git clone https://github.com/php-memcached-dev/php-memcache ...
- JavaWeb核心编程之Tomcat安装和配置
什么是JavaWeb 在Sun的Java Servlet规范中, 对Java Web应用做了这样的定义: "Java Web应用由一组Servlet, HTML页面, 类, 以及其他可以被绑 ...
- 生成php所需要的APNS Service pem证书的步骤
1.登录到 iPhone Developer Connection Portal 并点击 App IDs 2.创建一个不使用通配符的 App ID .通配符 ID 不能用于推送通知服务.例如,我们的i ...
- Android 多状态按钮 ToggleButton
ToggleButton 选中状态,未选中状态并且需要为不同的状态设置不同的显示文本. 属性: checked="true" ...
- querySelectorAll的BUG
querySelector和querySelectorAll是W3C提供的新的查询接口 目前 IE8/9及Firefox/Chrome/Safari/Opera 的最新版已经支持它们. 但是Eleme ...
- Probability theory
1.Probability mass functions (pmf) and Probability density functions (pdf) pmf 和 pdf 类似,但不同之处在于所适用的分 ...
- android select选择器 checkbox改外观,button按下状态
android 可以用选择器.来加载视图.选择器里的选项也很多针对实际使用中用的几个进行描述. 1.button 的按下弹起改外观.选择器属性用 android:state_pressed 2.C ...
- 接收时物料必须为Active状态
应用 Oracle Inventory 层 Level Function 函数名 Funcgtion Name RCV_RCVRCERC 表单名 Form Name RCVRCERC 说明 Descr ...