using namespace std;
int main()
{
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	int i,j,n=1,a[100][100];
	cin >>i>>j;
	int x=0,y=j-1;
	a[x][y]=1;
	while(1)
	{
		if(n==i*j)
			break;
		while(1)
		{
			if(n==i*j)
			break;
			if(x<i-1&&a[x+1][y]==0)
			{
				n++;
				x++;
				a[x][y]=n;
			}
			else break;
			if(n==i*j)
			break;
		}
		while(1)
		{
			if(n==i*j)
				break;
			if(y>0&&a[x][y-1]==0)
			{
				n++;
				y--;
				a[x][y]=n;
			}
			else break;
			if(n==i*j)
			break;
			
		}
		while(1)
		{
			if(n==i*j)
			break;
			if(x>0&&a[x-1][y]==0)
			{
				n++;
				x--;
				a[x][y]=n;
			}
			else break;
			if(n==i*j)
			break;
		}
		while(1)
		{
			if(n==i*j)
				break;
			if(y<j-1&&a[x][y+1]==0)
			{
				n++;
				y++;
				a[x][y]=n;
			}
			else break;
			if(n==i*j)
			break;
		}
			if(n==i*j)
			break;
	}
	for(int k=0;k<i;k++)
	{
		for(int l=0;l<j;l++)
			{
				printf("%5d",a[k][l]);
			}
			cout << endl;
	}
	return 0;	
 } 

0 comments

No comments so far...

Information

ID
78
Time
1000ms
Memory
256MiB
Difficulty
7
Tags
# Submissions
668
Accepted
172
Uploaded By