标题:扫雷的搜索步骤的函数出现溢出,找不到原因,求助!
只看楼主
好烦、
Rank: 2
等 级:论坛游民
帖 子:78
专家分:72
注 册:2020-10-10
结帖率:87.5%
 问题点数:0 回复次数:1 
扫雷的搜索步骤的函数出现溢出,找不到原因,求助!
对于一个二维数组(第三维可忽略不看),对输入的点的周围进行判断,此代码中全为非雷,会全部递归搜索,为何溢出?

#include <conio.h>
#include<stdio.h>

void scanaround(int land[15][15][3], int i, int j)
{
    int q,w,e,a,d,z,x,c;//描述周围的点有无雷
    int k,t = 0;
    q=w=e=a=d=z=x=c=0;
    if (i == 0)
    {
        if (j == 0)
        {
            //扫描右面
            for (k = 0; k < 2; k++)
            {
                if (land[i + k][j + 1][0] == 0 )                //地面
                {
                    if(k == 0)
                        d = 1;
                    else
                        c = 1;
                }
                else if (land[i + k][j + 1][0] == 1)            //地雷
                    t++;   
            }
            //扫描下面
            if (land[i + 1][j][0] == 0)
                x = 1;
            else if (land[i + 1][j][0] == 1)
                t++;
        }
        else if (j == 14)
        {
            //扫描左面
            for (k = 0; k < 2; k++)
            {
                if (land[i + k][j - 1][0] == 0)            //地面
                {
                    if(k == 0)
                        a = 1;
                    else
                        z = 1;
                }
                else if (land[i + k][j - 1][0] == 1)
                    t++;
            }
            //扫描下面
            if (land[i + 1][j][0] == 0)
                x = 1;
            else if (land[i + 1][j][0] == 1)
                t++;
        }
        else
        {
            //扫描右面
            for (k = 0; k < 2; k++)
            {
                if (land[i + k][j + 1][0] == 0)            //地面
                {
                    if(k == 0)
                        d = 1;
                    else
                        c = 1;
                }
                else if (land[i + k][j + 1][0] == 1)
                    t++;
            }
            //扫描左面
            for (k = 0; k < 2; k++)
            {
                if (land[i + k][j - 1][0] == 0)            //地面
                {
                    if(k == 0)
                        a = 1;
                    else
                        z = 1;
                }
                else if (land[i + k][j - 1][0] == 1)
                    t++;
            }
            //扫描下面
            if (land[i + 1][j][0] == 0)
                x = 1;
            else if (land[i + 1][j][0] == 1)
                t++;
        }
    }
    else if (i == 14)
    {
        if (j == 0)
        {
            //扫描右面
            for (k = 0; k < 2; k++)
            {
                if (land[i + k - 1][j + 1][0] == 0)            //地面
                {
                    if(k == 0)
                        e = 1;
                    else
                        d = 1;
                }
                else if (land[i + k - 1][j + 1][0] == 1)
                    t++;
            }
            //扫描上面
            if (land[i - 1][j][0] == 0)
                w = 1;
            else if (land[i - 1][j][0] == 1)
                t++;
        }
        else if (j == 14)
        {
            //扫描左面
            for (k = 0; k < 2; k++)
            {
                if (land[i + k - 1][j - 1][0] == 0)            //地面
                {
                    if(k == 0)
                        q = 1;
                    else
                        a = 1;
                }
                else if (land[i + k - 1][j - 1][0] == 1)
                    t++;
            }
            //扫描上面
            if (land[i - 1][j][0] == 0)
                w = 1;
            else if (land[i - 1][j][0] == 1)
                t++;
        }
        else
        {
            //扫描右面
            for (k = 0; k < 2; k++)
            {
                if (land[i + k - 1][j + 1][0] == 0)            //地面
                {
                    if(k == 0)
                        e = 1;
                    else
                        d = 1;
                }
                else if (land[i + k - 1][j + 1][0] == 1)
                    t++;
            }
            //扫描左面
            for (k = 0; k < 2; k++)
            {
                if (land[i + k - 1][j - 1][0] == 0)            //地面
                {
                    if(k == 0)
                        q = 1;
                    else
                        a = 1;
                }
                else if (land[i + k - 1][j - 1][0] == 1)
                    t++;
            }
            //扫描上面
            if (land[i - 1][j][0] == 0)
                w = 1;
            else if (land[i - 1][j][0] == 1)
                t++;
        }
    }
    else if (i != 0 && i != 14)
    {
        if (j == 0)
        {
            //扫描右面
            for (k = 0; k < 3; k++)
            {
                if (land[i - 1 + k][j + 1][0] == 0)
                {
                    if(k == 0)
                        e = 1;
                    else if(k == 1)
                        d = 1;
                    else if(k == 2)
                        c = 1;
                }
                else if (land[i - 1 + k][j + 1][0] == 1)
                    t++;
            }
            //扫描上下
            if (land[i - 1][j][0] == 0)
                w = 1;
            else if (land[i - 1][j][0] == 1)
                t++;
            if (land[i + 1][j][0] == 0)
                x = 1;
            else if (land[i + 1][j][0] == 1)
                t++;
        }
        else if (j == 14)
        {
            //扫描左面
            for (k = 0; k < 3; k++)
            {
                if (land[i - 1 + k][j - 1][0] == 0)
                {
                    if(k == 0)
                        q = 1;
                    else if(k == 1)
                        a = 1;
                    else if(k == 2)
                        z = 1;
                }
                else if (land[i - 1 + k][j - 1][0] == 1)
                    t++;
            }
            //扫描上下
            if (land[i - 1][j][0] == 0)
                w = 1;
            else if (land[i - 1][j][0] == 1)
                t++;
            if (land[i + 1][j][0] == 0)
                x = 1;
            else if (land[i + 1][j][0] == 1)
                t++;
        }
        else
        {
            //扫描上面
            for (k = 0; k < 3; k++)
            {
                if (land[i - 1][j + k - 1][0] == 0)
                {
                    if(k == 0)
                        q = 1;
                    else if(k == 1)
                        w = 1;
                    else if(k == 2)
                        e = 1;
                }
                else if (land[i - 1][j + k - 1][0] == 1)
                    t++;
            }
            //扫描下面
            for (k = 0; k < 3; k++)
            {
                if (land[i + 1][j + k - 1][0] == 0)
                {
                    if(k == 0)
                        z = 1;
                    else if(k == 1)
                        x = 1;
                    else if(k == 2)
                        c = 1;
                }
                else if (land[i + 1][j + k - 1][0] == 1)
                    t++;
            }
            //扫描左右
            if (land[i][j - 1][0] == 0)
                a = 1;
            else if (land[i][j - 1][0] == 1)
                t++;
            if (land[i][j + 1][0] == 0)
                d = 1;
            else if (land[i][j + 1][0] == 1)
                t++;
        }
    }
    if(t == 0)
    {
        if(q == 1)
            scanaround(land,i-1,j-1);
        if(w == 1)
            scanaround(land,i-1,j);
        if(e == 1)
            scanaround(land,i-1,j+1);
        if(a == 1)
            scanaround(land,i,j-1);
        if(d == 1)
            scanaround(land,i,j+1);
        if(z == 1)
            scanaround(land,i+1,j-1);
        if(x == 1)
            scanaround(land,i+1,j);
        if(c == 1)
            scanaround(land,i+1,j+1);
    }
    land[i][j][0] = 3;//本质状态改为揭开
    land[i][j][1] = 10 + t;    //显示的要输出的数
    land[i][j][2] = 0;    //不在此状态
}
int main(void)
{    int i,j;
    int land[15][15][3];
    for(int i=0;i<15;i++)
    {
        for(int j=0;j<15;j++)
        {
            land[i][j][0] = 0;
        }
    }
    scanf("%d%d",&i,&j);
    scanaround(land,i,j);
    printf("po");
    return 0;
}
搜索更多相关主题的帖子: 溢出 for if 扫描 int 
2021-01-01 21:41
好烦、
Rank: 2
等 级:论坛游民
帖 子:78
专家分:72
注 册:2020-10-10
得分:0 
会不会是开的局部变量太多了呢
2021-01-01 21:49



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-504477-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.083598 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved