[Matrix] spiral Matrix problem (top)

Source: Internet
Author: User
Do a leetcode on the topic of the spiral matrix: Spiral Matrix II looked at the problem of the spiral matrix: First Look at the Leetcode topic:

Given an integer n, generate a square matrix filled with elements from 1 to N2 in Spiral order.

For example,
Given n = 3, you should return the following matrix:

[
 [1, 2, 3],
 [8, 9, 4],
 [7, 6, 5]
]

The main idea is to n*n an integer, put them into a n*n matrix, as shown below, forming a clockwise spiral from the outside to the inside:

It is easy to think of a way of thinking is in accordance with the spiral direction of the ranks of the row alternately walk, until you go to the end of the line, wrap the line at the end of the column, and end up with each position filled in, for example, the first line, add 1, 2, 3, and then add 4, 5, and then three rows of 6,7, a column of 8, two lines 9, end. >>> >>> >>> >>> Follow this logic to write code, one to loop responsible for the entire matrix, 4 small loops responsible for each direction, the key place please refer to the code comment.

vector<vector<int>>  Generatespiralmatrix (int n) { vector<vector<int>> Retvec (n, Vector<int> (n, 1));//Data Matrix  vector<vector<int>> Retvecflag (n, vector<int> (n, 0));//Auxiliary matrix that identifies whether the location has been processed by 0: unhandled, 1: Processed  if ((1 = = N) | | (0 = = N))
Boundary judgment  {  return Retvec;  }  size_t count = 0;
 int i = 0;

 int j = 0;  while (Count < n * n)//outer matrix Loop  {  while ((J < N) && (0 = = Retvecflag[i][j])//column increases direction loop, Corresponding to the above steps 1 and 5   {   retvec[i][j] = ++count;//corresponding position fill number    retvecflag[i][j] = 1;// Place the processed identity    ++j;//continue to populate the next position of the row   }   --j;//loop exits, j==n, you need to fallback a location   ++i;// The row has been processed and continues on the next line   while ((i < n) && (0 = = Retvecflag[i][j]))//line to increase the direction of the loop, corresponding to the above step 2   {&NBSP;&N
BSP;&NBSP;RETVEC[I][J] = ++count;
&NBSP;&NBSP;&NBSP;RETVECFLAG[I][J] = 1;
   ++i;
  }   --i;   --j;   while ((J >=0) && (0 = = Retvecflag[i][j]))//column reduction direction loop, corresponding to the above step 3   {   
RETVEC[I][J] = ++count;
&NBSP;&NBSP;&NBSP;RETVECFLAG[I][J] = 1;
   --j;
  }   ++j;

  --i;   while ((i >= 0) && (0 = = Retvecflag[i][j])//Line reduction direction cycle, corresponding to the above step 4   {   
RETVEC[I][J] = ++count;
&NBSP;&NBSP;&NBSP;RETVECFLAG[I][J] = 1;
   --i;
  }   ++i;
  ++j;
 }  return Retvec; }

This is more easily thought of a way of thinking, and later on the online search, found a more general understanding of the method, tomorrow to write a detailed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.