Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- void left(int& i, int& j)
- {
- j--;
- }
- void right(int& i, int& j)
- {
- j++;
- }
- void up(int& i, int& j)
- {
- i--;
- }
- void down(int& i, int& j)
- {
- i++;
- }
- int b[5][5] = {{0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0},
- {0, 0, 0, 0, 0}};
- bool isFree(int i, int j, int n)
- {
- if (b[i][j] != 0 || i < 0 || i >= n || j < 0 || j >= n)
- {
- return false;
- }
- return true;
- }
- int main()
- {
- int n = 5;
- int a[5][5] = {{1, 2, 3, 4, 5},
- {6, 7, 8, 9, 10},
- {11, 12, 13, 14, 15},
- {16, 17, 18, 19, 20},
- {21, 22, 23, 24, 25}};
- /*
- int si = 0, sj = 0;
- for (int k = 0; k <= n / 2; k++)
- {
- while (isFree(si, sj, n))
- {
- b[si][sj] = 1;
- cout << a[si][sj] << " ";
- right(si, sj);
- }
- sj--;
- si++;
- while (isFree(si, sj, n))
- {
- b[si][sj] = 1;
- cout << a[si][sj] << " ";
- down(si, sj);
- }
- sj--;
- si--;
- while (isFree(si, sj, n))
- {
- b[si][sj] = 1;
- cout << a[si][sj] << " ";
- left(si, sj);
- }
- si--;
- sj++;
- while (isFree(si, sj, n))
- {
- b[si][sj] = 1;
- cout << a[si][sj] << " ";
- up(si, sj);
- }
- si++;
- sj++;
- }
- */
- // dlur
- int si = 0, sj = n - 1;
- for (int k = 0; k <= n / 2; k++)
- {
- while (isFree(si, sj, n))
- {
- b[si][sj] = 1;
- cout << a[si][sj] << " ";
- down(si, sj);
- }
- sj--;
- si--;
- while (isFree(si, sj, n))
- {
- b[si][sj] = 1;
- cout << a[si][sj] << " ";
- left(si, sj);
- }
- si--;
- sj++;
- while (isFree(si, sj, n))
- {
- b[si][sj] = 1;
- cout << a[si][sj] << " ";
- up(si, sj);
- }
- si++;
- sj++;
- while (isFree(si, sj, n))
- {
- b[si][sj] = 1;
- cout << a[si][sj] << " ";
- right(si, sj);
- }
- sj--;
- si++;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement