当前位置:网站首页>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;
}
边栏推荐
- [agc009e] eternal average - conclusion, DP
- The difference between MVVM and MVC
- Platformio create libopencm3 + FreeRTOS project
- Platform bus
- thinkphp5.1跨域问题解决
- opencv 判断点在多边形内外
- Binary tree (II) -- code implementation of heap
- Win11 runs CMD to prompt the solution of "the requested operation needs to be promoted"
- 笔记本电脑蓝牙怎么用来连接耳机
- Hcip day 16
猜你喜欢

Solutions for unexplained downtime of MySQL services

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

Web3为互联网带来了哪些改变?

Server optimization of performance tuning methodology

IIC bus realizes client device

Two stage locking protocol for concurrency control

A substring with a length of three and different characters in the leetcode simple question

航海日答题小程序之航海知识竞赛初赛

2022 Software Test Engineer salary increase strategy, how to reach 30K in three years

Concurrency control of performance tuning methodology
随机推荐
Understand the basic concept of datastore in Android kotlin and why SharedPreferences should be stopped in Android
Distance from point to line intersection and included angle of line
第一讲:蛇形矩阵
Record several frequently asked questions (202207)
【无标题】
点到直线的距离直线的交点及夹角
Oracle is sorted by creation time. If the creation time is empty, the record is placed last
The new content of the text component can be added through the tag_ Config set foreground and background colors
Wonderful review of the digital Expo | highlight scientific research strength, and Zhongchuang computing power won the digital influence enterprise award
Platform bus
Promql demo service
Go语言学习教程(十五)
Damn, window in ie open()
Three "factions" in the metauniverse
Form artifact
All expansion and collapse of a-tree
Solutions for unexplained downtime of MySQL services
Win11缺少dll文件怎么办?Win11系统找不到dll文件修复方法
实战:fabric 用户证书吊销操作流程
Performance monitoring of database tuning solutions