Cleaning Robot
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4395   Accepted: 1763

Description

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

The
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.

Sample 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

Sample Output

8
49
-1 分析:BFS得到邻接矩阵,这样就是一个固定起点的TSP问题,再用递归DFS(也叫回溯法)+剪枝,就可以得到答案。
问题:输入数据是连续的,直到0 0终止。倒腾了好久都是WA,居然是这个原因。。。。
 #include <stdio.h>
#include <stdlib.h> #define MAX_MAX 65535
#define MAX_ROOM 25
#define MAX_DIRT 15
#define Q_LEN 10000 typedef struct
{
int x;
int y;
int step;
}T_Node; const int deltax[] = {-, , , };
const int deltay[] = {, , , -}; T_Node gatDirt[MAX_DIRT];
T_Node queue[Q_LEN];
int gwLen;
int gwWide;
int gwDirtNum = ;
char gawMap[MAX_ROOM][MAX_ROOM];
int gawDist[MAX_DIRT][MAX_DIRT]; int BFS(T_Node *ptStart, T_Node *ptEnd)
{
int head = ;
int tail = ;
int direction = ;
char Map[MAX_ROOM][MAX_ROOM];
queue[head] = *ptStart; int i,j;
for(j=; j<gwLen; j++)
{
for(i=; i<gwWide; i++)
{
Map[j][i] = gawMap[j][i];
}
} Map[ptStart->y][ptStart->x] = 'x';
while(head != tail)
{
for(direction=; direction<; direction++)
{
if(queue[head].x + deltax[direction] <
|| queue[head].x + deltax[direction] >= gwWide
|| queue[head].y + deltay[direction] <
|| queue[head].y + deltay[direction] >= gwLen)
continue;
queue[tail].x = queue[head].x + deltax[direction];
queue[tail].y = queue[head].y + deltay[direction];
if(queue[tail].x == ptEnd->x && queue[tail].y == ptEnd->y)
{
return queue[head].step + ;
}
if(Map[queue[tail].y][queue[tail].x] != 'x')
{
queue[tail].step = queue[head].step + ;
Map[queue[tail].y][queue[tail].x] = 'x';
tail++;
}
}
head++;
}
return -;
} int gawIsCleaned[MAX_DIRT];
int gwBest = MAX_MAX; void DFS(int sum, int position, int deep)
{
int k = ;
int ThisSum = sum;
deep++;
if(deep == gwDirtNum)
if(sum < gwBest)
{
gwBest = sum;
return;
}
for(k=; k<gwDirtNum; k++)
{
if(gawDist[position][k] == || gawIsCleaned[k] ==)
{
continue;
}
sum += gawDist[position][k];
if(sum > gwBest)
break;
gawIsCleaned[position] = ;
DFS(sum, k, deep);
sum = ThisSum;
gawIsCleaned[position] = ;
}
return;
} int main(void)
{
int i,j;
while(scanf("%d %d", &gwWide, &gwLen))
{
getchar();
if(gwWide == || gwLen == )
{
return ;
}
gwDirtNum = ;
for(j=; j<gwLen; j++)
{
for(i=; i<gwWide; i++)
{
scanf("%c", &gawMap[j][i]);
if(gawMap[j][i] == '*')
{
gatDirt[gwDirtNum].x = i;
gatDirt[gwDirtNum].y = j;
gwDirtNum++;
}
if(gawMap[j][i] == 'o')
{
gatDirt[].x = i;
gatDirt[].y = j;
}
}
getchar();
}
for(j=; j<gwDirtNum; j++)
{
for(i=j+; i<gwDirtNum; i++)
{
gawDist[j][i] = BFS(&gatDirt[i], &gatDirt[j]);
if(gawDist[j][i] == -)
{
gwBest = ;
break;
}
if(j != ) gawDist[i][j] = gawDist[j][i];
}
} if(gwBest == )
{
gwBest = MAX_MAX;
printf("-1\n");
}
else
{
gwBest = MAX_MAX;
DFS(, , );
printf("%d\n", gwBest);
}
}
return ;
}


