当前位置:网站首页>Lesson 1: serpentine matrix
Lesson 1: serpentine matrix
2022-07-05 22:28:00 【Fight! Sao Nian!】
subject :AcWing 756. Snake matrix
Enter two integers n and m, Output one n That's ok m Columns of the matrix , The digital 1 To n×m Fill the matrix in the shape of a serpentine .
For the specific matrix form, please refer to the example .
Input format
Enter a total of one line , Contains two integers n and m.
Output format
Output a matrix that meets the requirements .
Matrix occupation n That's ok , Each row contains m An integer separated by spaces .
Data range
1≤n,m≤100
sample input :
3 3
sample output :
1 2 3
8 9 4
7 6 5
Topic analysis :
This question mainly uses x、y The offset
such as (x,y) If you want to go down, it's (x+1,y+0), The same goes for other directions
If you change direction ?
Use an array to save the offsets in four directions , If you change the direction of the array subscript +1 model 4 that will do .
#include <iostream>
using namespace std;
const int N = 105;
int a[N][N];
int dx[]={
0,1,0,-1}; // Right down left up
int dy[]={
1,0,-1,0};
int main()
{
int n,m;
cin>>n>>m;
int x=0,y=0,d=0;
for(int i=1;i<=n*m;i++)
{
a[x][y]=i;
x+=dx[d],y+=dy[d]; // For mobile
if(x<0||y<0||x>=n||y>=m||a[x][y])
{
x-=dx[d],y-=dy[d]; // If the Movement fails, it needs to be restored
d=(d+1)%4;
x+=dx[d],y+=dy[d]; // Move... Again
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)cout<<a[i][j]<<' ';
cout<<endl;
}
return 0;
}
边栏推荐
- Practice: fabric user certificate revocation operation process
- [groovy] mop meta object protocol and meta programming (Introduction to groovyobject interface | introduction to metaclass | implementation of class methods using groovyobject invokemethod)
- Request preview display of binary data and Base64 format data
- EasyCVR集群部署如何解决项目中的海量视频接入与大并发需求?
- Sparse array [matrix]
- Performance monitoring of database tuning solutions
- The statistics of leetcode simple question is the public string that has appeared once
- 如何快速体验OneOS
- 第一讲:蛇形矩阵
- Win11运行cmd提示“请求的操作需要提升”的解决方法
猜你喜欢
Win11 runs CMD to prompt the solution of "the requested operation needs to be promoted"
How to quickly experience oneos
What changes has Web3 brought to the Internet?
谷歌地图案例
Oracle triggers
科技云报道:算力网络,还需跨越几道坎?
Win11运行cmd提示“请求的操作需要提升”的解决方法
點到直線的距離直線的交點及夾角
Talking about MySQL index
[groovy] groovy dynamic language features (automatic type inference of function arguments in groovy | precautions for function dynamic parameters)
随机推荐
Server optimization of performance tuning methodology
Qtquick3d real time reflection
如何开发引入小程序插件
二叉树(二)——堆的代码实现
MySQL actual combat 45 lecture learning (I)
Win11运行cmd提示“请求的操作需要提升”的解决方法
QT creator 7 beta release
Practice: fabric user certificate revocation operation process
抖音__ac_signature
Distance entre les points et les lignes
opencv 判断点在多边形内外
C language - structural basis
The statistics of leetcode simple question is the public string that has appeared once
Granularity of blocking of concurrency control
Win11 runs CMD to prompt the solution of "the requested operation needs to be promoted"
A trip to Suzhou during the Dragon Boat Festival holiday
Unique occurrence times of leetcode simple questions
QT creator 7-cmake update
Thinkphp5.1 cross domain problem solving
了解 Android Kotlin 中 DataStore 的基本概念以及为什么应该停止在 Android 中使用 SharedPreferences