ProDeveloperTutorial.com

教程和编程解决方案
菜单
  • Shell脚本
  • 系统设计
  • Linux系统编程
  • 4g LTE
  • 编码问题
  • C
  • C ++
  • DSA
  • GIT

给定一个m×n个元素的矩阵,请在CPP中以螺旋顺序打印矩阵的所有元素

前开发者教程 2018年8月21日
Example 1:
Input:
[
 [ 1, 2, 3 ],
 [ 4, 5, 6 ],
 [ 7, 8, 9 ]
]
Output: [1,2,3,6,9,8,7,4,5]

解决方案非常简单。

螺旋矩阵

如上图所示,

  1. 我们向右走
  2. 我们往下走
  3. 我们向左走
  4. 我们向上走

为此,我们采用4个变量:

rowStart = 0;
rowEnd = matrix.size()-1;

colStart = 0;
colEnd = matrix[0].size() - 1;

In our example:
rowStart = 0
rowEnd = 2

colStart = 0
colEnd = 2

To traverse right we use below code:
for (int i = colStart; i <= colEnd; i ++) 
	// print variable matrix[rowStart][i]
rowStart++;


To traverse down we use below code:
for (int i = rowStart; i <= rowEnd; i ++) 
	// print variable matrix[i][colEnd]
colEnd--;

To traverse left we use below code:
for (int i = colEnd; i >= colStart; i --) 
	// print variable matrix[rowEnd][i]
rowEnd--;


To traverse up we use below code:
for (int i = rowEnd; i >= rowStart; i --) 
	// print variable matrix[i][colStart]
colStart ++;

we run the loop untill "rowStart <= rowEnd && colStart <= colEnd"
At the end of pass 1 below elements will be entered into the result vector
1 2 3
At the end of pass 2 below elements will be entered into the result vector
1 2 3 6 9
At the end of pass 3 below elements will be entered into the result vector
1 2 3 6 9 8 7
At the end of pass 4 below elements will be entered into the result vector
1 2 3 6 9 8 7 4
At the end of pass 5 below elements will be entered into the result vector. Hence our required result.
1 2 3 6 9 8 7 4 5
#include<iostream>
#include<vector>

using namespace std;
vector<int> get_spiral(vector<vector<int> > &matrix) 
{

	vector<int> result;

	int rowStart = 0;
	int rowEnd = matrix.size()-1;

	int colStart = 0;
	int colEnd = matrix[0].size() - 1;

	int dir = 1;
        int j = 1;

    while (rowStart <= rowEnd && colStart <= colEnd) 
    {
        if (dir == 1) 
        {    // left to right
            for (int i = colStart; i <= colEnd; ++i) 
            {
                result.push_back(matrix[rowStart][i]);
            }
 
            ++rowStart;
            dir = 2;
        } 
        else if (dir == 2) 
        {     // top to bottom
            for (int i = rowStart; i <= rowEnd; ++i) 
            {
                result.push_back(matrix[i][colEnd]);
            }
 
            --colEnd;
            dir = 3;
        } 
        else if (dir == 3) 
        {     // right to left
            for (int i = colEnd; i >= colStart; --i) 
            {
                result.push_back(matrix[rowEnd][i]);
            }
 
            --rowEnd;
            dir = 4;
        } 
        else if (dir == 4) 
        {     // bottom to up
            for (int i = rowEnd; i >= rowStart; --i) 
            {
                result.push_back(matrix[i][colStart]);
            }
            ++colStart;
            dir = 1;
        }
       j++;
    }

	return result;
}


int main() 
{
	//Declare a 2d array
	vector<vector<int> > myMatrix;
	int row = 3;
	int column = 3;

	std::vector<int > result;


	int temp = 1; // variable to put the values in to vector

	//populate the array
	for(int i=0; i < row; i++)
	{
		//create a temp vector, it will act as a row
		vector<int>  temp_vector;

		for(int i=0; i<column; i++)
		{
			temp_vector.push_back(temp);
			temp++;
		}
		// push the content of temp vector to the main vector
		myMatrix.push_back(temp_vector); 
	}

	cout<< "Entered vector is : "<<endl;
	for (int i = 0; i < row; i++) 
	{
		for (int j = 0; j < column; j++)
			cout << myMatrix[i][j] << ' ';
		cout << endl;
	}

	result = get_spiral(myMatrix);

	cout<< "The matrix in spiral order is : "<<endl;
	for (int i = 0; i < result.size(); i++) 
	{
		cout << result[i] << ' ';
	}
	cout<<endl;
	return 0;
}

输出:

Entered vector is :
1 2 3
4 5 6
7 8 9
The matrix in spiral order is :
1 2 3 6 9 8 7 4 5

该网站上可用的教程列表:

C编程20+章C ++编程80+章
100多个编码问题数据结构和算法85+章
系统设计20+章Shell脚本编写12章
4g LTE 60+章节最常见的编码问题
5G NR 50+章Linux系统编程20+章
分享
电子邮件
鸣叫
领英
Reddit
绊倒
Pinterest的
上一篇文章
下一篇

关于作者

前开发者教程

每天我们都会讨论竞争性编程问题,请加入我们的网站:   电报频道

ProDeveloperTutorial.com

教程和编程解决方案
版权© 2020 ProDeveloperTutorial.com
从以下课程获得热门课程: 教育性的


    • <q id="UdWhbhn" class="UuqVPsi"></q>



        1. <thead class="RhzIPaK"><video id="F76zJn9"></video></thead>
          <tbody id="wBA7kB2" class="wLbHml3"><figcaption id="FNLIOdV" class="FYwcbYt"><details id="hLZtPXk" class="h0e8PpU"></details></figcaption></tbody>


        2. <ins id="eT5RBr2" class="e0hEtJd"></ins>