[英]Method to sequentially fill 2D vector<int> from specific index?
什么是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
,這似乎是一個不錯的方法,不確定實現。
假設您以行優先順序將矩陣寫入名為m
的std::vector<int>
並且width
是一行中的位數,則這將是廣度優先搜索算法的實現:
// these are the x values
#define OBSTACLE -1
// these are empty fields
#define EMPTY -2
std::vector<int> m;
int width;
// TODO: fill the vector here with OBSTACLEs and EMPTY and set the width!
// the list contains indexes of filled, yet unprocessed fields
std::queue<int> worklist;
// now initialize the starting zeroes. I am using the indexes
// from your example matrix here.
worklist.push(1 * width + 12); m[1 * width + 12] = 0; // second row, 13th field
worklist.push(1 * width + 13); m[1 * width + 13] = 0; // second row, 14th field
worklist.push(1 * width + 14); m[1 * width + 14] = 0; // .. and so on.
worklist.push(1 * width + 15); m[1 * width + 15] = 0;
// algorithm
while (worklist.size() > 0) {
const int cur = worklist.front();
worklist.pop();
const int newval = m[cur] + 1;
int directions[4] = {0, 0, 0, 0};
if (cur >= width) {
// we are not on the first row, check upwards
directions[0] = -width;
}
if (cur % width != 0) {
// we are not on the first column, check left
directions[1] = -1;
}
if ((cur + 1) % width != 0) {
// we are not on the last column, check right
directions[2] = 1;
}
if (cur + width < m.size()) {
// we are not on the last row, check downwards
directions[3] = width;
}
// I am not calculating diagonal connections here; you also seem to
// want those. They would be trivial to add.
for (int i = 0; i < 4; ++i) {
if (directions[i] != 0) {
const int index = cur + directions[i];
if (m[index] == EMPTY) {
m[index] = newval;
worklist.push(index);
}
}
}
}
我試圖將@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
Xxxxxx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-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
值並且沒有填充。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.