C ++中用于执行以下操作的适当方法是什么:
对于此示例,允许x等于更大的int
。
xxxxxxxxxxxxxxxxxx
xx xxxxxx0000xx
xx xxxxx xx
xx xxxxx xx
xx xx
xx xx
xxxxxxxxxxxxxxxxxx
这个2D std::vector<int>
就是我现在拥有的。我需要开始使用从0
位置的索引开始增加的值来填充空白,以便发生以下操作:
xxxxxxxxxxxxxxxxxx
xx xxxxxx0000xx
xx 99xxxxx1111xx
xx 98xxxxx2222xx
xx 98765433333xx
xx 98765444444xx
xxxxxxxxxxxxxxxxxx
继续直到填空。我查看了std::iota
,但我正在使用C ++ 98(强制限制)。阅读std::generate
,这似乎是一个很好的方法,不确定实施。
答案 0 :(得分:0)
假设您将row-major order中的矩阵写入名为std::vector<int>
的{{1}}中,m
是一行中的位数,这将是广度的实现 - 第一搜索算法:
width
答案 1 :(得分:0)
我的尝试并将@flyx代码转换为可以支持std::vector<std::vector<int>
的表单。我正在努力进行对角线检查。
我知道缩进已关闭,直接从IDE复制。
queue<pair<int, int> > worklist;
for (size_t i = 0; i < nlevel.size(); i++) {
for (size_t j = 0; j < nlevel[i].size(); j++) {
if (nlevel[i][j] == 0) {
/*cout << i << ' ' << j << endl;*/
worklist.push(make_pair(i,j));
}
}
}
while (worklist.size() > 0) {
pair<int, int> cur = worklist.front();
worklist.pop();
const int newval = nlevel[cur.first][cur.second] + 1;
int directions[4] = {0, 0, 0, 0};
int width = nlevel[0].size();
int height = nlevel.size();
if (cur.first > 0) {
// we are not on the first row, check upwards
directions[0] = -1;
}
if (cur.second > 0) {
// we are not on the first column, check left
directions[2] = -1;
}
if (cur.first < height) {
// we are not on the last row, check downwards
directions[1] = 1;
}
if (cur.second < width) {
// we are not on the last column, check right
directions[3] = 1;
}
for (int i = 0; i < 2; i++) {
for (int j = 2; j < 4; j++) {
if (directions[i] != 0 && directions[j] != 0) {
const int xindex = cur.first + directions[j];
const int yindex = cur.second + directions[i];
//cout << xindex << ' ' << yindex << endl;
if (nlevel[xindex][yindex] == -1) {
nlevel[xindex][yindex] = newval;
worklist.push(make_pair(xindex, yindex));
}
}
}
}
}
cout << endl;
for (size_t i = 0; i < nlevel.size(); i++) {
for (size_t j = 0; j < nlevel[i].size(); j++) {
cout << nlevel[i][j];
}
cout << endl;
}
我相信我已经让它大部分都在工作了,但是在输出上没有间距也难以遵循!从来没有,这是输出:
xxxxXxxxxXxxxxXxxxxXxxxxXxxxxX
xxxxxx xxxxx xxx xx
xxxxx xxx xx x
xx xx x x x
X xx x x x
x xx x x
x xxxxxxxxxxxxxx x x
x xxxxxxxxxxxxxxxxxxxxx x
xFFFF x xxxxxxxxxxxxxx x
XFFFF x xxxxxxxx x
x x 1 x
xxxxxxx 2 xxx x
xxxxxx 3 x x
x x
Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2
-2-2-2-2-2-2-1-1-1-1-1-1-2-2-2-2-2-1-1-1-1-1-1-2-2-2-1-1-2-2
-2-2-2-2-2-1-1-1-1-1-1-1-1-2-2-2-1-1-1-1-1-1-1-2-2-1-1-1-1-2
-2-2-1-1-1-1-1-2-2-1-1-1-1-1-2-1-1-1-1-1-1-1-1-2-1-1-1-1-1-2
-2-1-1-1-1-1-1-2-2-1-1-1-1-1-1-1-1-1-1-1-1-1-1-2-1-1-2-1-1-2
-2-1-1-1-1-1-1-2-2-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-2-1-1-2
-2-1-1-1-1-1-1-2-2-2-2-2-2-2-2-2-2-2-2-2-2-1-1-1-1-1-2-1-1-2
-2-1-1-1-1-1-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-1-1-2
-20000-1-2-1-1-2-2-2-2-2-2-2-2-2-2-2-2-2-2-1-1-1-1-1-1-2
-20000-1-2-1-1-1-1-2-2-2-2-2-2-2-2-1-1-1-1-1-1-1-1-1-1-2
-2-1-1-1-1-1-2-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-2
-2-2-2-2-2-2-2-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-2-2-2-1-1-1-1-2
-2-2-2-2-2-2-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-2-1-1-1-1-1-2
-2-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-2
-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2
-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2
-2-2-2-2-2-27779911-2-2-2-2-2171719192121-2-2-22727-2-2
-2-2-2-2-266688101012-2-2-216161818202022-2-226262628-2
-2-255555-2-299111113-21515171719192121-22525252727-2
-2444444-2-21010101212141416161818202022-22424-22628-2
-2333333-2-21111111113131515171719192121232325-22927-2
-2222222-2-2-2-2-2-2-2-2-2-2-2-2-2-22022222424-22830-2
-211111-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-23129-2
-200001-25050-2-2-2-2-2-2-2-2-2-2-2-2-2-2343432323032-2
-200001-251494947-2-2-2-2-2-2-2-239373735353333313331-2
-211111-250504848464644444242404038383636343432343234-2
-2-2-2-2-2-2-2514949474745454343414139393737-2-2-235333533-2
-2-2-2-2-2-25250504848464644444242404038383838-23634363436-2
-2-1-1-1-1535151494947474545434341413939393939373537353735-2
-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2
最后一个网格的左下角有一个部分仍然有-1
个值并且没有填充。