当前位置:网站首页>Breadth first search to catch cattle
Breadth first search to catch cattle
2022-06-29 00:46:00 【chengqiuming】
One Link to original question
3278 -- Catch That Cow
http://poj.org/problem?id=3278
Two Input and output
1 Input
Two Numbers , The first 1 The number represents the position of the farmer , The first 2 Number represents the position of cattle
2 Output
The minimum number of steps for a farmer to catch an ox
3、 ... and Input and output examples
1 sample input
5 17
2 sample output
4
Four Code
package graph.poj3278;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class POJ3278BFS {
static final int MAXN = 100009;
static boolean vis[] = new boolean[MAXN];
static int d[] = new int[MAXN];
static int n, k;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
k = scanner.nextInt();
if (k <= n) {
System.out.println(n - k);
return;
}
solve();
}
static void solve() {
Queue<Integer> q = new LinkedList<>();
vis[n] = true;
d[n] = 0;
q.add(n);
while (!q.isEmpty()) {
int u = q.peek();
q.poll();
if (u == k) {
System.out.println(d[k]);
return;
}
int x;
x = u + 1;
if (x >= 0 && x <= 100000 && !vis[x]) { // Take a step forward
d[x] = d[u] + 1;
vis[x] = true;
q.add(x);
}
x = u - 1;
if (x >= 0 && x <= 100000 && !vis[x]) { // Take a step back
d[x] = d[u] + 1;
vis[x] = true;
q.add(x);
}
x = u * 2;
if (x >= 0 && x <= 100000 && !vis[x]) { // Jump walk
d[x] = d[u] + 1;
vis[x] = true;
q.add(x);
}
}
}
}
5、 ... and test
Green is the input , White is the output .
![]()
边栏推荐
- 光纤滑环价格过高的原因
- Blazor University (34)表单 —— 获得表单状态
- [communication] wide band source DOA estimation method based on incoherent signal subspace (ISM)
- Ensemble de données sur les visages masqués et méthode de génération des visages masqués
- 利用verilogA模块采样
- Daily practice: delete duplicates in the ordered array
- Analysis of basic structure and working principle of slip ring
- 10. Yolo series
- 同期群分析是什么?教你用 SQL 来搞定
- Cross domain problem of canvas drawing caused by background image cache
猜你喜欢

Reference materials in the process of using Excel

盘点 6 月 yyds 的开源项目!

FSS object storage how to access the Intranet

MySQL 8.0 above reporting 2058 solution
【架构师(第三十八篇)】 服务端开发之本地安装最新版 MySQL 数据库

pinhole camera model

矩 阵 压 缩

What is redis

Difference between applying for trademark in the name of individual and company

12. object detection mask RCNN
随机推荐
Introduction to JVM working principle
Daily question 1: the number of numbers in the array
毕业三年的25岁码农总结
JDBC连接、断开数据库的封装
Blazor University (34)表单 —— 获得表单状态
滑环电机是如何工作的
【leetcode】153. Find the lowest value in the rotation sort array
Matrix compression
Baidu online disk login verification prompt: unable to access this page, or the QR code display fails, the pop-up window shows: unable to access this page, ensure the web address....
Zoom with mouse wheel
[staff] pedal mark (step on pedal ped mark | release pedal * mark | corresponding pedal command in MIDI | continuous control signal | switch control signal)
var、let、const 三者的区别
UI高度自适应的修改方案
Es6:let, const, arrow functions
Program environment and pretreatment
MySQL 8.0 above reporting 2058 solution
FATAL ERROR: Could not find ./ bin/my_ print_ Solutions to defaults
[agile 5.1] core of planning: user stories
Differences among VaR, let and Const
Reprint: VTK notes - clipping and segmentation - irregular closed loop clipping -vtkselectpolydata class (black mountain old demon)