Young87

SmartCat's Blog

So happy to code my life!

游戏开发交流QQ群号60398951

当前位置:首页 >跨站数据测试

蛇形矩阵

一、题目

在这里插入图片描述

在这里插入图片描述

二、题解

#include <iostream>
#include <math.h>
#include <algorithm>
#include <cstring>
using namespace std;
#define N 110
int res[N][N];
int n,m;
int main()
{
	cin>>n>>m;
	//01 10 0-1 -10
	int dx[]={0,1,0,-1};
	int dy[]={1,0,-1,0};
	for (int k=1,x=0,y=0,d=0;k<=m*n;k++)
	{
		res[x][y]=k;
		int a=x+dx[d];
		int b=y+dy[d];
		if (a<0 || a>=n || b<0 || b>=m || res[a][b])
		{
			d=(d+1)%4;
			a =x+dx[d], b = y+dy[d];
		}
		x=a,y=b;
	}
	
	for (int i=0;i<n;i++)
	{
		for (int j=0;j<m;j++)
			cout<<res[i][j]<<" ";
		cout<<"\n";
	}

	return 0;
}

除特别声明,本站所有文章均为原创,如需转载请以超级链接形式注明出处:SmartCat's Blog

上一篇: 返璞归真:ZooKeeper内部原理——请求、事务和标识符

下一篇: (二)Jetson Nano开发:Tensorflow下载安装(很多细节)

精华推荐