< serpentine fill >--algorithm Race Primer Classic (2nd edition)-3.1 array program 3-3 serpentine fill

Source: Internet
Author: User

Snake filling : In the nxn phalanx filled with 1,2,....,nxn, asked to fill the snake. For example, n = 4 o'clock Square is:

10 11 12 1

9 16 13 2

8 15 14 3

7 6 5 4

Above the square, the extra space is only to facilitate the observation of the law, do not have to strictly output. N <= 8 .

Analogy to the above matrix in mathematics, we can consider using a two-dimensional array to store.

   problem-Solving ideas: starting from the top right corner to fill the number, when the end of the turn to the left to fill the value, when the left side of the time to turn on the number, when the top of the time to the right to fill the value ... In turn. The first circle of the judging condition is very simple, that is, when the horizontal axis is less than the number of rows, ordinate less than the number of columns. But this cycle goes down when the first round of values is covered by the second cycle, that is, all the way down the outermost lap (when the values are filled and then stopped). we can solve this problem by starting to set the array to 0and then adding the value of && not the next position when judging the turn.

Here is the code for the specific problem solving:

1#include <iostream>2#include <cstdio>3#include <cstring>4 #defineMAXN 45 using namespacestd;6 intA[MAXN][MAXN];7 8 intMain () {9     intI, J;Ten     /* One For (i = 0; i < MAXN; i++) { A For (j = 0; J < Maxn; J + +) - A[i][j] = 0; -     }*/ theMemset (A,0,sizeof(a));//this is the function in CString, of course, you can also use the method above to assign values -a[0][maxn-1] =1; -j = maxn-1; -i =0; +     intCount =1;///starting from 1, fill in the number -      +     /*---------Think about why the following is wrong??? A While (Count < MAXN*MAXN) at     { - while (I+1 < MAXN &&!a[++i][j]) a[i][j] = ++count; - while (j-1 >= 0 &&!a[i][--j]) a[i][j] = ++count; - while (i-1 >= 0 &&!a[--i][j]) a[i][j] = ++count; - while (J+1 < MAXN &&!a[i][++j]) a[i][j] = ++count; -     }*/ in  -      while(Count < MAXN*MAXN)//① to     { +          while(i+1< MAXN &&!a[i+1][J]) a[++i][j] = ++count;//② -          while(J-1>=0&&!a[i][j-1]) A[i][--j] = + +count; the          while(I-1>=0&&!a[i-1][J]) A[--i][j] = + +count; *          while(j+1< MAXN &&!a[i][j+1]) A[i][++j] = + +count; $     }Panax Notoginseng      for(i =0; i < MAXN; i++)//Output -     { the          for(j =0; J < Maxn; J + +) +printf"%3d", A[i][j]); Acout <<Endl; the     } +     return 0; -}
View Code

    interested in code friends can consider :

1.      why not the ① place ? count <= maxn*maxn ;

2.      why not the ② place ? count++ ;

3.      ② can be changed into while (i+1 < MAXN &&!a[++i][j]) a[i+1][j] = ++count ;

  Welcome code Friends Comments, thank you for your support.

< serpentine fill >--algorithm Race Primer Classic (2nd edition)-3.1 array program 3-3 serpentine fill

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.