3 回答

TA貢獻(xiàn)1966條經(jīng)驗(yàn) 獲得超4個(gè)贊
根據(jù)您在我的第一個(gè)答案下的評論:
我也在考慮另一種解決方案......使用 BFS。對于起始點(diǎn),假設(shè)為 0,0,每次迭代總是深度為 1,因此如果從 0,0 開始,則執(zhí)行 1,0 1,1 0,1,當(dāng)?shù)竭_(dá)行或列邊界時(shí),您會(huì)增加深度意味著在下一次迭代中您將使用 0,2 1,2 2,2 2,1 2,0 并且如果矩陣是 nxm 且 n!=m 您只需根據(jù)您所在的位置跳過列或行(檢查矩陣總是綁定 m 和 n)。
忘了說,這樣它也適用于字母。
我決定添加一個(gè)新答案而不是編輯第一個(gè)答案。
由于我不太熟悉圖和廣度優(yōu)先搜索,我無法為基于 BSF 的算法提供任何建議。
但我想我理解你想要如何進(jìn)行的想法,這是一個(gè)簡單的實(shí)現(xiàn)方法。您希望在每次迭代中采取的步驟
0,0
-----
0,1
1,1
1,0
-----
0,2
1,2
2,2
2,1
2,0
-----
0,3
1,3
2,3
3,3
3,2
3,1
3,0
...
無論矩陣是否是正方形,我最初都會(huì)關(guān)注正方形區(qū)域。例如,如果是一個(gè)4x6矩陣,我只看部分4x4,忽略最后兩列。同樣,如果行大于列。對于a,7x5我將查看5x5并忽略最后兩行。在每次迭代中,我將使用兩個(gè)內(nèi)部循環(huán),第一個(gè)循環(huán)增加行,第二個(gè)循環(huán)減少列。
與之前的答案相反,這次我將使用字符串?dāng)?shù)組來完成您的評論:
忘了說,這樣它也適用于字母。
static String[][] fillArray(int rows, int columns){
char ch = 'A';
String[][] matrix = new String[rows][columns];
int square = Math.min(rows, columns);
for(int i = 0; i < square; i++){
String curr = String.valueOf(ch);
for(int r = 0; r <= i ; r++){
matrix[r][i] = curr;
}
for(int c = i-1; c >= 0; c--){
matrix[i][c] = curr;
}
ch++;
}
return matrix;
}
例如使用 args 調(diào)用上述方法4x5
public static void main(String[] args) {
String[][] filled = fillArray(4,5);
for(String[] row: filled){
System.out.println(Arrays.toString(row));
}
}
將導(dǎo)致
[A, B, C, D, null]
[B, B, C, D, null]
[C, C, C, D, null]
[D, D, D, D, null]
現(xiàn)在讓我們看看仍然充滿 的被忽略的區(qū)域null,即 的情況rows > columns or rows < columns。為了簡單起見,我將用 填寫此區(qū)域*。
static String[][] fillArray(int rows, int columns){
char ch = 'A';
String[][] matrix = new String[rows][columns];
int square = Math.min(rows, columns);
for(int i = 0; i < square; i++){
String curr = String.valueOf(ch);
for(int r = 0; r <= i ; r++){
matrix[r][i] = curr;
}
for(int c = i-1; c >= 0; c--){
matrix[i][c] = curr;
}
ch++;
}
ch = '*';
if (rows > columns) {
for (int i = square; i < rows; i++) {
for (int j = 0; j < columns; j++) {
matrix[i][j] = String.valueOf(ch);
}
}
}
if (rows < columns) {
for (int i = square; i < columns; i++) {
for (int j = 0; j < rows; j++) {
matrix[j][i] = String.valueOf(ch);
}
}
}
return matrix;
}
調(diào)用 fillArray(4,6)
public static void main(String[] args) {
String[][] filled = fillArray(4,6);
for(String[] row: filled){
System.out.println(Arrays.toString(row));
}
}
現(xiàn)在應(yīng)該導(dǎo)致:
[A, B, C, D, *, *]
[B, B, C, D, *, *]
[C, C, C, D, *, *]
[D, D, D, D, *, *]
這篇文章比我實(shí)際想象的要長。但我希望它很容易理解。

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超13個(gè)贊
如果您查看 nxn 矩陣,就會(huì)發(fā)現(xiàn)某種模式需要識(shí)別。
1 2 5 10 17
4 3 6 11 18
9 8 7 12 19
16 15 14 13 20
25 24 23 22 21
第一列中總是有平方數(shù)
在第一行中,從最小的平方數(shù) 1 開始,添加奇數(shù)序列,即1,3,5,7,9,11 ....
每行的模式
[1] +1 +3 +5 +7 ....
[4] -1 +3 +5 +7 ....
[9] -1 -1 +5 +7 ....
[16] -1 -1 -1 +7 ....
[25] -1 -1 -1 -1 ....
有了這些知識(shí),現(xiàn)在就可以輕松填充n x n矩陣了。1創(chuàng)建一個(gè)包含從到 的奇數(shù)的列表columns-1,通過逐個(gè)添加列表的元素來填充第一行,每次迭代后將列表中的第 i 個(gè)數(shù)字替換為-1能夠使用該行的列表i+1th。
現(xiàn)在對于非方矩陣,可以使用上述方法填充矩陣的方區(qū)域,并分別處理剩余的行或列。找到正方形區(qū)域的大小以定義其余行/列的計(jì)數(shù)器相對簡單。
只要計(jì)算一下Math.pow(Math.min(rows, columns), 2);
我采用了您的方法名稱并稍微更改了您的打印方法。我沒有評論代碼,但希望清楚正在做什么。如果有任何不清楚的地方,請隨時(shí)詢問。
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Test{
public static int matrix[][];
public static void main(String[] args) {
borderLayout(5, 5);
print();
}
public static void borderLayout(int rows, int columns) {
//create a list (1,3,5, ...) with size = columns - 1
List<Integer> list = IntStream.iterate(1, i -> i + 2)
.limit(columns - 1).boxed()
.collect(Collectors.toList());
matrix = new int[rows][columns];
int square = Math.min(rows, columns);
for (int i = 0; i < square; i++) {
int temp = (i + 1) * (i + 1);
for (int j = 0; j < square; j++) {
if (j == 0) {
matrix[i][j] = temp;
} else {
matrix[i][j] = matrix[i][j - 1] + list.get(j - 1);
}
}
if (i < columns - 1) {
list.set(i, -1);
}
}
if (rows > columns) {
int counter = (int) Math.pow(Math.min(rows, columns), 2);
int sqrt = (int) Math.sqrt(counter);
for (int i = sqrt; i < rows; i++) {
for (int j = 0; j < columns; j++) {
matrix[i][j] = ++counter;
}
}
} else if (rows < columns) {
int counter = (int) Math.pow(Math.min(rows, columns), 2);
int sqrt = (int) Math.sqrt(counter);
for (int i = sqrt; i < columns; i++) {
for (int j = 0; j < rows; j++) {
matrix[j][i] = ++counter;
}
}
}
}
private static void print() {
for (int[] row : matrix) {
for (int i : row) {
System.out.printf("%4d ", i);
}
System.out.println();
}
}
}

TA貢獻(xiàn)1862條經(jīng)驗(yàn) 獲得超7個(gè)贊
如果你沿著左欄看,就會(huì)發(fā)現(xiàn)一個(gè)模式:
1
4
9
16
其中 是(r + 1)^2,其中r是行號(hào)。
沿行,值遞減 1,直到對角線,其中r == c:
1
4 3
9 8 7
16 15 14 13
這是(r + 1)^2 - c.
如果你沿著頂行看,就會(huì)發(fā)現(xiàn)一個(gè)模式:
1 2 5 10
這是c^2 + 1.
沿著列行向下,值增加 1,直到對角線,其中r == c:
1 2 5 10
3 6 11
7 12
13
這是c^2 + 1 + r
因此,您可以使用嵌套循環(huán)計(jì)算數(shù)組元素:
for (int r = 0; r < N; ++r) {
for (int c = 0; c < M; ++c) {
matrix[r][c] = (r >= c) ? ((r + 1)*(r + 1) - c) : (c * c + 1 + r);
}
}
添加回答
舉報(bào)