Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
For example,
Given n = 3,
[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ]
We can use the solution to Spiral Matrix to help solve this problem by creating the matrix first and fill each spot while spiral traversal
C++ Code:
/*
* func: spiral_matrix_helper
* goal: recursive helper function to generate spiral matrix
* @param matrix: matrix to be filled
* @param start_row: current row
* @param start_column: current column
* @param row_bound: row bound
* @param column_bound: column bound
* @param value: start value for this round
* reutrn:
*/
void spiral_matrix_helper(vector<vector<int>> &matrix, int start_row, int start_column, int row_bound, int column_bound, int &value){
//All elements are already added
if(start_row >= row_bound || start_column >= column_bound){
return;
}
//There is only one row
if(start_row + 1 == row_bound){
for(int i = start_column; i < column_bound; ++i){
matrix[start_row][i] = value++;
}
return;
}
//There is only one column
if(start_column + 1 == column_bound){
for(int i = start_row; i < row_bound; ++i){
matrix[i][start_column] = value++;
}
return;
}
//Add upside row
for(int i = start_column; i < column_bound; ++i){
matrix[start_row][i] = value++;
}
//Add rightside column
for(int i = start_row+1; i < row_bound; ++i){
matrix[i][column_bound-1] = value++;
}
//Add bottom row
for(int i = column_bound-2; i >= start_column; --i){
matrix[row_bound-1][i] = value++;
}
//Add leftside column
for(int i = row_bound-2; i > start_row; --i){
matrix[i][start_column] = value++;
}
//Continue for the inner matrix
spiral_matrix_helper(matrix, start_row+1, start_column+1, row_bound-1, column_bound-1, value);
}
/*
* func: generate_spiral_matrix
* goal: generate a spiral matrix contaning n*n numbers
* @param n: the total elements number
* return: spiral matrix
*/
vector<vector<int> > generate_spiral_matrix(int n){
if(n <= 0){
vector<vector<int> > result;
return result;
}else{
vector<vector<int> > result(n, vector<int>(n, 0));
int start_value = 1;
spiral_matrix_helper(result, 0, 0, n, n, start_value);
return result;
}
}
Python Code:
# func: Create a square matrix with elements from 1 to n*n in spiral order
# @param n: input size n
# @return: a list of lists of integers
def generate_matrix(n):
if n <= 0:
return []
else:
result = [[0] * n for _ in xrange(n)]
value = 1
row = 0
column = 0
row_limit = n-1
column_limit = n-1
while True:
#Upside from left to right
i = column
while i <= column_limit:
result[row][i] = value
value += 1
i += 1
row += 1
if row > row_limit:
break
#Rightside from top to bottom
i = row
while i <= row_limit:
result[i][column_limit] = value
value += 1
i += 1
column_limit -= 1
if column > column_limit:
break
#Bottomside from right to left
i = column_limit
while i >= column:
result[row_limit][i] = value
value += 1
i -= 1
row_limit -= 1
if row > row_limit:
break
#Leftside from bottom to top
i = row_limit
while i >= row:
result[i][column] = value
value += 1
i -= 1
column += 1
if column > column_limit:
break
return result
No comments:
Post a Comment