Toliak

ariken1

Jul 2nd, 2017
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.22 KB | None | 0 0
  1. #include <iostream>
  2. //#include <iomanip>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. bool findIn2D(int what__, vector<vector<int>>& where__);
  8. void cout2D(vector<vector<int>>& what__);
  9.  
  10. int main() {
  11.     int n; cin >> n;        //Считываем количество
  12.     vector<vector<int>> a(n);   //двумерный массив
  13.     for (int i=0; i < n; i++) {     //Это обычная инициализация двумерного массива в c++
  14.         vector<int> into(n,-1);     //А теперь вспомни свой питон
  15.         a[i] = into;        //Тут оператор будет как копирование
  16.     }
  17.     int counterX = -1, counterY = 0;
  18.     int globalCounter = 0;
  19.     while (findIn2D(-1, a)) {   //Если в массиве есть -1, то пройти еще разок
  20.         while (counterX + 1 != n && a[counterY][counterX + 1] == -1) {      //left
  21.             counterX++;
  22.             globalCounter++;
  23.             a[counterY][counterX] = globalCounter;
  24.         }
  25.         while (counterY + 1 != n && a[counterY+1][counterX] == -1) {        //down (не ты, я не троль)
  26.             counterY++;
  27.             globalCounter++;
  28.             a[counterY][counterX] = globalCounter;
  29.         }
  30.         while (counterX - 1 > -1 && a[counterY][counterX - 1] == -1) {      //ультра right
  31.             counterX--;
  32.             globalCounter++;
  33.             a[counterY][counterX] = globalCounter;
  34.         }
  35.         while (counterY - 1 > -1 && a[counterY - 1][counterX] == -1) {      //up
  36.             counterY--;
  37.             globalCounter++;
  38.             a[counterY][counterX] = globalCounter;
  39.         }
  40.     }
  41.     cout2D(a);  //вывод массива
  42.     return 0;
  43. }
  44.  
  45. bool findIn2D(int what__, vector<vector<int>>& where__) {       //что найти и ссылка на массив (как указатель, только удобнее (но не всегда))
  46.     for (int i = 0; i < where__.size(); i++) {
  47.         for (int l = 0; l < where__[i].size(); l++) {
  48.             if (where__[i][l] == what__) return true;   //если нашлось то, что мы искали
  49.         }
  50.     }
  51.     return false;       //если не нашлось
  52. }
  53.  
  54. void cout2D(vector<vector<int>>& what__) {          //ссылка на массив
  55.     for (int i = 0; i < what__.size(); i++) {
  56.         for (int l = 0; l < what__[i].size(); l++) {
  57.             //cout << setw(10);         //для красоты
  58.             cout << what__[i][l] << " ";    //вывод
  59.         }
  60.         cout << "\n";
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment