当前位置:网站首页>顺时针打印个数组
顺时针打印个数组
2022-06-13 08:36:00 【First Snowflakes】
顺时针打印个N*N数组:

code:
Just like state machine.
#include <thread>
#include <mutex>
#include <list>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;
enum Dir {
right = 0,
left,
down,
up
};
Dir getNextDirection(const int rowIdx, const int colIdx, const int N, \
Dir currDir, \
vector<vector<bool>>& filled) {
if (currDir == Dir::right) {
if ((colIdx + 1) < N && (!filled[rowIdx][colIdx + 1])) {
return Dir::right;
}
else {
return Dir::down;
}
}else if (currDir == Dir::left) {
if ((colIdx - 1) >= 0 && (!filled[rowIdx][colIdx - 1])) {
return Dir::left;
}
else {
return Dir::up;
}
}else if (currDir == Dir::up) {
if ((rowIdx - 1) >= 0 && (!filled[rowIdx-1][colIdx])) {
return Dir::up;
}
else {
return Dir::right;
}
} else if (currDir == Dir::down) {
if ((rowIdx + 1) < N && (!filled[rowIdx + 1][colIdx])) {
return Dir::down;
}
else {
return Dir::left;
}
}
}
void func(int N) {
if (N < 1) {
return;
}
const int totalNum = N * N;
vector<vector<bool>> filled(N, vector<bool>(N,false));
vector<vector<int>> result(N, vector<int>(N, 0));
Dir currDir = Dir::right;
int currRowIdx = 0;
int currColIdx = 0;
result[currRowIdx][currColIdx] = 0;
filled[currRowIdx][currColIdx] = true;
for (int i = 1; i < totalNum; ++i) {
currDir = getNextDirection(currRowIdx, currColIdx, N, currDir, filled);
if (currDir == Dir::right) {
currColIdx++;
} else if (currDir == Dir::left) {
currColIdx--;
}if (currDir == Dir::up) {
currRowIdx--;
}if (currDir == Dir::down) {
currRowIdx++;
}
result[currRowIdx][currColIdx] = i;
filled[currRowIdx][currColIdx] = true;
if (i == (totalNum - 1)) {
break;
}
}
// print:
std::cout << std::endl;
for (auto& arr : result) {
for (auto& val : arr) {
std::cout << std::setw(5) << val << " ";
}
std::cout << std::endl;
}
}
int main() {
func(5);
return 0;
}
边栏推荐
- Founder of Starbucks: no longer open "public toilets" to non store consumers for safety reasons
- 从零开始-实现JpetStore网站-1-建立项目框架以及项目介绍
- AcWing 1977. Information relay (base ring tree, parallel search set)
- Guidance process and service control
- Phpexcel 10008 error resolution
- Journal d'acquisition du faisceau de fichiers à Elk
- [pychart pit stepping record] prompt after configuring remote operation, [errno 2] no such file or directory
- array_ Pop error: only variables should be passed by reference
- JS to download files in batches
- 4、 Js-es5-i / O
猜你喜欢

How to modify desktop path in win10 system

How about a well-known food material distribution information management system?

4、 Js-es5-i / O

AcWing 1977. Information relay (base ring tree, parallel search set)

Shellshock Attack Lab

2020-12-28

Buuctf web (V)
![WARNING:tornado. access:404 GET /favicon. ICO (172.16.8.1) 1.84ms [with static file settings]](/img/6d/6eef1f0ebcab45e9a209a3b6c62b03.png)
WARNING:tornado. access:404 GET /favicon. ICO (172.16.8.1) 1.84ms [with static file settings]

Buuctf web (III)

How to use annotations in word
随机推荐
How to efficiently manage commodities and inventory in the beverage wholesale industry
Notes on MySQL transaction not automatically submitting
容器概念和云原生
Disk management and disk partition operation
WARNING:tornado.access:404 GET /favicon.ico (172.16.8.1) 1.84ms [附静态文件设置]
Centering problem - the width and height of child elements are known
Buffer Overflow Vulnerability Lab
filebeat采集日志到ELK
Microservice project construction III: automatic code generation
Buuctf web (VI)
HCIP_ Static experiment
Logstash failed to create queue
Document contains question type
Verify the word limit of textarea input box. Only prompt but no submission limit
CCNP_ Summary (Continued)
JS - max. of array cases
Sky background map, navigation page lovefanfan top
JS to get the date in the next seven days of the current date
Reverse order and comparison of strings
JS - array de duplication in the array object case