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 ...
随机推荐
- OCP prepare 20140701
1. rman的完全备份,和不完全备份 Oracle 数据库可以实现数据库不完全恢复与完全恢复.完全恢复是将数据库恢复到最新时刻,也就是无损恢复,保证数据库无丢失的恢复.而不完全恢复则是根据需要特意将 ...
- [iOS]手把手教你实现微信小视频
本文个人原创,转载请注明出处,谢谢. 前段时间项目要求需要在聊天模块中加入类似微信的小视频功能,这边博客主要是为了总结遇到的问题和解决方法,希望能够对有同样需求的朋友有所帮助. 效果预览: 这里先罗列 ...
- 使用关联对象(AssociatedObject)为UIButton添加Block响应
在开发中,要给UIButton添加点击事件的话,通常的做法是这样的 UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; [ ...
- MariaDB忘记root密码
在MariaDB配置文件/etc/my.cnf [mysqld]中加入skip-grant-tables一行: [Richard@localhost ~]$ sudo vi /etc/my.cnf[ ...
- C# - 重定义一个接口的实现
using System;using System.Collections.Generic;using System.Text; namespace MyTester{ public inter ...
- PHP获取中文汉字首字母方法
function getFirstLetter($str){ $fchar = ord($str{0}); if($fchar >= ord("A") and $fchar ...
- EBS OAF中如何在多行表中实现附件功能
EBS OAF中如何在多行表中实现附件功能 (版权声明,本人原创或者翻译的文章如需转载,如转载用于个人学习,请注明出处:否则请与本人联系,违者必究) 在OAF中使用附件功能之前,要先明白Entity( ...
- 怎样在Eclipse中使用debug模式调试程序
最基本的操作是: 1, 首先在一个java文件中设断点,然后运行,当程序走到断点处就会转到debug视图下, 2, F5键与F6键均为单步调试,F5是step into,也就是进入本行代码中执行,F6 ...
- Ubuntu 12.04 Android2.2源码make** /classes-full-debug.jar Error 41错误解决
出现make: *** [out/target/common/obj/APPS/CMParts_intermediates/classes-full-debug.jar] Error 41这样的错误最 ...
- SLC和MLC闪存芯片的区别
许多人对闪存的SLC和MLC区分不清.就拿目前热销的MP3随身听来说,是买SLC还是MLC闪存芯片的呢?在这里先告诉大家,如果你对容量要求不高,但是对机器质量.数据的安全性.机器寿命等方面要求较高,那 ...