hrbust 1564 螺旋矩陣 dfs過

2021-07-10 06:23:01 字數 1527 閱讀 8281

螺旋矩陣

time limit: 1000 ms

memory limit: 10240 k

total submit: 276(77 users)

total accepted: 79(71 users)

rating:

special judge: no

description

對於給定的乙個數n,要你列印n*n的螺旋矩陣。

比如n=3時,輸出:

1 2 3

8 9 4

7 6 5

input

多組測試資料,每個測試資料報含乙個整數n(1<=n<=32)

output

對於每組測試資料,輸出乙個n*n的螺旋矩陣,定義在題目描述裡。

在一組測試資料中,每個數占的字元寬度是該組資料中最大的數字數加1,比如3*3的螺旋矩陣,最大值是9,那麼每個數就要佔2個字元寬度。

兩組測試資料之間用乙個空行隔開。

sample input

1

23

sample output

1

1 24 3

1 2 3

8 9 4

7 6 5

好**題解報告:

水題一發,輸出部分學到乙個新知識,寬度控制可以這樣來控制:

printf("%*d",len+1,ans[i][j]);
然後剩下的內容就比較簡單了,我從座標(1,1)出發 ,然後用1、2、3、4分別標記四個方向,當前方向一直走下去,如果碰到了邊界或者是走過的地方,右轉即可、

ac**:

#include#include#include#include#includeusing namespace std;

int ans[50][50];

int vis[50][50];

//1right,2down,3left,4up

int n;

int check(int x,int y)

void dfs(int x,int y,int num,int cur)

else

}if(cur==2)//1right,2down,3left,4up

else

}if(cur==3)//1right,2down,3left,4up

else

}if(cur==4)

else

}return ;

}int main()

f++;

int date=n*n;

int len=0;

while(date)

//printf("%d\n",len);

memset(vis,0,sizeof(vis));

memset(ans,0,sizeof(ans));

dfs(1,1,1,1);

for(int i=1;i<=n;i++)

printf("\n");}}

}

HRBUST 1564 螺旋矩陣 DFS

螺旋矩陣 time limit 1000 ms memory limit 10240 k total submit 282 78 users total accepted 81 72 users rating special judge no description 對於給定的乙個數n,要你列印n ...

hrbust 2192 螺旋的矩陣

螺旋的矩陣 time limit 1000 ms memory limit 32768 k total submit 30 19 users total accepted 16 16 users rating special judge no description 給出乙個奇數 n,我們可以把數字...

leetcode刷題python之螺旋矩陣II

思路 按照題目要求依次迴圈遍歷,需要安排好順序,設定好四個座標參考,left,right,top,bottom,用於移動的參考,每次移動後進行相應的更新,在乙個while下分別放著4個for迴圈,分別表示在四個方向上的移動 class solution def generatematrix self...