当前位置:网站首页>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;
}
边栏推荐
- 119. Pascal‘s Triangle II. Sol
- Matlab draws a cute fat doll
- Leetcode simple question: check whether each row and column contain all integers
- Golang writes the opening chapter of selenium framework
- Nanjing: full use of electronic contracts for commercial housing sales
- Platform bus
- EasyCVR集群部署如何解决项目中的海量视频接入与大并发需求?
- QT creator 7 beta release
- 2022 Software Test Engineer salary increase strategy, how to reach 30K in three years
- ESP32 hosted
猜你喜欢
Sparse array [matrix]
Leetcode simple question: check whether each row and column contain all integers
航海日答题小程序之航海知识竞赛初赛
Server optimization of performance tuning methodology
数博会精彩回顾 | 彰显科研实力,中创算力荣获数字化影响力企业奖
CA certificate trampled pit
Common interview questions of redis factory
Distributed resource management and task scheduling framework yarn
Post-90s tester: "after joining Ali, this time, I decided not to change jobs."
Wonderful review of the digital Expo | highlight scientific research strength, and Zhongchuang computing power won the digital influence enterprise award
随机推荐
Sub total of Pico development
Learning of mall permission module
344. Reverse String. Sol
链表之双指针(快慢指针,先后指针,首尾指针)
Business learning of mall order module
ESP32 hosted
A substring with a length of three and different characters in the leetcode simple question
The code generator has deoptimised the styling of xx/typescript. js as it exceeds the max of 500kb
EasyCVR集群部署如何解决项目中的海量视频接入与大并发需求?
Record several frequently asked questions (202207)
Leetcode simple question: find the nearest point with the same X or Y coordinate
南京:全面启用商品房买卖电子合同
How can easycvr cluster deployment solve the massive video access and concurrency requirements in the project?
U盘的文件无法删除文件怎么办?Win11无法删除U盘文件解决教程
GWT module may need to be (RE) compiled reduce - GWT module may need to be (RE) compiled reduce
Performance monitoring of database tuning solutions
分布式解决方案之TCC
Nanjing: full use of electronic contracts for commercial housing sales
【无标题】
Metaverse Ape猿界应邀出席2022·粤港澳大湾区元宇宙和web3.0主题峰会,分享猿界在Web3时代从技术到应用的文明进化历程