Approximate title: Print the number of mines around the corresponding grid, pay attention to the output format .
Complete enough code:
char land[100][100];
void mine_less(int x, int y) {
for (int i = x; i <= x + 2; i++)
for (int j = y; j <= y + 2; j++)
if (land[i][j] != '*') land[i][j]++;
}
void mine_lots(int x, int y) {
for (int i = x; i <= x + 2; i++)
for (int j = y; j <= y + 2; j++)
if (land[i][j] == '*') land[x + 1][y + 1]++;
}
int num_mine, num_safe;
int main() {
int xin, yin, ordinal = 1;
while (~scanf("%d%d", &xin, &yin) && !(xin == 0 || yin == 0)) {
num_mine = 0;
num_safe = 0;
for (int i = 0; i < xin; i++) {
getchar();
for (int j = 0; j < yin; j++) {
scanf("%c", &land[i][j]);
if (land[i][j] == '.') {
land[i][j] = '0';
num_safe++;
} else
num_mine++;
}
}
if (num_mine < num_safe)
{
for (int i = 0; i < xin; i++)
for (int j = 0; j < yin; j++)
if (land[i][j] == '*')
mine_less(i - 1, j - 1);
} else {
for (int i = 0; i < xin; i++)
for (int j = 0; j < yin; j++)
if (land[i][j] != '*')
mine_lots(i - 1, j - 1);
}
printf("Field #%d:\n", ordinal);
for (int i = 0; i < xin; i++)
for (int j = 0; j < yin; j++) {
printf("%c", land[i][j]);
if (!((j + 1) % yin))
printf("\n");
}
printf("\n");
ordinal++;
}
}
but .The above is not safe enough:
We should consider array out-of-bounds:
Out of bounds: An access violation occurred while reading the location. It would be very bad at the console "0xC0000005" You never know the consequences
So the true_core demining function is as follows:
for (int i = 0; i < xin; i++) for (int j = 0; j < yin; j++) if (land[i][j] == '*') mine_less(i - 1, j - 1); for (int i = 0; i < xin; i++) for (int j = 0; j < yin; j++) if (land[i][j] != '*') mine_lots(i - 1, j - 1); First, let's see when the demining function is called. They behave similarly, deciding whether to start a function to increase the value of the surrounding coordinates when traversing a two-dimensional array based on the state of the coordinates they are in. One mine is more, one mine is less, it is just used to save running time To prevent overflow, we add a condition:i >= 0 && i < Xin && j >= 0 && j < Yin OPS, another problem arises...Xin?Yin? meet'*',arounall'0'+1; void mine_less(int x, int y) { for (int i = x; i <= x + 2; i++) for (int j = y; j <= y + 2; j++) if (condition && land[i][j] != '*') land[i][j]++; } meet'0',thecentral'0'+1; void mine_lots(int x, int y) { for (int i = x; i <= x + 2; i++) for (int j = y; j <= y + 2; j++) if (condition && land[i][j] == '*') land[x + 1][y + 1]++; }
Just in the question, global variables have the necessary impact. Let's see who must put the code in front of everything:
char land[100][100];
int xin, yin;1.In the algorithm competition, static memory such as large arrays is recommended to be opened in global variables, of course, remember to initialize in the data loop.
Global variables are stored in heap memory, variables in functions are stored in stack memory, the size of heap memory is generally much larger than stack memory, and opening a large array in a function is easy to burst memory.
2.They are also needed in addition to the main function
Thanks to the solver Manchester.Here's one of mine, youth edition code:
char land[100][100];
int xin, yin;
void mine_less(int x, int y) {
for (int i = x; i <= x + 2; i++)
for (int j = y; j <= y + 2; j++)
if (i >= 0 && i < xin && j >= 0 && j < yin && land[i][j] != '*') land[i][j]++;
}
int main() {
int ordinal = 1;
while (~scanf("%d%d", &xin, &yin) && !(xin == 0 || yin == 0)) {
for (int i = 0; i < xin; i++) {
getchar();
for (int j = 0; j < yin; j++) {
scanf("%c", &land[i][j]);
if (land[i][j] == '.')
land[i][j] = '0';
}
}
for (int i = 0; i < xin; i++)
for (int j = 0; j < yin; j++)
if (land[i][j] == '*')
mine_less(i - 1, j - 1);
printf("Field #%d:\n", ordinal);
for (int i = 0; i < xin; i++)
for (int j = 0; j < yin; j++) {
printf("%c", land[i][j]);
if (!((j + 1) % yin))
printf("\n");
}
printf("\n");
ordinal++;
}
}
...Finally, when there are consecutive scf%c reads, take care to empty the buffer data.