回形遍历(螺旋遍历)实现-C语言版

最近帮同学写一个二维数组的回形遍历,看了一下,没什么好思路,百度,google了一把,使用递归算法还是比较好理解的,记录已备用
1 什么是螺旋遍历?
2 实现的思路是递归: 
Solution: There are several ways to solve this problem, but I am mentioning a method that is intuitive to understand and easy to implement. The idea is to consider the matrix similar to onion which can be peeled layer after layer. We can use the same approach to print the outer layer of the matrix and keep doing it recursively on a smaller matrix (with 1 less row and 1 less column).
图形理解:
3 C语言实现: 

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. void print_layer_top_right(int a[][4], int col, int row, int collen, int rowlen);
  4. void print_layer_bottom_left(int a[][4], int col, int row, int collen, int rowlen);

  5. int main(void)
  6. {
  7. int a[5][4] = {
  8. {1,2,3,4},
  9. {5,6,7,8},
  10. {9,10,11,12},
  11. {13,14,15,16},
  12. {17,18,19,20}
  13. };

  14. print_layer_top_right(a,0,0,3,4);
  15. exit(0);
  16. }

  17. //
  18. // prints the top and right shells of the matrix
  19. //
  20. void print_layer_top_right(int a[][4], int col, int row, int collen, int rowlen)
  21. {
  22. int i = 0, j = 0;

  23. // print the row
  24. for(= col; i<=collen; i++)
  25. {
  26. printf("%d,", a[row][i]);
  27. }

  28. //print the column 
  29. for(= row + 1; j <= rowlen; j++) 
  30. {
  31. printf("%d,", a[j][collen]);
  32. }

  33. // see if we have more cells left 
  34. if(collen-col > 0)
  35. {
  36. // 'recursively' call the function to print the bottom-left layer
  37. print_layer_bottom_left(a, col, row + 1, collen-1, rowlen); 
  38. }
  39. }

  40. //
  41. // prints the bottom and left shells of the matrix

  42. void print_layer_bottom_left(int a[][4], int col, int row, int collen, int rowlen)
  43. {
  44. int i = 0, j = 0;

  45. //print the row of the matrix in reverse
  46. for(= collen; i>=col; i--)
  47. {
  48. printf("%d,", a[rowlen][i]);
  49. }

  50. // print the last column of the matrix in reverse
  51. for(= rowlen - 1; j >= row; j--)
  52. {
  53. printf("%d,", a[j][col]);
  54. }

  55. if(collen-col > 0)
  56. {
  57. // 'recursively' call the function to print the top-right layer
  58. print_layer_top_right(a, col+1, row, collen, rowlen-1);
  59. }
  60. }

C Code2:

//打印回形数组
#include<iostream>
using namespace std;

void Traval(int a[][5],int row,int col,int rowlen,int collen)
{
	if (row<=rowlen&&col<=collen)
	{
		//打印上行
		for(int j1=col;j1<=collen;j1++)
			printf("%d,",a[row][j1]);
		//打印右列
		for(int i1=row+1;i1<=rowlen;i1++)
			printf("%d,",a[i1][collen]);
		//打印下行
		for(int j2=collen-1;j2>=col;j2--)
			printf("%d,",a[rowlen][j2]);
		//打印左列
		for(int i2=rowlen-1;i2>=row+1;i2--)
			printf("%d,",a[i2][col]);
	}
	else
		return;
	Traval(a,row+1,col+1,rowlen-1,collen-1);

}

int main()
{
	//int aa[4][5]={{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15},{16,17,18,19,20}};//4行5列
	int aa[5][5]={{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15},{16,17,18,19,20},{21,22,23,24,25}};//5行5列
	cout<<"回形打印:"<<endl;
	Traval(aa,0,0,4,3);//其中,4和3可以随意设置,按要求打印
	cout<<endl;
	return 0;
}

输出结果:

回形打印:
1,2,3,4,9,14,19,24,23,22,21,16,11,6,7,8,13,18,17,12,
请按任意键继续. . .

 

4 ref: 
http://shashank7s.blogspot.com/2011/04/wap-to-print-2d-array-matrix-in-spiral.html
http://ideone.com/Ik6bFr

 

转自:http://blog.chinaunix.net/uid-22334392-id-3730188.html

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值