我必须编写扫雷。 我已经找到了一种方法来检查相邻的指数来计算当前指数周围有多少炸弹而不超出界限。但它很长,很丑,很可能效率低下。 它们只是每个可能触发范围的唯一单元格的一系列条件语句。我的问题是,有更短的方法吗?并且实现一维扭曲阵列会使它变得更容易/更难吗?
int count = 0;
for (int i = 0; i < WIDTH; i++){
for (int j = 0; j < HEIGHT; j++){
**// index 0,0**
if (i == 0 && j == 0 && field[i][j] != 10){
if (field[i][j + 1] == 10)
count++;
if (field[i + 1][j] == 10)
count++;
if (field[i + 1][j + 1] == 10)
count++;
}
**// index 0,9**
else if (i == 0 && j == HEIGHT - 1 && field[i][j] != 10){
if (field[i][j - 1] == 10)
count++;
if (field[i + 1][j - 1] == 10)
count++;
if (field[i + 1][j] == 10)
count++;
}
**// index 9,0**
else if (i == WIDTH - 1 && j == 0 && field[i][j] != 10){
if (field[i - 1][j] == 10)
count++;
if (field[i][j + 1] == 10)
count++;
if (field[i - 1][j + 1] == 10)
count++;
}
**// index 9,9**
else if (i == WIDTH - 1 && j == HEIGHT - 1 && field[i][j] != 10){
if (field[i - 1][j] == 10)
count++;
if (field[i][j - 1] == 10)
count++;
if (field[i - 1][j - 1] == 10)
count++;
}
**// if first row**
else if (i == 0 && (j != 0 && j != HEIGHT - 1) && field[i][j] != 10){
if (field[i][j - 1] == 10)
count++;
if (field[i + 1][j - 1] == 10)
count++;
if (field[i + 1][j] == 10)
count++;
if (field[i + 1][j + 1] == 10)
count++;
if (field[i][j + 1] == 10)
count++;
}
**// if last row**
else if (i == WIDTH - 1 && (j != 0 && j != HEIGHT - 1) && field[i][j] != 10){
if (field[i][j - 1] == 10)
count++;
if (field[i - 1][j - 1] == 10)
count++;
if (field[i - 1][j] == 10)
count++;
if (field[i - 1][j + 1] == 10)
count++;
if (field[i][j + 1] == 10)
count++;
}
**// if first col**
else if (j == 0 && (i != 0 && i != HEIGHT - 1) && field[i][j] != 10){
if (field[i - 1][j] == 10)
count++;
if (field[i - 1][j + 1] == 10)
count++;
if (field[i][j + 1] == 10)
count++;
if (field[i + 1][j] == 10)
count++;
if (field[i + 1][j + 1] == 10)
count++;
}
**// if last col**
else if (j == HEIGHT - 1 && (i != 0 && i != WIDTH - 1) && field[i][j] != 10){
if (field[i - 1][j - 1] == 10)
count++;
if (field[i - 1][j] == 10)
count++;
if (field[i][j - 1] == 10)
count++;
if (field[i + 1][j - 1] == 10)
count++;
if (field[i + 1][j] == 10)
count++;
}
**// Cells that are fully surrounded**
else if (field[i][j] != 10){
if (field[i - 1][j - 1] == 10)
count++;
if (field[i - 1][j] == 10)
count++;
if (field[i - 1][j + 1] == 10)
count++;
if (field[i][j - 1] == 10)
count++;
if (field[i][j + 1] == 10)
count++;
if (field[i + 1][j - 1] == 10)
count++;
if (field[i + 1][j] == 10)
count++;
if (field[i + 1][j + 1] == 10)
count++;
}
if (field[i][j] != 10)
field[i][j] = count;
count = 0;
}
}
答案 0 :(得分:1)
有一种标准方法可以处理这种情况 - 使用有效的移动定义一个数组并迭代它。以下是如何迭代8个相邻单元格的示例:
static int moves[8][2] = {{-1,0}, {-1, -1}, {0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, {-1, 1}};
for (int i = 0; i < WIDTH; i++) {
for (int j = 0; j < HEIGHT; j++) {
for (int l = 0; l < 8 /*sizeof(move) / sizeof(move[0]) */; ++l) {
int ti = i + move[l][0];
int tj = j + move[l][1];
if (ti < 0 || ti >= WIDTH || tj < 0 || tj >= HEIGHT) {
continue;
}
// cell (ti, tj) is valid neighbor do something with it.
}
}
}