Advertisement
Guest User

Untitled

a guest
May 20th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int a[50][50];
  6. int visited[50][50];
  7. int dx[4] = {0, 1, 0, -1};
  8. int dy[4] = {1, 0, -1, 0};
  9.  
  10. bool isInside (int i, int j, int n, int m) {
  11. return 1 <= i and i <= n and 1 <= j and j <= m;
  12. }
  13.  
  14. int main() {
  15. int n, m, currentI = 1, currentJ = 1, currentDirection = 0;
  16. cin >> n >> m;
  17. for (int i = 1; i <= n; ++i) {
  18. for (int j = 1; j <= m; ++j) {
  19. cin >> a[i][j];
  20. }
  21. }
  22. for (int i = 1; i < n * m; ++i) {
  23. cout << a[currentI][currentJ] << ' ';
  24. visited[currentI][currentJ] = 1;
  25. int nextI = currentI + dx[currentDirection];
  26. int nextJ = currentJ + dy[currentDirection];
  27. while (!isInside(nextI, nextJ, n, m) or visited[nextI][nextJ]) {
  28. currentDirection += 1;
  29. currentDirection %= 4;
  30. nextI = currentI + dx[currentDirection];
  31. nextJ = currentJ + dy[currentDirection];
  32. }
  33. currentI = nextI;
  34. currentJ = nextJ;
  35. }
  36. cout << a[currentI][currentJ];
  37. return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement