HRBUST 1564 螺旋矩陣 DFS

2021-07-10 06:23:01 字數 1780 閱讀 3241

螺旋矩陣

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*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 2

3 sample output

11 2

4 31 2 3

8 9 4

7 6 5

本題我用的是搜尋

關鍵點在於搜尋的方向控制

方向控制很簡單 就是用fx[4]與fy[4]控制方向

「→」「↓」「←」「↑」這四個方向的迴圈

如果上一次搜尋是那個搜尋那個方向的 如果搜尋的圖已經到頭了

就從下乙個方向開始搜尋

如下就是搜尋的**

void dfs(int x,int y,int ii)

}}

本題非常容易pe 主要由以下兩點

1.兩組資料中間的空格。(這個很容易 但是要讀題啊 博主就因為沒看到而pe多次)

2.圖內最大的數有幾位 圖內的所有數都佔幾+1位(這樣想就沒有空格的干擾了)

關於控制資料佔位的個數可以這樣表示

printf("%*d",length+1,a[i][j]); //其中*號是位數 對應length是對應位數的資料 +1是數值之前的空格佔一位
#include 

#include

#include

#include

#include

#include

#include

using namespace std;

int n,num;

int a[34][34];

int fx[4]=;

int fy[4]=;

void dfs(int x,int y,int ii)

}}int main()

memset(a,0,sizeof(a));

num=2;

int length=1,m=n*n;

while(m>=10)

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

a[1][1]=1;

dfs(1,1,0);

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

printf("\n");

}flag=1;

}}

hrbust 1564 螺旋矩陣 dfs過

螺旋矩陣 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 ...

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,我們可以把數字...

螺旋方針(螺旋矩陣)

螺旋方陣 time limit 1000ms memory limit 65536kb problem description 的螺旋方陣當n 5和n 3時分別是如下的形式 請給出乙個程式,對於任意的輸入 0 11 輸出按照上面規律所獲得的 的螺旋方陣。input 輸入第一行為整數 0 10 代表有...