poj 2907 Collecting Beepers dfs
来源:互联网 发布:唱歌的软件 编辑:程序博客网 时间:2024/06/05 03:09
Description
Karel is a robot who lives in a rectangular coordinate system where each place is designated by a set of integer coordinates (x and y). Your job is to design a program that will help Karel pick up a number of beepers that are placed in her world. To do so you must direct Karel to the position where each beeper is located. Your job is to write a computer program that finds the length of the shortest path that will get Karel from her starting position, to each of the beepers, and return back again to the starting position.
Karel can only move along the x and y axis, never diagonally. Moving from one position (i, j) to an adjacent position (i, j + 1), (i, j − 1), (i − 1, j), or (i + 1, j) has a cost of one.
You can assume that Karel’s world is never larger than 20 × 20 squares and that there will never be more than 10 beepers to pick up. Each coordinate will be given as a pair (x, y) where each value will be in the range 1 through the size of that particular direction of the coordinate system.
Input
First there will be a line containing the number of scenarios you are asked to help Karel in. For each scenario there will first be a line containing the size of the world. This will be given as two integers (x-size and y-size). Next there will be one line containing two numbers giving the starting position of Karel. On the next line there will be one number giving the number of beepers. For each beeper there will be a line containing two numbers giving the coordinates of each beeper.
Output
The output will be one line per scenario, giving the minimum distance that Karel has to move to get from her starting position to each of the beepers and back again to the starting position.
Sample Input
110 101 142 35 59 46 5
Sample Output
The shortest path has length 24
题意及分析:
在一个地图里依次访问几个点,然后返回起点。问最短的总路程是多少。
简单来说,就是决定先走哪个点,再走哪个点。说实话,水。
比赛没出,方向完全错了。是个教训。
AC代码:
新把之前的bfs的代码给改了(哎,比赛脑子不灵活),分享一下。当时总调不出来,最后在学长的帮助下给调出来了。
用bfs的话,就相当蠢了。有兴趣可以试一下。
要用状态压缩的方法,用三维数组来判重。之前一直过不了是因为数据有坑点。那就是目标点可能与起点重合(也有可能目标点之间重合)。这样的bfs话就无法找到最短长度了。
调试后的代码如下:
- poj 2907 Collecting Beepers dfs
- POJ 2907 Collecting Beepers DFS+回溯+剪枝
- POJ 2907 Collecting Beepers (DFS+回溯)
- POJ 2907 Collecting Beepers
- POJ-2907(全排列->dfs)(Collecting Beepers)
- POJ 2907 Collecting Beepers 笔记
- POJ Collecting Beepers (dfs 深度搜索)
- poj 2907 Collecting Beepers 邮递员问题暴力解法
- Poj 2907 Collecting Beepers【枚举+最短路径】
- Collecting Beepers(简单的dfs)
- UVa 10496 - Collecting Beepers
- poj 2096 Collecting Bugs
- POJ 2096 Collecting Bugs
- poj 2096 Collecting Bugs
- POJ 2096 Collecting Bugs
- poj 2096 Collecting Bugs
- POJ 2096 Collecting Bugs
- POJ 2096 Collecting Bugs
- 2014.08.13总结
- AC自动机
- optimizer_index_cost_adj和optimizer_index_caching
- uitableView 选择跳转后 再跳回来 颜色不变 问题
- ios客户端发现_华为爱奇艺手机活动开发总结
- poj 2907 Collecting Beepers dfs
- cmake 常用变量和常用环境变量查表手册---整理 .
- fstat函数
- (十一)8天快速掌握Android视频教程_查看与输出日志信息
- iOS 基本编码格式转化
- 编程之美2.14 求数组的子数组之和的最大值
- hdu3788zoj问题
- 我能给的 --- 吴克群
- enum与typedef enum的用法