Zuo Shen Classroom --- Zigzag Printing Matrix --- Macro Scheduling Thought

Question Description: Given a matrix, it is required to print out the matrix in a zigzag pattern.


Then the printing order is: 1 2 5 9 6 3 4 7 10 11 8 12

Basic idea: define two points a, b. At the beginning, point a and point b are both at (0,0) position, then point a goes to the right, point b goes down, when point a touches the far right, point a goes down, and when point b touches At the bottom, go right at point b. Every time you go, print the numbers on the diagonal of point a and point b. Then define a variable of type bool to determine whether to print from the upper right to the lower left or from the lower left to the upper right.

code show as below:

#include<bits/stdc++.h> //Basic idea: use a point a and a point b, at the beginning point a, point b are at (0,0) position, then point a goes right, and point b goes down
using namespace std; //When point a touches the far right, a goes down. When b touches the bottom, b goes to the right. Each time a, b go, print the number between a and b
int n;
int m;
int a[10000][10000];
bool p; // Boolean type variable, used to determine whether to print from upper right to lower left or from Print
class heat from bottom left to top right
{
    //private:
    public:
        void shuru() //input value
        {
            cin>>n>>m;
            p=true;
            for(int i=0;i<n;i++)
                for(int j=0;j<m;j++)
                 cin>>a[i][j];
        }
    public:
        void dayin(int ha,int sa,int hb,int sb,bool p) //print between diagonal lines number
        {
           if(p) //If p is true, then print from top right to bottom left
           {
               while(ha!=hb+1&&sa!=sb-1)
               {
                   cout<<a[ha][sa]<<" ";
                   ha+ =1;
                   sa-=1;
               }
           }
           else //Print from bottom left to top right
           {
               while(hb!=ha-1&&sb!=sa+1)
               {
                   cout<<a[hb][sb]<<" ";
                   hb -=1;
                   sb+=1;
               }
           }
        }
    public:
       void hexin()
       {
          int ha=0,sa=0,hb=0,sb=0;//ha, sa represent the abscissa and ordinate of point a respectively. hb and sb represent the abscissa and ordinate of point b, respectively.
          int endm=m-1,endn=n-1;
          cout<<a[0][0]<<" ";
          while (ha!=endn) //When the abscissa of point a reaches the last line, hide There is a message that sa has reached the rightmost point, which means that point a and point b have reached the bottom rightmost point
          {
             //Note: the order of judgment cannot be changed. For the judgment of the horizontal and vertical coordinates of point a, it must be First judge the abscissa and then judge the ordinate, because the judgment of the abscissa requires the original ordinate of a
              //If the ordinate of a is judged first, then the original ordinate of a may change, then, a will go next The abscissa may be wrong
              //point b is the same as

              ha=sa==endm?ha+1:ha; //Determine the abscissa that point a should go next, if sa reaches the last column, then ha should go down , otherwise, unchanged
              sa=sa==endm?sa:sa+1; //Judging the ordinate of point a that should go next, if sa does not reach the last line, sa continues to go to the right, otherwise, sa remains unchanged
              sb =hb==endn?sb+1:sb; //Same
as above               hb=hb==endn?hb:hb+1;
              dayin(ha,sa,hb,sb,p);
              p=!p;//If this time it is printed from the upper right to the lower left, then the next time it must be printed from the lower left to the upper right.
          }
       }
};
int main()
{
    heat nul;
    nul.shuru();
    nul.hexin();
}






Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325767663&siteId=291194637