Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- //#include <iomanip>
- #include <vector>
- using namespace std;
- bool findIn2D(int what__, vector<vector<int>>& where__);
- void cout2D(vector<vector<int>>& what__);
- int main() {
- int n; cin >> n; //Считываем количество
- vector<vector<int>> a(n); //двумерный массив
- for (int i=0; i < n; i++) { //Это обычная инициализация двумерного массива в c++
- vector<int> into(n,-1); //А теперь вспомни свой питон
- a[i] = into; //Тут оператор будет как копирование
- }
- int counterX = -1, counterY = 0;
- int globalCounter = 0;
- while (findIn2D(-1, a)) { //Если в массиве есть -1, то пройти еще разок
- while (counterX + 1 != n && a[counterY][counterX + 1] == -1) { //left
- counterX++;
- globalCounter++;
- a[counterY][counterX] = globalCounter;
- }
- while (counterY + 1 != n && a[counterY+1][counterX] == -1) { //down (не ты, я не троль)
- counterY++;
- globalCounter++;
- a[counterY][counterX] = globalCounter;
- }
- while (counterX - 1 > -1 && a[counterY][counterX - 1] == -1) { //ультра right
- counterX--;
- globalCounter++;
- a[counterY][counterX] = globalCounter;
- }
- while (counterY - 1 > -1 && a[counterY - 1][counterX] == -1) { //up
- counterY--;
- globalCounter++;
- a[counterY][counterX] = globalCounter;
- }
- }
- cout2D(a); //вывод массива
- return 0;
- }
- bool findIn2D(int what__, vector<vector<int>>& where__) { //что найти и ссылка на массив (как указатель, только удобнее (но не всегда))
- for (int i = 0; i < where__.size(); i++) {
- for (int l = 0; l < where__[i].size(); l++) {
- if (where__[i][l] == what__) return true; //если нашлось то, что мы искали
- }
- }
- return false; //если не нашлось
- }
- void cout2D(vector<vector<int>>& what__) { //ссылка на массив
- for (int i = 0; i < what__.size(); i++) {
- for (int l = 0; l < what__[i].size(); l++) {
- //cout << setw(10); //для красоты
- cout << what__[i][l] << " "; //вывод
- }
- cout << "\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment