ChoDog

Задача V. Разноцветные диагонали

Dec 1st, 2019
543
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.37 KB | None | 0 0
  1. #include <iostream>
  2. #include <set>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <limits>
  6. #include <string>
  7. #include <queue>
  8.  
  9. #define ll unsigned long long
  10.  
  11. using namespace std;
  12.  
  13. int main() {
  14.     int n;
  15.     cin >> n;
  16.     vector<vector<bool>> free(n , vector<bool>(n , true));
  17.     vector<vector<int>> color(n , vector<int>(n , 0));
  18.  
  19.     queue<pair<int, int>> q;
  20.    
  21.     for (int i = 0; i < n; i++) {
  22.         q.push(make_pair(i, i));
  23.         q.push(make_pair(n - i - 1, i));
  24.         free[i][i] = false;
  25.         free[n - i - 1][i] = false;
  26.         free[i][i] = 0;
  27.         free[n - i - 1][i] = 0;
  28.     }
  29.  
  30.     while (!q.empty()) {
  31.         int x = q.front().first, y = q.front().second;
  32.  
  33.         if (x + 1 < n && free[x + 1][y] ) {
  34.             color[x + 1][y] = color[x][y] % 25 + 1;
  35.             free[x + 1][y] = false;
  36.             q.push(make_pair(x + 1, y));
  37.         }
  38.         if (x - 1 >= 0 && free[x - 1][y]) {
  39.             color[x - 1][y] = color[x][y] % 25 + 1;
  40.             free[x - 1][y] = false;
  41.             q.push(make_pair(x - 1, y));
  42.         }
  43.         if (y + 1 < n && free[x][y + 1]) {
  44.             color[x][y + 1] = color[x][y] % 25 + 1;
  45.             free[x][y + 1] = false;
  46.             q.push(make_pair(x,y + 1));
  47.         }
  48.         if (y - 1 >= 0 && free[x][y - 1]) {
  49.             color[x][y - 1] = color[x][y] % 25 + 1;
  50.             free[x][y - 1] = false;
  51.             q.push(make_pair(x, y - 1));
  52.         }
  53.  
  54.         q.pop();
  55.     }
  56.  
  57.     for (int i = 0; i < n; i++) {
  58.         for (int j = 0; j < n; j++) {
  59.             cout << (char) ('a' + color[i][j]);
  60.         }
  61.         cout << endl;
  62.     }
  63.  
  64.     return 0;
  65. }
Add Comment
Please, Sign In to add comment