算法基础系列之三:螺旋形矩阵
如何打印出如下這樣的螺旋形的矩陣:
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
總結
以上是生活随笔為你收集整理的算法基础系列之三:螺旋形矩阵的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到父亲去世是什么征兆
- 下一篇: 梦到打死三条蛇预示着什么意思