Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. int n, m;
  9. cin >> n >> m;
  10.  
  11. int a[n+2][m+2];
  12. memset(a, 0, sizeof(a));
  13. for (int i = 1; i < n+1; i++)
  14. for (int j = 1; j < m+1; j++)
  15. cin >> a[i][j];
  16.  
  17. int x = 1, y = 1, flag = 0;
  18. for (int i = 0; i < n*m; i++)
  19. {
  20. cout << a[x][y] << " ";
  21. a[x][y] = 0;
  22. if (flag == 0)
  23. {
  24. if (a[x][y+1] != 0) y++;
  25. else { flag = 1; x++; }
  26. continue;
  27. }
  28. if (flag == 1)
  29. {
  30. if (a[x+1][y] != 0) x++;
  31. else { flag = 2; y--; }
  32. continue;
  33. }
  34. if (flag == 2)
  35. {
  36. if (a[x][y-1] != 0) y--;
  37. else { flag = 3; x--; }
  38. continue;
  39. }
  40. if (flag == 3)
  41. {
  42. if (a[x-1][y] != 0) x--;
  43. else { flag = 0; y++; }
  44. continue;
  45. }
  46. }
  47.  
  48. return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement