当前位置:网站首页>蛇形矩阵(数组模拟方向, d代表转弯)
蛇形矩阵(数组模拟方向, d代表转弯)
2022-06-30 00:00:00 【山岚~】
- 蛇形矩阵

#include <iostream>
using namespace std;
const int N = 110;
int n, m;
int q[N][N];
int main()
{
cin >> n >> m;
int dx[] = {
-1, 0, 1, 0}, dy[] = {
0, 1, 0, -1};
int x = 0, y = 0, d = 1;
for (int i = 1; i <= n * m; i ++ )
{
q[x][y] = i;
int a = x + dx[d], b = y + dy[d];
if (a < 0 || a >= n || b < 0 || b >= m || q[a][b])
{
d = (d + 1) % 4;
a = x + dx[d], b = y + dy[d];
}
x = a, y = b;
}
for (int i = 0; i < n; i ++ )
{
for (int j = 0; j < m; j ++ )
cout << q[i][j] << ' ';
cout << endl;
}
return 0;
}

撞墙代表着出界或者已经走过了 ,d代表什么时候转弯;
边栏推荐
- How to realize the spelling correction function in search engine
- 數莓派 4怎麼樣?可能的玩法有哪些?
- 西门子低代码平台通过Database Connector 连接Mysql 实现增删改查
- 【微信小程序】认识小程序项目的基本组成结构
- Getting started with qpainter: drawing the chess interface
- QT learning 02 GUI program example analysis
- 爬虫入门实战:斗鱼弹幕数据抓取,附送11节入门笔记
- About mongodb error: connecting to: mongodb://127.0.0.1:27017/?compressors=disabled &gssapiServiceName=mongodb
- Will the flush SQL CDC parallelism affect the order? Generally, only 1 can be set for data synchronization.
- Matlab exercises -- program control process exercise
猜你喜欢

New CorelDRAW technical suite2022 latest detailed function introduction

EB-5 immigration in the United States reappears to be positive, and the reauthorization policy of the regional center is suspended

Serialization of binary tree 297 Serialization and deserialization of binary tree 652 Find duplicate subtrees

Et la tarte aux framboises 4? Quels sont les jeux possibles?

Binary search tree 230 The element with the smallest K in the binary search tree 1038 From binary search tree to larger sum tree

6.29 problem solving
![Cloner un Graphe non recté [bfs accède à chaque bord et pas seulement aux noeuds]](/img/34/2a1b737b6095293f868ec6aec0ceeb.png)
Cloner un Graphe non recté [bfs accède à chaque bord et pas seulement aux noeuds]

Embedded development: Hardware in the loop testing

The role of VMware virtual machine

Andorid source build/envsetup.sh 该知道的细节
随机推荐
Golang泛型的巧妙应用,防止变量空指针错误,防止结构体字段空指针错误
Binary search tree 230 The element with the smallest K in the binary search tree 1038 From binary search tree to larger sum tree
Cacti maximum monitoring number test
500 error occurred after importing skins folder into solo blog skin
Solr基础操作16
Ingenious application of golang generics to prevent null pointer errors of variables and structural fields
Root cause of glideexception: failed decodepath{directbytebuffer- > gifdrawable- > drawable}
Stack space of JVM
Leetcode (633) -- sum of squares
Solr basic operation 10
Exploration and Practice on the future direction of byte cloud database
Solr基础操作7
Inspiration collection · evaluation of creative writing software: flomo, obsidian memo, napkin, flowus
333333333333333333333333333333
Copy linked list with random pointer [space for time --hash record]
QT learning 06 widgets and window types
漫画安全HIDS、EDR、NDR、XDR
Rotating colored clover
How about counting Berry Pie 4? What are the possible ways to play?
MySQL multi table query