北大poj-2688的更多相关文章

  1. poj 2688 状态压缩dp解tsp

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

  2. 北大POJ题库使用指南

    原文地址:北大POJ题库使用指南 北大ACM题分类主流算法: 1.搜索 //回溯 2.DP(动态规划)//记忆化搜索 3.贪心 4.图论 //最短路径.最小生成树.网络流 5.数论 //组合数学(排列 ...

  3. poj 2688 Cleaning Robot bfs+dfs

    题目链接 首先bfs, 求出两两之间的距离, 然后dfs就可以. #include <iostream> #include <cstdio> #include <algo ...

  4. POJ 2688 Cleaning Robot

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

  5. Cleaning Robot POJ - 2688

    题目链接:https://vjudge.net/problem/POJ-2688 题意:在一个地面上,有一个扫地机器人,有一些障碍物,有一些脏的地砖,问,机器热能不能清扫所有的地砖, (机器人不能越过 ...

  6. 【Java】深深跪了,OJ题目Java与C运行效率对比(附带清华北大OJ内存计算的对比)

    看了园友的评论之后,我也好奇清橙OJ是怎么计算内存占用的.重新测试的情况附在原文后边. -------------------------------------- 这是切割线 ----------- ...

  7. POJ 1861 Network (Kruskal算法+输出的最小生成树里最长的边==最后加入生成树的边权 *【模板】)

    Network Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 14021   Accepted: 5484   Specia ...

  8. 各大OJ

    北大POJ 杭电HDU 浙大ZOj 蓝桥杯 PAT

  9. leetcode学习笔记--开篇

    1 LeetCode是什么? LeetCode是一个在线的编程测试平台,国内也有类似的Online Judge平台.程序开发人员可以通过在线刷题,提高对于算法和数据结构的理解能力,夯实自己的编程基础. ...

  10. OJ题目JAVA与C运行效率对比

    [JAVA]深深跪了,OJ题目JAVA与C运行效率对比(附带清华北大OJ内存计算的对比) 看了园友的评论之后,我也好奇清橙OJ是怎么计算内存占用的.重新测试的情况附在原文后边. ----------- ...

随机推荐

  1. node.js基础 1之 HTTP知识填坑

    http使用流程: http协议 http 客户端发起请求,创建端口 http 服务器在端口监听客户端请求 http 服务器向客户端返回状态和内容 解析域名查找dns(资源)的过程: 1.chrome ...

  2. TortoiseGit编辑全局变量支持https

    在windows,右键,进入tortoisegit的设置窗口,在左边树形菜单选Git,然后店"编辑全局.gid/config"按钮 输入以下文字 [http] sslVerify ...

  3. ID3、C4.5、CART、RandomForest的原理

    决策树意义: 分类决策树模型是表示基于特征对实例进行分类的树形结构.决策树可以转换为一个if_then规则的集合,也可以看作是定义在特征空间划分上的类的条件概率分布. 它着眼于从一组无次序.无规则的样 ...

  4. table表格宽度固定,同时td内容过长也不会被撑开

    table表格宽度固定,同时td内容过长也不会被撑开,设置如下css: table{table-layout:fixed;word-break:break-all;}

  5. phpize建立php扩展 Cannot find config.m4

    centos  php  安装 memcache 扩展的时候   爆 Cannot find config.m4 错误 解决方案参考以下文章 参考文章 http://blog.csdn.net/wgl ...

  6. Web Api 多项目文档生成之SwaggerUI

    SwaggerUI 可以生成不错的文档,但默认只能作用于单个api 项目,研究了一下源码发现只需修改一下SwaggerConfig.cs文件即可支持多API项目 1.使用生成脚本把xml文件复制到AP ...

  7. Scala 的确棒

    我的确认为计算机学院应该开一门 Scala 的语言课程. 在这篇文章中,我会讲述为什么我会有这样的想法,在此之前,有几点我想要先声明一下: 本文无意对编程语言进行评比,我要讲述的主体是为什么你应该学习 ...

  8. iOS_TCP和UDP的详解

    TCP和UDP面试经常被问到,一些初学者也经常问我这种问题,由于TCP协议和UDP协议是基于三次“对话”,解释起来很费劲,所以在这里详细的描述一下自己对TCP协议和UDP协议的理解,如有不妥之处,望指 ...

  9. web前端,移动开发规范概述

    以下规范建议,均是Alloyteam在日常开发过程中总结提炼出的经验,规范具备较好的项目实践,强烈推荐使用 字体设置 使用无衬线字体 body { font-family: "Helveti ...

  10. Github——入门笔记

    一般操作步骤 developer:(你在别人基础上开发) init->clone->add->commit->remote add(origin的地址)->push or ...