当前位置:网站首页>756. 蛇形矩阵
756. 蛇形矩阵
2022-07-26 09:05:00 【Hunter_Kevin】
题目
输入两个整数 n 和 m,输出一个 n 行 m 列的矩阵,将数字 1 到 n×m 按照回字蛇形填充至矩阵中。
具体矩阵形式可参考样例。
输入格式
输入共一行,包含两个整数 n 和 m。
输出格式
输出满足要求的矩阵。
矩阵占 n 行,每行包含 m 个空格隔开的整数。
数据范围
1≤n,m≤100
输入样例:
3 3
输出样例:
1 2 3
8 9 4
7 6 5
代码
#include <iostream>
using namespace std;
int q[110][110];
int main()
{
int n, m;
cin >> n >> m;
int dx[] = {
-1,0,1,0}, dy[] = {
0,1,0,-1};
int d = 1;
int x = 0, y = 0;
for(int i = 1; i <= n*m; i++)
{
q[x][y] = i;
int ne_x = x + dx[d], ne_y = y + dy[d];
if(ne_x < 0 || ne_x >= n || ne_y < 0 || ne_y >= m || q[ne_x][ne_y])
{
d = (d+1) % 4;
ne_x = x+dx[d], ne_y = y+dy[d];
}
x = ne_x, y = ne_y;
}
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
printf("%d ",q[i][j]);
printf("\n");
}
return 0;
}
边栏推荐
- 【ARKit、RealityKit】把图片转为3D模型
- unity简易消息机制
- Study notes of automatic control principle -- dynamic model of feedback control system
- 数据库操作 技能6
- 论文笔记: 知识图谱 KGAT (未完暂存)
- 2022茶艺师(中级)特种作业证考试题库模拟考试平台操作
- 力扣刷题,三数之和
- Introduction to AWD attack and defense competition
- Cve-2021-26295 Apache OFBiz deserialization Remote Code Execution Vulnerability recurrence
- 十大蓝筹NFT近半年数据横向对比
猜你喜欢
209. Subarray with the smallest length
2022流动式起重机司机考试题模拟考试题库模拟考试平台操作
Study notes of automatic control principle -- dynamic model of feedback control system
Pop up window in Win 11 opens with a new tab ---firefox
Two tips for pycharm to open multiple projects
2022年上海市安全员C证考试试题及模拟考试
TCP solves the problem of short write
十大蓝筹NFT近半年数据横向对比
day06 作业--技能题6
Grain College of all learning source code
随机推荐
Matlab 绘制阴影误差图
Kotlin properties and fields
What is the difference between NFT and digital collections?
idea快捷键 alt实现整列操作
QtCreator报错:You need to set an executable in the custom run configuration.
PHP 之 Apple生成和验证令牌
Regular expression: judge whether it conforms to USD format
李沐d2l(四)---Softmax回归
Which of count (*), count (primary key ID), count (field) and count (1) in MySQL is more efficient? "Suggested collection"
Day06 homework - skill question 7
Study notes of automatic control principle -- dynamic model of feedback control system
2022茶艺师(中级)特种作业证考试题库模拟考试平台操作
力扣题DFS
The lessons of 2000. Web3 = the third industrial revolution?
Advanced mathematics | Takeshi's "classic series" daily question train of thought and summary of error prone points
(2006,Mysql Server has gone away)问题处理
[eslint] Failed to load parser ‘@typescript-eslint/parser‘ declared in ‘package. json » eslint-confi
zsh: command not found: nvm
Sklearn machine learning foundation (linear regression, under fitting, over fitting, ridge regression, model loading and saving)
ONTAP 9文件系统的限制