当前位置:网站首页>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;
}
边栏推荐
- Unique occurrence times of leetcode simple questions
- Form artifact
- 2022-07-05: given an array, you want to query the maximum value in any range at any time. If it is only established according to the initial array and has not been modified in the future, the RMQ meth
- Three "factions" in the metauniverse
- Two stage locking protocol for concurrency control
- Performance monitoring of database tuning solutions
- Cobaltstrike builds an intranet tunnel
- Leetcode simple question: find the nearest point with the same X or Y coordinate
- All expansion and collapse of a-tree
- Technology cloud report: how many hurdles does the computing power network need to cross?
猜你喜欢

Matlab draws a cute fat doll

90后测试员:“入职阿里,这一次,我决定不在跳槽了”

科技云报道:算力网络,还需跨越几道坎?

What about data leakage? " Watson k'7 moves to eliminate security threats

Search: Future Vision (moving sword)

Alternating merging strings of leetcode simple questions

Win11缺少dll文件怎么办?Win11系统找不到dll文件修复方法

Leetcode simple question: check whether each row and column contain all integers

Metaverse ape received $3.5 million in seed round financing from negentropy capital

Technology cloud report: how many hurdles does the computing power network need to cross?
随机推荐
Learning of mall permission module
Postman核心功能解析-参数化和测试报告
Assign the output of a command to a variable [repeat] - assigning the output of a command to a variable [duplicate]
What if win11 is missing a DLL file? Win11 system cannot find DLL file repair method
Platformio create libopencm3 + FreeRTOS project
Understand the basic concept of datastore in Android kotlin and why SharedPreferences should be stopped in Android
New 3D particle function in QT 6.3
Sparse array [matrix]
如何开发引入小程序插件
链表之双指针(快慢指针,先后指针,首尾指针)
Blocking of concurrency control
1.3 years of work experience, double non naked resignation agency face-to-face experience [already employed]
第一讲:蛇形矩阵
【无标题】
Leetcode simple question check whether all characters appear the same number of times
Nacos installation and service registration
Post-90s tester: "after joining Ali, this time, I decided not to change jobs."
Business learning of mall commodity module
When the industrial Internet era is truly mature, we will look at the emergence of a series of new industrial giants
航海日答题小程序之航海知识竞赛初赛