網(wǎng)站首頁 編程語言 正文
前言:
本次蛇形矩陣我將以兩種方法來實(shí)現(xiàn),即非遞歸和遞歸
非遞歸的實(shí)現(xiàn):
#define right 1 #define down 2 #define left 3 #define up 4 #define n 5 //控制矩形的大小 #include<stdio.h> int main()//設(shè)計(jì)一個蛇形矩形圖案 順時針 { int m = 1; int x = 1; int y = 1; int direct; int i = 0; int j = 0; int arr[n + 1][n + 1]; for (x = 1; x < (n + 1); x++) { for (y = 1; y < (n + 1); y++) { arr[x][y] = 100;//隨機(jī)但是不能定為零 } } x = 1; y = 1; direct = right; while (m <= n * n) { arr[x][y] = m++; switch (direct) { case right: if (arr[x][y + 1] == 100) { y++; } else { direct = down; x++; }break; case down: if (arr[x + 1][y] == 100) { x++; } else { direct = left; y--; }break; case left: if (arr[x][y - 1] == 100) { y--; } else { direct = up; x--; }break; case up: if (arr[x - 1][y] == 100) { x--; } else { direct = right; y++; }break; } } //顯示矩形 for (x = 1; x <= n; x++) { for (y = 1; y <= n; y++) { printf("%2d ", arr[x][y]); } printf("\n"); } return 0; }
非遞歸的解題思想:定義一個數(shù)組,這個數(shù)組的大小是(N+1)*(N+1),目的是形成一個邊框,便于調(diào)整方向,其次就是當(dāng)x與y跑到邊框的位置就實(shí)現(xiàn)拐彎。
拐彎的思想就是上到下,下到左,左到上,上再到右,實(shí)現(xiàn)從外圍向內(nèi)包圍,直至m <= n * n。
遞歸的實(shí)現(xiàn):
#define right 1 #define down 2 #define left 3 #define up 4 #define n 7 //控制大小 int arr[n][n]; #include<stdio.h> void snake(int x, int y, int m, int direct) { arr[x][y]=m; if (m == n * n) return; switch (direct) { case right: if ((y+1) == (n+1) || arr[x][y+1] > 0) {//到達(dá)右邊邊界或者右邊有數(shù)字,不能再往右 direct = down; //改變方向,向下 x++; //向下移動一格 } else //可以向右填寫 y++; //向右移動一格 break; case down: if ((x + 1) == (n+1) || arr[x + 1][y] > 0) { direct = left; y--; } else x++; break; case left: if ((y + 1) == (n+1) || arr[x][y - 1] > 0) { direct = up; x--; } else y--; break; case up: if ((y + 1) == (n+1) || arr[x-1][y] > 0) { direct = right; y++; } else x--; break; } snake(x, y, ++m, direct); //填寫下一個數(shù) } int main()//用遞歸填寫這個矩形蛇形圖案 { int x = 1; int y = 1; int m = 1; snake(x, y, m, right); //顯示矩形 for (x = 1; x <= n; x++) { for (y = 1; y <= n; y++) { printf("%2d ", arr[x][y]);//這里有個二,別忘了 } printf("\n"); } return 0; }
遞歸的實(shí)現(xiàn)大體思路跟非遞歸的實(shí)現(xiàn)類似,從外面到內(nèi)部
但遞歸的每一個元素是單獨(dú)在一個函數(shù)里來定義的,直至最后的m==n*n,然后再main函數(shù)里實(shí)現(xiàn)最終的模型。
其中的n是來控制大小,例如當(dāng)n為5和9的結(jié)果如下:
原文鏈接:https://z-ming.blog.csdn.net/article/details/122353383
相關(guān)推薦
- 2024-02-01 QueryWrapper、LambdaQueryWrapper、QueryChainWrapper、
- 2022-07-06 windows清理系統(tǒng)垃圾bat腳本及使用步驟_DOS/BAT
- 2022-04-25 ASP.NET?Core中Cookie驗(yàn)證身份用法詳解_實(shí)用技巧
- 2022-06-08 基于Springboot實(shí)現(xiàn)專業(yè)認(rèn)證材料管理系統(tǒng)
- 2023-02-15 Go語言中節(jié)省內(nèi)存技巧方法示例_Golang
- 2022-09-17 C++中String類型的逆序方式_C 語言
- 2022-07-07 python中的format是什么意思,format怎么用_python
- 2022-07-25 Python?APScheduler?定時任務(wù)詳解_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支