首页 > 其他分享 >POJ3278 Catch That Cow

POJ3278 Catch That Cow

时间:2024-07-24 22:08:10浏览次数:17  
标签:Cow int POJ3278 bfs tra step iswalk Catch FJ

Catch That Cow

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 222142   Accepted: 67092

Description

Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting.

* Walking: FJ can move from any point X to the points - 1 or + 1 in a single minute
* Teleporting: FJ can move from any point X to the point 2 × X in a single minute.

If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?

Input

Line 1: Two space-separated integers: N and K

Output

Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.

Sample Input

5 17

Sample Output

4

Hint

The fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes.     题目大意: FJ抓位置固定不动的奶牛(二者均在一条直线上),FJ和奶牛位置分别为输入的N(0 ≤ N ≤ 100,000)和K(0 ≤ N ≤ 100,000),FJ有两种走法

*步行:FJ可以在一分钟内从任何一个点X移动到点X-1或X+1

*传送:FJ可以在一分钟内从任何一个X点移动到2*X点

输出FJ抓带到奶牛需要的最短时间,以分钟为单位

 

思路:

明显的BFS,几年没摸,重新回顾BFS,手写结构体构造先进先出的队列,

AC代码:

 1 #include<stdio.h>
 2 int iswalk[100050];
 3 int p;//实时移动数组下标,视作队尾添加
 4 int q;//取数,视作队头出队
 5 int N, K;
 6 struct trajectory
 7 {
 8     int step;
 9     int num;
10 };
11 struct trajectory tra[100050];
12 int main()
13 {
14     scanf("%d %d", &N, &K);
15     tra[q].num = N;
16     tra[q].step = 0;
17     iswalk[N] = 1;
18     while(q <= p)
19     {
20         if(tra[q].num == K){
21             printf("%d\n", tra[q].step);
22             break;
23         }
24         else
25         {
26             if(iswalk[tra[q].num - 1] == 0 && (tra[q].num - 1 >= 0) && (tra[q].num - 1) <= 100000){
27                 iswalk[tra[q].num - 1] = 1;
28                 p ++;
29                 tra[p].num = tra[q].num - 1;
30                 tra[p].step = tra[q].step + 1;
31             }
32             if(iswalk[tra[q].num + 1] == 0 && (tra[q].num + 1 >= 0) && (tra[q].num + 1) <= 100000){
33                 iswalk[tra[q].num + 1] = 1;
34                 p ++;
35                 tra[p].num = tra[q].num + 1;
36                 tra[p].step = tra[q].step + 1;
37             }
38             if(iswalk[tra[q].num * 2] == 0 && (tra[q].num * 2) >= 0 && (tra[q].num * 2) <= 100000){
39                 iswalk[tra[q].num * 2] = 1;
40                 p ++;
41                 tra[p].num = tra[q].num * 2;
42                 tra[p].step = tra[q].step + 1;
43             }
44 
45             q ++;
46         }
47     }
48 }

 

 

起手错误写法:

一:for的DFS(POJ3984的代码,都是BFS,于是推翻并找到奶牛这个基础题来写)

 1 #include<stdio.h>
 2 int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
 3 int bfs(int x, int y);
 4 int maze[5][5];
 5 int step;
 6 int evestep[25][2];//记录轨迹
 7 int OnGo[5][5];//是否走过这个点
 8 int main()
 9 //int dir[4][2] = {(-1, 0), (1, 0), (0, -1), (0, 1)};
10 {
11 //    int step;
12     for(int i = 0; i < 5; i ++)
13         for(int j = 0; j < 5; j ++)
14             scanf("%d", &maze[i][j]);
15     int x = 0, y = 0;
16     OnGo[0][0] == 1;
17 //    int bfs(x, y);
18     bfs(x, y);
19 }
20 int bfs(int x, int y)
21 {
22     for(int i = 0; i < 4; i ++)
23     {
24         x += dir[i][0];
25         y += dir[i][1];
26         if(maze[x][y] == 1 || x < 0 || y < 0 || x > 4 || y > 4)
27             continue;
28         else if(maze[x][y] == 0 && OnGo[x][y] == 0)
29         {
30             OnGo[x][y] == 1;
31             step++;
32             evestep[step][0] = x;
33             evestep[step][1] = y;
34             bfs(x, y);
35         }
36     }
37 //}
38 //推翻重写,昨天写的是DFS
39 
40 //BFS
View Code

二:推翻重写后,依旧是DFS的思路,以为BFS会携带每一层的step

 1 #include<stdio.h>
 2 int tra[100050];
 3 int iswalk[100050];
 4 int p, q;
 5 int bfs(int FJ_x, int step)
 6 {
 7     if(FJ_x == K)
 8         return step;
 9     else
10     {
11         if((FJ_x - 1) >= 0 && iswalk[FJ_x - 1] == 0){
12             iswalk[FJ_x - 1] = 1;
13             step ++;
14             bfs(FJ_x - 1, step)
15         }
16         if((FJ_x + 1) >= 0 && iswalk[FJ_x + 1] == 0){
17             iswalk[FJ_x + 1] = 1;
18             step ++;
19             bfs(FJ_x + 1, step)
20         }
21         if((FJ_x * 2) >= 0 && iswalk[FJ_x * 2] == 0){
22             iswalk[FJ_x * 2] = 1;
23             step ++;
24             bfs(FJ_x * 2, step)
25         }
26     }
27 }
View Code

 

标签:Cow,int,POJ3278,bfs,tra,step,iswalk,Catch,FJ
From: https://www.cnblogs.com/gerjcs/p/18307728

相关文章

  • 题解:P10450 [USACO03MAR] Best Cow Fences G
    题目链接O(n^3)做法直接暴力枚举长度、起点,再全部跑一边求平均数。附上我丑陋的代码和提交记录,这个代码可以得42分。#include<bits/stdc++.h>usingnamespacestd;constintNR=1e5+5;longlongn,l,a[NR],sum,ave;intmain(){ cin>>n>>l; for(inti......
  • P10280 [USACO24OPEN] Cowreography G 题解
    Description奶牛们组了一支舞蹈队,FarmerJohn是她们的编舞!舞蹈队最新而最精彩的舞蹈有\(N\)头奶牛(\(2\leN\le10^6\))排成一行。舞蹈中的每次动作都涉及两头奶牛,至多相距\(K\)个位置(\(1\leK<N\)),优雅地跳起并降落在对方的位置上。队伍中有两种奶牛——更赛牛(Guernsey)和荷......
  • P3089 [USACO13NOV] Pogo-Cow S
    原题链接题解暴力dp:遍历\(i,j,k\),\(dp[i][j]=\max(dp[j][k])+v_i\)其中\(x_i-x_j\geqx_j-x_k\)优化:对于\(j\)来说,随着\(i\)越大,\(k\)可以越小,因此省去了遍历一层\(k\),而是维护每个点的\(k\),(反正求的是最大值)细节1.有两个方向2.任意起点code#include<bit......
  • mailcow邮件服务器的安全防护措施有哪些?
    mailcow邮件服务器如何搭建?邮件服务器的优势特点?mailcow邮件服务器是一款功能强大的开源电子邮件服务器套件,旨在为用户提供高效、安全的邮件服务。为了确保邮件服务器的安全,mailcow邮件服务器采取了一系列的安全防护措施,AokSend将详细探讨这些措施。mailcow邮件服务器:访问控......
  • CF1990E Catch the Mole
    题意给你一颗树,大小为\(n\)。初始有一颗黑点在树上某个节点,你每次可以查询\(x\)表示黑点是否在\(x\)的子树内,且若答案为否则黑点会移动到父亲节点上。你需要在160次查询内找到黑点当前在哪个节点(不要求求出初始位置)。\(n\le5000\),EasyVer.查询次数300。分析由于每......
  • qcow2恢复案例
    确认文件和进程信息:使用lsof命令确认虚拟机进程正在使用已删除的qcow2文件,并记录文件描述符和虚拟机进程ID(例如3914和11u)。sudolsof|grepdeleted复制文件内容:使用文件描述符路径复制已删除的qcow2文件到一个新的目标位置。例如,假设文件描述符路径为/proc/3914/fd/11,将文件......
  • 题解:SP11469 SUBSET - Balanced Cow Subsets
    SP11469(折半搜索)题目大意:给出$N$个数,求可以被分成两个和相等的集合的子集数。思路分析:首先考虑朴素的DFS,每个数有三种情况:选为$A$集合,选为$B$集合,两个集合都不选。暴力DFS时间复杂度为$3^{20}$。观察到$N$很小,而$3^{10}$是可以通过本题的,于是考虑折半搜索。我......
  • 使用Docker部署mailcow开源邮件系统详细过程
    1.项目介绍项目网站:mailcow:dockerized–Blog根据官方介绍,这个项目名称是mailcow,名称都是小写的。下面内容是通过AI翻译自官方文档:mailcow:dockerizeddocumentationmailcow:dockerized是一个基于Docker的开源组件/电子邮件套件。mailcow依赖于许多广为人知且长期......
  • then catch 简易写法
    为了捕获上一步then中的promise结果,必须在上一步return;关闭遮罩层,放在finally中,即无论成功或失败都要执行;archiveAction(actionType,row){constids=row&&row.id?row.id:this.ids;consttip=row&&row.id?`“${row.projectName}”`:......
  • Cows in a Skyscraper G
    dfs版本#include<algorithm>#include<iostream>usingnamespacestd;constintN=2e1;intcat[N],cab[N];intn,w;intans;boolcmp(inta,intb){returna>b;}voiddfs(intnow,intcnt){if(cnt>=ans){re......