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. for循环立即执行和不立即执行,js闭包

    <script type="text/javascript" src="jquery-2.1.1.min.js"></script> & ...

  2. .NET 框架程序使用 Win32 API

    .NET 框架程序可以通过静态 DLL 入口点的方式来访问本机代码库.DllImport 属性用于指定包含外部方法的实现的dll 位置.       DllImport 属性定义如下:      na ...

  3. 获取Mac地址

    netapi32.lib#include <NB30.h>; typedef struct _ASTAT_ { ADAPTER_STATUS adapt; NAME_BUFFER Name ...

  4. 基础算法之快速排序Quick Sort

    原理 快速排序(Quicksort)是对冒泡排序的一种改进. 从数列中挑出一个元素,称为"基准"(pivot); 排序数列,所有元素比基准值小的摆放在基准前面,所有元素比基准值大的 ...

  5. poj 3734 Blocks

    ゲート 分析:这题过的人好多,然后大家好像是用矩阵过的(((φ(◎ロ◎;)φ))).我自己是推公式的. 对于任意的有这个式子, 就是先从里面选偶数个涂成两个指定的颜色,再在选出的里面选定涂某种颜色,选 ...

  6. 在Mac上安装Sublime Text3的插件

    首先安装插件管理器Package Control 打开Sublime, 按下快捷键 ctrl+', 然后粘贴下面的代码,然后按回车键: import urllib.request,os; pf = ' ...

  7. Task

    .net 4.0为我们带来了TPL(Task Parallel Library),其中Task相较ThreadPool线程池使用更简单,而且支持线程的取消,完成和失败通知等交互性操作,而这些是Thre ...

  8. Leetcode详解Maximum Sum Subarray

    Question: Find the contiguous subarray within an array (containing at least one number) that has the ...

  9. Handle机制的原理

    Android提供了Handle和Looper来满足线程间的通信.Handle先进先出原则.Looper类用来管理特定线程内对象之间的消息交换(Message Exchange). 1.Looper: ...

  10. django学习

    1.进入目录→创建一个新的项目mysite 命令:E:\MyCode\PythonCode>django-admin startproject mysite 自动生成目录: 2.创建一个APP ...