Minesweeper.arrayOOB.relevantfun.C

该文讨论了在处理二维数组时进行边界检查的重要性,以防止数组越界导致的内存访问错误。文中提供了一个游戏场景,通过mine_less和mine_lots函数更新数组元素,强调了在遍历和修改数组时需要考虑边界条件以确保程序的安全性。同时,提到了静态内存管理和全局变量在算法竞赛中的使用策略,以及如何通过优化代码来减少潜在的问题。
摘要由CSDN通过智能技术生成

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值