fork download
  1. //Aayush Agarwal - SNAKE
  2. #include <stdio.h>
  3.  
  4. int main() {
  5. int T, N, X, Y, arr[1001][1001],
  6. i, j, rowTop, colLeft, rowBottom, colRight, dir, value;
  7. scanf("%d", &T);
  8. while(T--) {
  9. scanf("%d %d %d", &N, &X, &Y); //input
  10.  
  11. dir = 1; //flag indicating direction for diagonal filling
  12. value = 1; //counter of value to store in array
  13.  
  14. //for lower half including principal diagonal
  15. rowTop = rowBottom = N-1;
  16. colLeft = colRight = 0;
  17. for(; rowTop >= 0; rowTop--, colRight++, dir ^= 1)
  18. if(dir == 0) //fill top to bottom
  19. for(i = rowTop, j = colLeft; i <= rowBottom; i++, j++)
  20. arr[i][j] = value++;
  21. else //fill bottom to top
  22. for(i = rowBottom, j = colRight; i >= rowTop; i--, j--)
  23. arr[i][j] = value++;
  24.  
  25. //for upper half
  26. rowTop = 0;
  27. rowBottom = N-2;
  28. colLeft = 1;
  29. colRight = N-1;
  30. for(; rowBottom >= 0; rowBottom--, colLeft++, dir ^= 1)
  31. if(dir == 0)
  32. for(i = rowTop, j = colLeft; i <= rowBottom; i++, j++)
  33. arr[i][j] = value++;
  34. else
  35. for(i = rowBottom, j = colRight; i >= rowTop; i--, j--)
  36. arr[i][j] = value++;
  37.  
  38. printf("%d\n", arr[X-1][Y-1]); //output
  39. }
  40. return 0;
  41. }
Success #stdin #stdout 0s 5852KB
stdin
2
4 2 3
6 1 5
stdout
12
34