PAT 甲级:1105. Spiral Matrix

这道题把N个数从大到小排列,然后按照顺时针的方向,写到一个矩形中。

这道题主要是模拟顺时针填满矩形这个过程。

这道题的核心在于定义了四个访问的方向,右,下,左,上。

	int y[4]={1,0,-1,0};
	int x[4]={0,1,0,-1};

按照这个方向走,如果遇到边界,则返回上一个访问的点,换一个方向走。
所以要写一个边界检测的函数,判断该点是否需要访问。
bool judge(int i,int j){
	if(vis[i][j]==true||i>=m||j>=n||i<0||j<0) return false;
	else return true;
}

以下是全部的代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<string.h>
#include<stack>
#include<string>
#include<math.h>
using namespace std;
const int maxn=1001;
int spiral[maxn][maxn];
bool vis[maxn][maxn];
int m,n;//列0---n-1,行0---m-1
bool judge(int i,int j){
	if(vis[i][j]==true||i>=m||j>=n||i<0||j<0) return false;
	else return true;
}
int main(){
	int N;
	scanf("%d",&N);
	//找到m,n
	fill(spiral[0],spiral[0]+maxn*maxn,-1);
	int i;
	for(i=(int)sqrt(1.0*N);i>=1;i--){
		if(N%i==0) break;
	}
	n=i,m=N/i;
	priority_queue<int> in;
	for(int i=0;i<N;i++){
		int d;
		scanf("%d",&d);
		in.push(d);
	}
	//列0---n-1,行0---m-1
	int y[4]={1,0,-1,0};
	int x[4]={0,1,0,-1};
	int h=0,l=-1;//定义行列的指针
	int index=0;
	while(in.size()>0){
		h+=x[index];
		l+=y[index];
		if(judge(h,l)){//可以访问,放入数据
			vis[h][l]=true;
			spiral[h][l]=in.top();
			in.pop();	
		}
		else{//碰壁,返回
			h-=x[index];
			l-=y[index];
			index++;
			index=index%4;
		}
	}
	for(int a=0;a<m;a++)
		for(int b=0;b<n;b++){
			printf("%d",spiral[a][b]);
			if(b<n-1) printf(" ");
			else printf("\n");
		}
	return 0;
}



class SpiralIterator: def init(self, source, x=810, y=500, length=None): self.source = source self.row = np.shape(self.source)[0]#第一个元素是行数 self.col = np.shape(self.source)[1]#第二个元素是列数 if length: self.length = min(length, np.size(self.source)) else: self.length = np.size(self.source) if x: self.x = x else: self.x = self.row // 2 if y: self.y = y else: self.y = self.col // 2 self.i = self.x self.j = self.y self.iteSize = 0 geo_transform = dsm_data.GetGeoTransform() self.x_origin = geo_transform[0] self.y_origin = geo_transform[3] self.pixel_width = geo_transform[1] self.pixel_height = geo_transform[5] def hasNext(self): return self.iteSize < self.length # 不能取更多值了 def get(self): if self.hasNext(): # 还能再取一个值 # 先记录当前坐标的值 —— 准备返回 i = self.i j = self.j val = self.source[i][j] # 计算下一个值的坐标 relI = self.i - self.x # 相对坐标 relJ = self.j - self.y # 相对坐标 if relJ > 0 and abs(relI) < relJ: self.i -= 1 # 上 elif relI < 0 and relJ > relI: self.j -= 1 # 左 elif relJ < 0 and abs(relJ) > relI: self.i += 1 # 下 elif relI >= 0 and relI >= relJ: self.j += 1 # 右 #判断索引是否在矩阵内 x = self.x_origin + (j + 0.5) * self.pixel_width y = self.y_origin + (i + 0.5) * self.pixel_height z = val self.iteSize += 1 return x, y, z dsm_path = 'C:\sanwei\jianmo\Productions\Production_2\Production_2_DSM_part_2_2.tif' dsm_data = gdal.Open(dsm_path) dsm_array = dsm_data.ReadAsArray() spiral_iterator = SpiralIterator(dsm_array,x=810,y=500) while spiral_iterator.hasNext(): x, y, z = spiral_iterator.get() print(f'Value at ({x},{y}):{z}')这段代码怎么改可以将地面点坐标反算其原始航片对应的像素行列号
06-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值