c language meander-shaped access

The number of take-shaped back

Problem description
  meandering fetch access is along side of the matrix, if the current direction has numerous desirable or take off, then 90 degrees left. A start left corner of the matrix, the downward direction.
Input format
  input of the first row is a positive integer of not more than two of m 200, n, denotes the matrix of rows and columns. Next, each row of n row m is an integer, denotes the matrix.
Output format
  output a single line, a total of mn number, taken as an input matrix meandering number of results obtained. Between the number separated by a space, end of the line do not have extra spaces.
Sample input
. 3. 3
. 1 2. 3
. 4. 5. 6
. 7. 8. 9
Sample Output
147,896,325
sample input
. 3 2
. 1 2
. 3. 4
. 5. 6
Sample Output
135642

problem analysis:

	关于回形问题,或者是数组中顺时针或者逆时针走法输出的问题,递归方法是一个不错的解决问题的方法,只要理解如何调转方向,就没问题
	
	就本题而言,步骤如下:
		1,设置一个标志数组,用来判定是否已经打印过该位置上的数据
		2,写出打印输出的判断条件,除了要满足在数组内(也就是不超过 m行 n列),还要判断是否打印过,也就是标志位数组的值是否为0
		3,设置一个常数变量 cn++, 根据 cn%4 的值来调转方向(数组就四条边)
		4,递归的使用,以及递归函数中 参数的变化(调转方向)

Code shows (Verified):

#include <stdio.h>
#include <stdlib.h>
#include "string.h"
#define MAX 200
int a[MAX][MAX]; //存储输入的数据
int b[MAX][MAX]; //是否取过的标志位 数组
int m, n;
int cn = 0;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

void print(int i,int j)		//从a[0][0]位置 开始打印
{
	//int b[MAX][MAX];
	//memset(b, 0, sizeof(b));	//初始化 标志数组b[][]=0 
	//int cn = 0;			//cycle number  cn 圈数 一个矩阵 四条边 每次输出一条边的数据  可用递归算法
						// 每次输出一行或者一列  cn++
	//int i = 0, j = 0;  不能在递归中这样给数据赋值  需要提前赋值结束 可以定义全局变量
	if (i<m && i>=0 && j<n && j>=0 && b[i][j]==0)		//判定输出条件
	{
		printf("%d ", a[i][j]);
		b[i][j] = 1;
	}
	else  // 当不满足条件时 说明输出位置已经到了边缘 进行cn++
	{
		cn++;
		return;		//跳出判定条件
	}
	// 递归开始 从第一列开始 两圈递归输出所有数据 
	if (cn % 4 == 0)
		print(i + 1, j);
	if (cn % 4 == 1)
		print(i, j + 1);
	if (cn % 4 == 2)
		print(i - 1, j);
	if (cn % 4 == 3)
		print(i, j - 1);		
	// 一圈 递归输出的数据会有丢失 所以要再加一圈递归
	
	if (cn % 4 == 0)
		print(i + 1, j);
	if (cn % 4 == 1)
		print(i, j + 1);
	if (cn % 4 == 2)
		print(i - 1, j);
	if (cn % 4 == 3)
		print(i, j-1);
		
	return;		//void 
}

int main(int argc, char *argv[]) 
{
	//	int a[MAX][MAX];
	int i, j;
	
	scanf("%d%d", &m, &n);
	
	for (i = 0; i < m;i++)
	for (j = 0; j < n; j++)
	{
		scanf("%d", &a[i][j]);
	}
	
	memset(b, 0, sizeof(b));//初始化 标志数组b[][]=0 	string.h
	
	print(0,0); //调用函数打印输出
	
	return 0;
}

Bubble:
In fact, this type of topic is that the end use of recursion and traverse the array of print output, involves:

参数变化-->改变方向,
cn 变量 --> 改变方向,
递归调用-->遍历打印,
标志位数组-->是否打印过
判断条件-->打印输出

ok, call it a day.

Guess you like

Origin blog.csdn.net/qq_42124842/article/details/90738252