Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- int arr[1000][1000];
- int main(){
- int n;
- scanf("%d", &n);
- int len = 2*n;
- // n = 3 6x6 grid
- // 1 2 3 4 5 6
- // 20 21 22 23 24 7
- // 19 32 33 34 25 8
- // 18 31 36 35 26 9
- // 17 30 29 28 27 10
- // 16 15 14 13 12 11
- int m = 1; //filling the array
- //q is the loop how many time he goes from outer to inner and repeats
- //so, 1 ~ n, means that patrick goes to outer from inner n times
- for(int q = 1; q <= n; q++){
- //right
- for(int i = q; i <= q; i++){
- for(int j = q; j <= len - q; j++){
- arr[i][j] = m++;
- //1 2 3 4 5
- }
- }
- // down
- for(int j = len + 1 - q; j <= len + 1 - q; j++){
- for(int i = q; i <=len - q; i++){
- arr[i][j] = m++;
- //6 7 8 9 10
- }
- }
- //left
- for(int i = len + 1 - q; i <= len + 1 - q; i++){
- for(int j = len + 1 - q; j >= q + 1; j--){
- arr[i][j] = m++;
- //11 12 13 14 15
- }
- }
- //top
- for(int j = q; j <= q; j++){
- for(int i = len + 1 - q; i >= q+1; i--){
- arr[i][j] = m++;
- //16 17 18 19 20
- }
- }
- }
- int i, j;
- for( i = 1; i <=len ; i++){
- for(j = 1; j <= len- 1; j++){
- printf("%d ", arr[i][j]);
- }
- printf("%d\n", arr[i][j]);
- }
- return 0;
- }
- //note : right
- //i does not change, but j keeps incrementing by 1
- //i and j always start from q as when q increase i does not start at the same tile
- //and start depends on q
- //repeat the step for everything, find the relation between q and len and i or j
- //remember to change the loop for downward because this time j does not change, but i keeps incrementing by 1
Add Comment
Please, Sign In to add comment