VIP test questions basic exercises to take the number (simulation) (C language)

 VIP test questions basic exercises to take the number (simulation)

Resource limit

Time limit: 1.0s Memory limit: 512.0MB

Problem Description

  Fetching the number is to take the number along the side of the matrix. If there are countless options in the current direction or have been taken, then turn left by 90 degrees. It starts at the upper left corner of the matrix, and the direction is downward.

Input format

  The first line of input is two positive integers m, n no more than 200, indicating the rows and columns of the matrix. The next m rows have n integers in each row, representing this matrix.

Output format

  The output is only one line, a total of mn numbers, which is the result of the input matrix shape fetching. Separate the numbers with a space, and do not have extra spaces at the end of the line.

Sample input

3 3
1 2 3
4 5 6
7 8 9

Sample output

1 4 7 8 9 6 3 2 5

Sample input

3 2
1 2
3 4
5 6

Sample output

1 3 5 6 4 2

 Implementation code:

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int a[201][201],book[201][201];//book为标志数组 
	int n,m;
	int i,j;
	scanf("%d%d",&m,&n);
	memset(book,0,sizeof(book));
	for(i=1;i<=m;i++)
	 for(j=1;j<=n;j++)
	 {
	 	scanf("%d",&a[i][j]);
	 	book[i][j]=1;
	 }
	int sum=0;
	
	printf("%d",a[1][1]);
	book[1][1]=0;
	sum++;
	
	i=2;j=1;//初始化一下 
	while(sum!=m*n)//输出全部的数时结束 
	{
		//模拟向下时候;
		while(book[i][j]==1&&i<=m)
		{
			book[i][j]=0;
			sum++;
			printf(" %d",a[i][j]);
			i++;
		} 
		i--;j++;
		//模拟向左的时候: 
		while(book[i][j]==1&&j<=n)
		{
			book[i][j]=0;
			sum++;
			printf(" %d",a[i][j]);
			j++;
		}
		//模拟向上的时候 
		i--;j--;
		while(book[i][j]==1&&i>=1)
		{
			book[i][j]=0;
			sum++;
			printf(" %d",a[i][j]);
			i--;
		}
		i++,j--;
		//模拟向左的时候
		while(book[i][j]==1&&j>=1)
		{
			book[i][j]=0;
			sum++;
			printf(" %d",a[i][j]);
			j--;
		}
		j++;i++; 
	}
	return 0;
}

 

Guess you like

Origin blog.csdn.net/with_wine/article/details/115010967