Fortune left-law basis class3- title 8 of the zigzag print matrix c ++ realize

Fortune left-law basis class3- title 8 of the zigzag print matrix c ++ realize

1. Topic

Given a matrix matrix, this matrix print zigzag manner in accordance with "the", for example: 1,234,567,891,011,121,314 15 16 "The" zigzag printed result is: 1,259,634,710,131,411,812 15 16
[claim] extra space complexity is O (1).

Here Insert Picture Description

2. Analysis

The title of the output matrix shape, can be divided according to the direction of the lower left to the upper right to the lower left and upper right directions two outputs, the output may in fact be seen obliquely, the direction of change can be used as specific bool variable. Consider setting A, B as two auxiliary points, the beginning A, B are two points (0,0) position of the top left corner position (0,0).
(1) is a digital connection between the required output of AB, outputs the round moves down to the right A, B;
(2) using a bool variable represents the direction of the print, the print taken after each round of inverse;
(3 ) when A is down to the extreme right, B moves to the lowermost end of the right, until the end A move to the lower right, the output follows FIG.
Here Insert Picture Description
Here Insert Picture Description

Here Insert Picture Description

3. core code

Update (1) A, B the point

aC, bC represents a column, aR, aR denotes a row, when moved to the lowermost end aR. Four variables updated code can be changed to use EXP1 EXE2:? EXP3 can be more concise.

while(aR!=height)
	{
		print(arr,dir,aR,aC,bR,bC);
		if(aC < width - 1)
		{
			aC++;
		}
		else
		{
			aR++;
		}
		if(bR < height - 1)
		{
			bR++;
		}
		else
		{
			bC++;
		}
		dir = !dir;
	}

Number between (2) Print A, B

Print A, B between the actual digital print is A, B numbers on the connection each time you change a row and column numbers can be. Obliquely from left to right to print, and B, a Save the line, to add a row to continue printing until it encounters A. From right to left oblique print, and A, to add a row, a column to reduce, then printing continues until it encounters a B. The idea initially is to use an intermediate variable recording position, and then change the intermediate variables to find the next point until the completion of the printing, both print and would not change the A, B, coordinates. Then think this may be packaged as part of a function, the advantage is not changed parameter arguments, direct change A, B coordinates for printing but also eliminates redundant variables.

void print(int arr[][width],bool dir,int aR,int aC,int bR,int bC)
{
	if(dir)
		{
			while(bR >= aR)
			{
				cout<<arr[bR--][bC++]<<" ";
			}
		}
		else
		{
			while(aR <= bR)
			{
				cout<<arr[aR++][aC--]<<" ";
			}
		}
}

4. The complete code

#include<iostream>
#define height 4
#define width 4
using namespace std;

void print(int arr[][width],bool dir,int aR,int aC,int bR,int bC)
{
	if(dir)
		{
			while(bR >= aR)
			{
				cout<<arr[bR--][bC++]<<" ";
			}
		}
		else
		{
			while(aR <= bR)
			{
				cout<<arr[aR++][aC--]<<" ";
			}
		}
}

int main()
{
	//int arr[height][width] = {1,2,3,4,5,6,7,8,9};
	int arr[height][width] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
	int aR = 0,aC = 0,bR = 0,bC = 0;
	bool dir = true;
	
	while(aR!=height)
	{
		print(arr,dir,aR,aC,bR,bC);
		if(aC < width - 1)
		{
			aC++;
		}
		else
		{
			aR++;
		}
		if(bR < height - 1)
		{
			bR++;
		}
		else
		{
			bC++;
		}
		dir = !dir;
	}


	system("pause");
	return 0;
}

5. The output

Here Insert Picture Description

Published 51 original articles · won praise 1 · views 1386

Guess you like

Origin blog.csdn.net/shi_xiao_xuan/article/details/103673365