当前位置:网站首页>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;
}
边栏推荐
- Nacos installation and service registration
- Overview of concurrency control
- QT creator 7 beta release
- Navigation day answer applet: preliminary competition of navigation knowledge competition
- [groovy] mop meta object protocol and meta programming (Introduction to groovyobject interface | introduction to metaclass | implementation of class methods using groovyobject invokemethod)
- Comment développer un plug - in d'applet
- Depth first DFS and breadth first BFS -- traversing adjacency tables
- Leetcode simple question: find the nearest point with the same X or Y coordinate
- 2022 Software Test Engineer salary increase strategy, how to reach 30K in three years
- Opencv judgment points are inside and outside the polygon
猜你喜欢

MCU case -int0 and INT1 interrupt count

Business learning of mall order module

Cobaltstrike builds an intranet tunnel

Solutions for unexplained downtime of MySQL services

笔记本电脑蓝牙怎么用来连接耳机

Storage optimization of performance tuning methodology

Postman核心功能解析-参数化和测试报告

Performance monitoring of database tuning solutions

Nacos 的安装与服务的注册

What about data leakage? " Watson k'7 moves to eliminate security threats
随机推荐
Go language learning tutorial (XV)
EasyCVR集群部署如何解决项目中的海量视频接入与大并发需求?
Draw a red lantern with MATLAB
MCU case -int0 and INT1 interrupt count
U盘的文件无法删除文件怎么办?Win11无法删除U盘文件解决教程
Postman核心功能解析-参数化和测试报告
从 1.5 开始搭建一个微服务框架——日志追踪 traceId
Golang writes the opening chapter of selenium framework
南京:全面启用商品房买卖电子合同
Stored procedures and stored functions
Concurrency control of performance tuning methodology
Metaverse Ape获Negentropy Capital种子轮融资350万美元
Oracle hint understanding
The code generator has deoptimised the styling of xx/typescript.js as it exceeds the max of 500kb
FBO and RBO disappeared in webgpu
科技云报道:算力网络,还需跨越几道坎?
Two stage locking protocol for concurrency control
2022 Software Test Engineer salary increase strategy, how to reach 30K in three years
Leetcode simple question ring and rod
QT creator 7-cmake update