目标是创建一个矩阵,其中所有0都连接(向上,向下,向左或向右触摸)。我生成了一个0/19的10/19矩阵并随机添加了1。现在我想逐步通过矩阵并更改值,使所有0都连接起来,但我正在努力解决这个问题。非常感谢任何帮助!
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main () {
//generate matrix of 0s
int map[10][19] = ;
int a = 10;
int b = 19;
//display matrix
for (int i = 0; i < a; ++i) {
for (int j = 0; j < b; ++j) {
std::cout << map[i][j] << ' ';
}
std::cout << std::endl;
}
cout << "\n" << endl;
//adds 1s to matrix
for (int i = 0; i < a; ++i) {
for (int j = 0; j < b; ++j) {
map[i][j] = 0 + rand() % 2;
}
}
//display matrix
for (int i = 0; i < a; ++i) {
for (int j = 0; j < b; ++j) {
std::cout << map[i][j] << ' ';
}
std::cout << std::endl;
}
cout << "\n" << endl;
首先输出:
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 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 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 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 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 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 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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
第二次输出:
1 1 1 0 0 0 0 0 1 1 0 1 0 0 1 1 1 1 0
0 1 1 1 1 0 1 1 0 0 1 1 0 0 1 0 1 1 1
1 1 1 0 1 0 1 1 0 1 0 1 1 1 0 0 0 0 0
0 0 1 0 0 1 0 1 1 1 0 0 1 0 1 1 1 1 0
1 0 1 1 1 0 1 1 1 1 1 0 1 0 0 1 1 0 0
0 0 1 0 0 1 1 1 0 1 0 1 1 0 0 1 1 1 1
1 0 0 1 0 0 0 1 0 0 1 0 1 0 0 0 0 1 0
0 1 1 1 0 1 1 0 1 0 1 0 0 0 1 1 0 0 0
1 1 0 1 1 0 1 0 1 0 1 0 0 0 0 0 0 0 0
0 1 0 0 0 0 1 1 1 1 1 0 1 1 1 0 1 1 0