北大poj-2688
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 4395 | Accepted: 1763 |
Description
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
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
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的更多相关文章
- poj 2688 状态压缩dp解tsp
题意: 裸的tsp. 分析: 用bfs求出随意两点之间的距离后能够暴搜也能够用next_permutation水,但效率肯定不如状压dp.dp[s][u]表示从0出发訪问过s集合中的点.眼下在点u走过 ...
- 北大POJ题库使用指南
原文地址:北大POJ题库使用指南 北大ACM题分类主流算法: 1.搜索 //回溯 2.DP(动态规划)//记忆化搜索 3.贪心 4.图论 //最短路径.最小生成树.网络流 5.数论 //组合数学(排列 ...
- poj 2688 Cleaning Robot bfs+dfs
题目链接 首先bfs, 求出两两之间的距离, 然后dfs就可以. #include <iostream> #include <cstdio> #include <algo ...
- POJ 2688 Cleaning Robot
题意: 给你一个n*m的图.你从'o'点出发,只能走路(图中的'.')不能穿墙(图中的'x'),去捡垃圾(图中的' * ')问最少走多少步能捡完所有垃圾,如有垃圾捡不了,输出-1. 思路: 有两个思路 ...
- Cleaning Robot POJ - 2688
题目链接:https://vjudge.net/problem/POJ-2688 题意:在一个地面上,有一个扫地机器人,有一些障碍物,有一些脏的地砖,问,机器热能不能清扫所有的地砖, (机器人不能越过 ...
- 【Java】深深跪了,OJ题目Java与C运行效率对比(附带清华北大OJ内存计算的对比)
看了园友的评论之后,我也好奇清橙OJ是怎么计算内存占用的.重新测试的情况附在原文后边. -------------------------------------- 这是切割线 ----------- ...
- POJ 1861 Network (Kruskal算法+输出的最小生成树里最长的边==最后加入生成树的边权 *【模板】)
Network Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14021 Accepted: 5484 Specia ...
- 各大OJ
北大POJ 杭电HDU 浙大ZOj 蓝桥杯 PAT
- leetcode学习笔记--开篇
1 LeetCode是什么? LeetCode是一个在线的编程测试平台,国内也有类似的Online Judge平台.程序开发人员可以通过在线刷题,提高对于算法和数据结构的理解能力,夯实自己的编程基础. ...
- OJ题目JAVA与C运行效率对比
[JAVA]深深跪了,OJ题目JAVA与C运行效率对比(附带清华北大OJ内存计算的对比) 看了园友的评论之后,我也好奇清橙OJ是怎么计算内存占用的.重新测试的情况附在原文后边. ----------- ...
随机推荐
- nodejs解决找不到express命令的问题
一般的书或者教程上的安装步骤是:(需要是-g,即全局安装) npm install -g express //全局安装 而我们应该多多关注下express的文档,github地址:https://gi ...
- 前端优化之图片延迟加载(lazyload.js)
要想缩短首屏加载时间,思路一般是减少http请求次数和降低每次的请求量.本文中使用现成的lazyload.js插件,文末会放出下载地址. lazyload.js可以实现图片分批次加载,不是一次性加载完 ...
- zend studio 9 字体,颜色,快捷键等相关设置
1.zend studio 9可以破解吗? 可以的,具体破解步骤查看:http://www.geekso.com/ZendStudio9-key/ 2.如何将zend studio 9的默认GBK编码 ...
- 微信支付之扫码支付开发:我遇到的坑及解决办法(附:Ecshop 微信支付插件)
前段时间帮一个朋友的基于ecshop开发的商城加入微信扫描支付功能,本以为是很简单的事儿——下载官方sdk或开发帮助文档,按着里面的做就ok了,谁知折腾了两三天的时间才算搞定,中间也带着疑问在网上找了 ...
- 【FPGA】【代码】资源优化,结构优化
资源优化 原始资源 定义时初始化和复位电路初始化都起作用,删除定义的初值后 将always块描述的组合逻辑变为时序逻辑后
- 自制c#简易计算器
这是一个课堂作业,我觉得作为一个简易的计算器不需要态度复杂的东西,可能还有一些bug,有空再慢慢加强. using System;using System.Collections.Generic;us ...
- Spark机器学习读书笔记-CH05
5.2.从数据中提取合适的特征 [root@demo1 ch05]# sed 1d train.tsv > train_noheader.tsv[root@demo1 ch05]# lltota ...
- Oracle之内存结构(SGA、PGA)
一.内存结构 SGA(System Global Area):由所有服务进程和后台进程共享: PGA(Program Global Area):由每个服务进程.后台进程专有:每个进程都有一个PGA. ...
- Three.js学习(相机,场景,渲染,形状)
相机分为透视相机和正交相机(还有第三人称相机不介绍). var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window. ...
- JQuery 判断不同浏览器
if($.browser.msie) { //IE浏览器alert("this is msie"); } else if($.browser.safari) ...