日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

算法基础系列之三:螺旋形矩阵

發布時間:2023/11/29 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 算法基础系列之三:螺旋形矩阵 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

如何打印出如下這樣的螺旋形的矩陣:

1???????? 2? 3

8?? 9? 4

7?? 6? 5

?

方法一:

static void SpiralMatrix(int count)

{

??? int[,] iarray = new int[count, count];

??? for (int i = 0; i < count; i++)

??? {

??????? for (int j = 0; j < count; j++)

??????? {

??????????? iarray[i, j] = 0;

??????? }

??? }

?

??? iarray[0, 0] = 1;

??? int row = 0;

??? int col = 0;

??? int temprowsub = 0;

??? int tempcolsub = 1;

?

??? for (int i = 0; i < count * count; i++)

??? {

??????? if (tempcolsub == 1)//right

??????? {

??????? ????if (col + 1 <= count - 1 && iarray[row, col + 1]==0)//right

??????????? {

??????????????? iarray[row, col + 1] = iarray[row, col] + 1;

??????????????? col++;

??????????? }

??????????? else if (row + 1 <= count - 1 && iarray[row + 1, col] == 0)//down

? ??????????{

??????????????? iarray[row + 1, col] = iarray[row, col] + 1;

??????????????? temprowsub = 1;

??????????????? tempcolsub = 0;

??????????????? row++;

??????????? }

??????????? else

??????????? {

??????????????? break;

??????????? }

??????? }

??? ????else if (tempcolsub == -1)//left

??????? {

??????????? if (col - 1 >= 0 && iarray[row, col - 1] == 0)//left

??????????? {

??????????????? iarray[row, col - 1] = iarray[row, col] + 1;

??????????????? col--;

??????????? }

??????????? else if (row - 1 >= 0 && iarray[row - 1, col] == 0)//up

??????????? {

??????????????? iarray[row - 1, col] = iarray[row, col] + 1;

??????????????? temprowsub = -1;

??????????????? tempcolsub = 0;

??????????????? row--;

??????????? }

??????????? else

??????????? {

??????????? ????break;

??????????? }

??????? }

??????? if (temprowsub == -1)//up

??????? {

??????????? if (row-1 >=0 && iarray[row-1, col] == 0)//up

??????????? {

??????????????? iarray[row-1, col] = iarray[row, col] + 1;

??????????????? row--;

??????????? }

???????? ???else if (col + 1 <= count - 1 && iarray[row, col + 1] == 0)//right

??????????? {

??????????????? iarray[row, col + 1] = iarray[row, col] + 1;

??????????????? temprowsub = 0;

??????????????? tempcolsub = 1;

??????????????? col++;

??????????? }

????????? ??else

??????????? {

??????????????? break;

??????????? }

??????? }

??????? if (temprowsub == 1)//down

??????? {

??????????? if (row + 1 <= count - 1 && iarray[row+1, col] == 0)//down

??????????? {

??????????????? iarray[row+1, col] = iarray[row, col] + 1;

??????????????? row++;

??????????? }

??????????? else if (col - 1 >= 0 && iarray[row, col - 1] == 0)//left

??????????? {

??????????????? iarray[row, col - 1] = iarray[row, col] + 1;

??????????????? temprowsub = 0;

??????????????? tempcolsub = -1;

??????? ????????col--;

??????????? }

??????????? else

??????????? {

??????????????? break;

??????????? }

??????? }

??? }

?

//TO DO:OUTPUT

……

}

?

方法二:

static void SpiralMatrix(int count)

{

??? int round = count - 1;

??? int[,] matrix = new int[count, count];

??? int num = 1;

?

??? for (int r = 0; r < round && num <= count * count; r++)

??? {

??????? for (int tj = r; tj <= round - r; tj++)//top

??????????? matrix[r, tj] = num++;

??????? for (int ri = r + 1; ri <= round - r; ri++) //right

??????????? matrix[ri, round - r] = num++;

??????? for (int bj = round - r - 1; bj >= r; bj--)//bottom

??????????? matrix[round - r, bj] = num++;

??????? for (int li = round - r - 1; li > r; li--)//left

??????????? matrix[li, r] = num++;

??? }

//TO DO:OUTPUT

……

}

?

方法一是菜鳥我寫的,有點類似爬迷宮,代碼有點煩。后來在CSDN看到一個比較簡潔的(方法二),也抄上來。

?

?

轉載于:https://www.cnblogs.com/morvenhuang/archive/2006/09/16/506057.html

總結

以上是生活随笔為你收集整理的算法基础系列之三:螺旋形矩阵的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。