You are required to form a matrix of size where is the number of rows and is the number of columns. You are required to form the waves of numbers around the provided center, (0-based indexing).
Example:
Input format
The first line contains four integers , , , and denoting the number of rows, number of columns, coordinate of center, and coordinate of center.
Output format
Print the pattern for the provided input.
Constraints
Example:
- Size of the matrix: and
- Center coordinates: and (0 based indexing)
- Pattern:
4 4 4 4 4 4 4 4 4
4 3 3 3 3 3 3 3 4
4 3 2 2 2 2 2 3 4
4 3 2 1 1 1 2 3 4
4 3 2 1 0 1 2 3 4
4 3 2 1 1 1 2 3 4
4 3 2 2 2 2 2 3 4
4 3 3 3 3 3 3 3 4
4 4 4 4 4 4 4 4 4
You are given the values of (the values of and is 0-based indexed). Your task is to print the provided pattern.4 3 3 3 3 3 3 3 4
4 3 2 2 2 2 2 3 4
4 3 2 1 1 1 2 3 4
4 3 2 1 0 1 2 3 4
4 3 2 1 1 1 2 3 4
4 3 2 2 2 2 2 3 4
4 3 3 3 3 3 3 3 4
4 4 4 4 4 4 4 4 4
Input format
The first line contains four integers , , , and denoting the number of rows, number of columns, coordinate of center, and coordinate of center.
Output format
Print the pattern for the provided input.
Constraints
SAMPLE INPUT
10 10 5 5
SAMPLE OUTPUT
5 5 5 5 5 5 5 5 5 5 5 4 4 4 4 4 4 4 4 4 5 4 3 3 3 3 3 3 3 4 5 4 3 2 2 2 2 2 3 4 5 4 3 2 1 1 1 2 3 4 5 4 3 2 1 0 1 2 3 4 5 4 3 2 1 1 1 2 3 4 5 4 3 2 2 2 2 2 3 4 5 4 3 3 3 3 3 3 3 4 5 4 4 4 4 4 4 4 4 4
Explanation
Given input have shown output.
solution:-
#include<stdio.h>
#include<stdlib.h>
void main()
{
int r,c,a,b,i,j,k,l,m,n,p=0;
scanf("%d%d%d%d",&r,&c,&a,&b);
for(i=0;i<=r-1;i++)
{
for(j=0;j<=c-1;j++)
{
if(i==a&&j==b)
printf("%d ",p);
else
{
l=abs(a-i);
k=abs(b-j);
m=p+l;
n=p+k;
if(l>=k)
{
if(m>=0)
printf("%d ",m);
else printf("0 ");
}
else
{
if(n>=0)
printf("%d ",n);
else
printf("0 ");
}
}
}
printf("\n");
}
}
1 Comments
Nice
ReplyDelete