我正在编写一个代码来对2D矩阵执行递归螺旋扫描。当行数和列数相同时,它工作正常,但不是这样(当num rows> num列,反之亦然)。螺旋扫描以螺旋顺序打印矩阵。例如:
输入矩阵:
1 2 3
4 5 6
7 8 9
输出:
1 2 3 6 9 8 7 4 5
请帮助查找错误。这是我的代码:
#include <stdio.h>
#include <stdlib.h>
int a[10][10];
void spiral_scan(int a[][10],int sr,int sc,int er,int ec)
{
if((ec<1)||(er<1))
return ;
int i;
for(i=sr;i<=ec;i++)
printf("%d ",a[sr][i]);
//printf("\n");
sr++;
for(i=sr;i<=er;i++)
printf("%d ",a[i][ec]);
ec--;
for(i=ec;i>=sc;i--)
printf("%d ",a[er][i]);
er--;
for(i=er;i>=sr;i--)
printf("%d ",a[i][sc]);
sc++;
spiral_scan(a,sr,sc,er,ec);
}
int main()
{
printf("\t\tSPIRAL SCAN OF MATRIX RECURSUVELY\n");
scanf("Enter the number of rows and coloumns:\n");
int m1,n1,i,j;
scanf("%d%d",&m1,&n1);
printf("Enter the elements in the matrix:\n");
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
scanf("%d",&a[i][j]);
}
printf("The entered matrix is:\n");
for(i=0;i<m1;i++)
{
printf("\n");
for(j=0;j<n1;j++)
printf("%d ",a[i][j]);
}
printf("\n");
spiral_scan(a,0,0,m1-1,n1-1);
return 0;
}
答案 0 :(得分:0)
这是我在C ++中的代码。
//to check for boundary conditions
bool check(int i,int j,int rows,int cols)
{
if(i>=0 && j>=0 && i<rows && j<cols)
return 1;
return 0;
}
//to check if all elements have been traversed
bool brk(int rows,int cols,vector<vector<int>> mark)
{
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
if(mark[i][j]==0)
return 0;
}
}
return 1;
}
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector < int > V;
if(matrix.size()==0)
return V;
int rows = matrix.size();
int cols = matrix[0].size();
vector < vector<int> > mark(rows , vector<int>(cols,0) );
int i=0,j=0;
while(1)
{
//when going right
while(check(i,j,rows,cols) && mark[i][j]==0)
{
mark[i][j]=1;
V.push_back(matrix[i][j]);
j++;
}
j--; i++;
if(brk(rows,cols,mark)==1)
break;
//when going down
while(check(i,j,rows,cols) && mark[i][j]==0)
{
mark[i][j]=1;
V.push_back(matrix[i][j]);
i++;
}
i--; j--;
if(brk(rows,cols,mark)==1)
break;
//when going left
while(check(i,j,rows,cols) && mark[i][j]==0)
{
mark[i][j]=1;
V.push_back(matrix[i][j]);
j--;
}
j++; i--;
if(brk(rows,cols,mark)==1)
break;
//when going up
while(check(i,j,rows,cols) && mark[i][j]==0)
{
mark[i][j]=1;
V.push_back(matrix[i][j]);
i--;
}
i++; j++;
if(brk(rows,cols,mark)==1)
break;
}
return V;
}
这是我在Leetcode中接受的代码。 问题链接 - https://leetcode.com/problems/spiral-matrix/
递归解决方案。
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix)
{
vector<int> ret;
if (!matrix.size())return ret;
int direc = 1;
set<pair<int, int>> exist;
pair<int, int> start{ 0, 0 };
direction(matrix, direc, exist, start, ret);
return ret;
}
void direction(vector<vector<int>>& matrix, int &direc, set<pair<int, int>> &exist, pair<int, int> &start, vector<int>& ret)
{
if (start.first>int(matrix.size()) - 1 || start.second>int(matrix[0].size()) - 1 || exist.count(start))
return;
int j = 0;
switch (direc)
{
case 1: //east
for (j = start.second; j<matrix[0].size() && !exist.count(make_pair(start.first, j)); j++)
{
ret.push_back(matrix[start.first][j]);
exist.insert(make_pair(start.first, j));
}
direc = 2;
start = make_pair(start.first + 1, j - 1);
direction(matrix, direc, exist, start, ret);
break;
case 2://south
for (j = start.first; !exist.count(make_pair(j, start.second)) && j<matrix.size(); j++)
{
ret.push_back(matrix[j][start.second]);
exist.insert(make_pair(j, start.second));
}
direc = 3;
start = make_pair(j - 1, start.second - 1);
direction(matrix, direc, exist, start, ret);
break;
case 3://west
for (j = start.second; !exist.count(make_pair(start.first, j)) && j >= 0; j--)
{
ret.push_back(matrix[start.first][j]);
exist.insert(make_pair(start.first, j));
}
direc = 4;
start = make_pair( start.first - 1,j+1);
direction(matrix, direc, exist, start, ret);
break;
case 4://north
for (j = start.first; !exist.count(make_pair(j, start.second)) && j >= 0; j--)
{
ret.push_back(matrix[j][start.second]);
exist.insert(make_pair(j, start.second));
}
direc = 1;
start = make_pair(j + 1, start.second + 1);
direction(matrix, direc, exist, start, ret);
break;
}
}
};