Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- using namespace std;
- int main()
- {
- int n, m;
- cin >> n >> m;
- int a[n+2][m+2];
- memset(a, 0, sizeof(a));
- for (int i = 1; i < n+1; i++)
- for (int j = 1; j < m+1; j++)
- cin >> a[i][j];
- int x = 1, y = 1, flag = 0;
- for (int i = 0; i < n*m; i++)
- {
- cout << a[x][y] << " ";
- a[x][y] = 0;
- if (flag == 0)
- {
- if (a[x][y+1] != 0) y++;
- else { flag = 1; x++; }
- continue;
- }
- if (flag == 1)
- {
- if (a[x+1][y] != 0) x++;
- else { flag = 2; y--; }
- continue;
- }
- if (flag == 2)
- {
- if (a[x][y-1] != 0) y--;
- else { flag = 3; x--; }
- continue;
- }
- if (flag == 3)
- {
- if (a[x-1][y] != 0) x--;
- else { flag = 0; y++; }
- continue;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement