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 ...
随机推荐
- bootstrapvalidator之API学习
最近项目用到了bootstrap框架,其中前端用的校验,采用的是bootstrapvalidator插件,也是非常强大的一款插件.我这里用的是0.5.2版本.下面记录一下使用中学习到的相关API,不定 ...
- android监听键盘
android中的带有输入功能的页面布局经常被弹出的键盘遮挡,一种处理方法是监听键盘的弹出,设置布局的padding或隐藏某些占位控件,使得输入框不被键盘遮挡.一种常用的方法是当Activity设置为 ...
- C# - 动态连接数据库字符串
String conStr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|数据库文件.mdf;Integrated ...
- Windows 去掉启动时的放大镜
控制面板-轻松访问中心-使计算机更易于显示不勾选 启用放大镜
- AutoCompleteTextview、MultiAutoCompleteTextView
AutoCompleteTextview 动态匹配输入内容文本框 属性: android:completionThreshold="2"; ...
- Arduino周边模块:执行部件(舵机、直流电机、步进电机)
Arduino周边模块:执行部件 Arduino周边模块:执行部件 嵌入式系统的构成 如今已经有各种各样的基于Arduino的嵌入式系统, 比如:智能小车.3D打印机.机器人,甚至还有基于Arduin ...
- Android 获取有规律资源Id解决方案
在多个有规律的资源ID获取的时候,可以使用getIdentifier方法来获取,来获取. 用到场景:工具类打成.jar包的时候,有时候会需要引用到res中的资源,这时候不能将资源一起打包,只能通过反射 ...
- Asp.net MVC学习
一.mvc项目的创建并运行 1.启动vs2010 2.新建项目 3.选择Asp.net mvc应用程序 4.不创建测试用例 5.创建之后的效果 6.运行后的mvc程序
- OSG中的视角 eye up center
这三个值都是vec3变量,其中eye和center确定视角 eye就相当于人的眼睛,我们观察场景,是从这个坐标去看的,然后有了眼睛,我们观察得有一个方向,那么久需要另外一个坐标,就是c ...
- TLC是什么
TLC = Triple-Level Cell,即3bit/cell,它的寿命短,速度慢,约500-5000次擦写寿命. 现在U盘多为MLC,TLC也有一部分,将来TLC会占大部分市场. 一种名为TL ...