当前位置:网站首页>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 .
![]()
边栏推荐
- mysql 8.0以上报2058 解决方式
- scp拷贝文件夹
- Go1.18 new feature: discard strings Title Method, a new pit!
- 光纤滑环价格过高的原因
- 2022_ 2_ 16 the second day of learning C language_ Constant, variable
- FATAL ERROR: Could not find ./bin/my_print_defaults的解决办法
- Daily practice: delete duplicates in the ordered array
- oracle 去掉html标签
- Notes on the infrastructure of large websites
- 深度优先搜索实现抓牛问题
猜你喜欢

12. Détection d'objets Mask rcnn

每日一题:消失的数字

Getting started with SQL

手下两个应届生:一个踏实喜欢加班,一个技术强挑活,怎么选??

成功解决(机器学习分割数据问题):ModuleNotFoundError: No module named ‘sklearn.cross_validation‘

盘点 6 月 yyds 的开源项目!

Reasons for high price of optical fiber slip ring

Matrix compression

Document management.

Two fresh students: one is practical and likes to work overtime, and the other is skilled. How to choose??
随机推荐
Mapbox GL loading local publishing DEM data
Daily question 1: the number of numbers in the array
MySQL high availability dual master synchronization
Windows平台下安装MySQL(附:Navicat Premium 12 “使用” 教程)
oracle 去掉html标签
Summary of the 25-year-old Ma Nong who graduated three years ago
Daily English articles, reading accumulation
Is it safe to open an account on great wisdom
Encapsulation of JDBC connection and disconnection database
Difference between applying for trademark in the name of individual and company
Report on the convenient bee Lantern Festival: the first peak sales of pasta products this year; prefabricated wine dumplings became the winners
Sampling with VerilogA module
[image detection] recognition of the front and back of a coin based on texture features with matlab code attached
Résumé de Manon, 25 ans, diplômé en trois ans
搭建单机 nacos 负载均衡ribbon 轮询策略 权重2种方式
UI高度自适应的修改方案
【SV 基础】queue 的一些用法
Operation level smart campus system source code smart campus applet source code + electronic class card + face recognition system
Cross domain problem of canvas drawing caused by background image cache
Excel使用过程中的参考资料