audreych

12884 - Patruto and Spongebob run away

Dec 22nd, 2020 (edited)
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.55 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int arr[1000][1000];
  4. int main(){
  5.     int n;
  6.     scanf("%d", &n);
  7.     int len = 2*n;
  8.     // n = 3 6x6 grid
  9.     // 1  2  3  4  5  6
  10.     // 20 21 22 23 24 7
  11.     // 19 32 33 34 25 8
  12.     // 18 31 36 35 26 9
  13.     // 17 30 29 28 27 10
  14.     // 16 15 14 13 12 11
  15.     int m = 1; //filling the array
  16.     //q is the loop how many time he goes from outer to inner and repeats
  17.     //so, 1 ~ n, means that patrick goes to outer from inner n times
  18.     for(int q = 1; q <= n; q++){
  19.         //right
  20.         for(int i = q; i <= q; i++){
  21.             for(int j = q; j <= len - q; j++){
  22.                 arr[i][j] = m++;
  23.                 //1 2 3 4 5
  24.             }
  25.         }
  26.         // down
  27.         for(int j = len + 1 - q; j <= len + 1 - q; j++){
  28.             for(int i = q; i <=len - q; i++){
  29.                 arr[i][j] = m++;
  30.                 //6 7 8 9 10
  31.             }
  32.         }
  33.         //left
  34.         for(int i = len + 1 - q; i <= len + 1 - q; i++){
  35.             for(int j = len + 1 - q; j >= q + 1; j--){
  36.                 arr[i][j] = m++;
  37.                 //11 12 13 14 15
  38.             }
  39.         }
  40.         //top
  41.         for(int j = q; j <= q; j++){
  42.             for(int i = len + 1 - q; i >= q+1; i--){
  43.                 arr[i][j] = m++;
  44.                 //16 17 18 19 20   
  45.             }
  46.         }  
  47.        
  48.     }
  49.     int i, j;
  50.     for( i = 1; i <=len ; i++){
  51.         for(j = 1; j <= len- 1; j++){
  52.             printf("%d ", arr[i][j]);
  53.         }
  54.         printf("%d\n", arr[i][j]);
  55.     }
  56.     return 0;
  57. }
  58. //note : right
  59. //i does not change, but j keeps incrementing by 1
  60. //i and j always start from q as when q increase i does not start at the same tile
  61. //and start depends on q
  62. //repeat the step for everything, find the relation between q and len and i or j
  63. //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