Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <vector>
- #include <cassert>
- #include <string>
- #include <algorithm>
- using namespace std;
- const int MAXN = 101;
- int a[MAXN][MAXN];
- int dx[4] = {0, 1, 0, -1};
- int dy[4] = {1, 0, -1, 0};
- int main()
- {
- int n,m;
- scanf("%d%d",&n,&m);
- int x = 0;
- int y = 0;
- int dir = 0;
- int k = 1;
- a[0][0] = k;
- k++;
- while( k <= n*m )
- {
- int nextX = x + dx[dir];
- int nextY = y + dy[dir];
- if( 0<= nextX && nextX < n && 0<= nextY && nextY < m && a[nextX][nextY] == 0)
- {
- x = nextX;
- y = nextY;
- a[x][y] = k;
- k++;
- }
- else
- {
- dir++;
- if(dir==4)
- dir=0;
- }
- }
- for(int i=0; i<n; i++)
- {
- for(int j=0; j<m; j++)
- {
- if(j!=0)
- printf(" ");
- printf("%d", a[i][j]);
- }
- printf("\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment