Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int a[50][50];
- int visited[50][50];
- int dx[4] = {0, 1, 0, -1};
- int dy[4] = {1, 0, -1, 0};
- bool isInside (int i, int j, int n, int m) {
- return 1 <= i and i <= n and 1 <= j and j <= m;
- }
- int main() {
- int n, m, currentI = 1, currentJ = 1, currentDirection = 0;
- cin >> n >> m;
- for (int i = 1; i <= n; ++i) {
- for (int j = 1; j <= m; ++j) {
- cin >> a[i][j];
- }
- }
- for (int i = 1; i < n * m; ++i) {
- cout << a[currentI][currentJ] << ' ';
- visited[currentI][currentJ] = 1;
- int nextI = currentI + dx[currentDirection];
- int nextJ = currentJ + dy[currentDirection];
- while (!isInside(nextI, nextJ, n, m) or visited[nextI][nextJ]) {
- currentDirection += 1;
- currentDirection %= 4;
- nextI = currentI + dx[currentDirection];
- nextJ = currentJ + dy[currentDirection];
- }
- currentI = nextI;
- currentJ = nextJ;
- }
- cout << a[currentI][currentJ];
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement