xlh刷书日记(BFS)

POJ 3278
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 X - 1 or X + 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.

Source
USACO 2007 Open Silver

看到这个题开始想用动态规划,可是想了想还是用bfs比较好,对于一个点有三种转移方法,first+1,first-1和first*2。考虑到时间复杂度的问题,我们要对其进行剪枝,具体方法是判重。还有就是求最小次数,我们可以用批次来记录,记录哪些点是同一批,没找到就是0,发现就是非零。下一批次与上一批次之间相差一。用数组存放,多的不说,直接上代码

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<string.h>
using namespace std;
#define check(k) (k>=0&&k<=100000)
queue<int>q;
int hadfind[200010];
int flag=0,k;
void bfs(int start){
	q.push(start);
	hadfind[start]=1;//记录批次 ,司机批次 
	int first,next;
	while(!q.empty()){
		if(hadfind[k]!=0){
			break;
		}
		 first=q.front();//
		 q.pop();
		 for(int i=0;i<3;i++){
		 	if(i==0) next=first-1;
		 	else if(i==1) next=first+1;
		 	else next=first*2;
		 	if(check(next)&&hadfind[next]==0){
		 		hadfind[next]=hadfind[first]+1;
		 		q.push(next);
			 }
		 }//同层标记同层 
	} 
}
int main(){
	int n;
	memset(hadfind,0,sizeof(hadfind));
	cin>>n>>k;
	bfs(n);
	cout<<hadfind[k]-1<<endl;
	return 0;
}

其实数组还可以开小一点,但寻思着已经ac了就没必要做改动了

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
import os import numpy as np from osgeo import gdal input_folder = 'G:/xianlinhotel/xlh632envi' output_folder = "G:/xianlinhotel/xlh_nir_rg_632envicai" target_width = 1230 target_height = 910 for filename in os.listdir(input_folder): if filename.endswith(".tif"): tif_path = os.path.join(input_folder, filename) tif_dataset = gdal.Open(tif_path) if tif_dataset is not None and tif_dataset.RasterXSize == 1280 and tif_dataset.RasterYSize == 960: data = tif_dataset.ReadAsArray() x_offset = (tif_dataset.RasterXSize - target_width) // 2 y_offset = (tif_dataset.RasterYSize - target_height) // 2 new_data = data[:, y_offset:y_offset+target_height, x_offset:x_offset+target_width] output_path = os.path.join(output_folder, filename) driver = gdal.GetDriverByName("GTiff") new_dataset = driver.Create(output_path, target_width, target_height, tif_dataset.RasterCount, tif_dataset.GetRasterBand(1).DataType) geotransform = tif_dataset.GetGeoTransform() new_geotransform = (geotransform[0] + x_offset * geotransform[1], geotransform[1], geotransform[2], geotransform[3] + y_offset * geotransform[5], geotransform[4], geotransform[5]) new_dataset.SetGeoTransform(new_geotransform) new_dataset.SetProjection(tif_dataset.GetProjection()) for i in range(1, tif_dataset.RasterCount + 1): new_dataset.GetRasterBand(i).WriteArray(new_data[i - 1]) new_dataset = None # 关闭数据集以保存文件和释放资源 print(f"Saved {filename} to {output_path}") else: print(f"{filename} has invalid size or is not a TIFF file.") tif_dataset = None # 关闭数据集以释放资源 详细解释
05-30

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值