god_lanbo 2019-02-26 01:50 采纳率: 0%
浏览 933

在vs2017上运行没有问题,在学校的oj上(gcc编译器)发生段错误,不知道错误点在哪里,求点明

在vs2017上运行没有问题,在学校的oj上(gcc编译器)发生段错误,下面的代码是为了解决那个Biggest Number那个题目。小白一枚,实在找不到错误点,望大佬点明。

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include<string.h>
char temp[40];
int high, wide;
void DFS(char all[][15],int x,int y,char number[],int len)
{
    number[len] = all[x][y];
    all[x][y] -= 10;
    if (x + 1 < high && all[x + 1][y] != '#'&&all[x + 1][y] > '0')
    {
        DFS(all, x + 1, y, number, len+1);
        all[x + 1][y] += 10;
    }
    if (x - 1 >= 0 && all[x - 1][y] != '#'&&all[x - 1][y] > '0')
    {
        DFS(all, x - 1, y, number, len+1);
        all[x - 1][y] += 10;
    }
    if (y + 1 < wide && all[x][y + 1] != '#'&&all[x][y + 1]>'0')
    {
        DFS(all, x, y + 1, number, len+1);
        all[x][y + 1] += 10;
    }
    if (y - 1 >= 0 && all[x][y - 1] != '#'&&all[x][y - 1] > '0')
    {
        DFS(all, x, y - 1, number, len+1);
        all[x][y - 1] += 10;
    }
    if (strlen(temp) == strlen(number))
    {
        if (strcmp(number, temp) > 0)
            strcpy(temp, number);
        number[len] = '\0';
    }
    else if (strlen(temp) < strlen(number))
    {
        strcpy(temp, number);
        number[len] = '\0';
    }
    else
        number[len] = '\0';
}
int main()
{
    char all[15][15];
    char number[40];
    for (int i = 0; i < 25; i++)
    {

        memset(all, 0, 225);
        memset(temp, 0, 40);
        scanf("%d %d", &high, &wide);
        if (high == 0 && wide == 0)
            break;
        getchar();
        getchar();
        for (int i = 0; i < high; i++)
        {
            for (int j = 0; j < wide; j++)
                all[i][j] = getchar();
            getchar();
        }
        for (int i = 0; i < high; i++)
            for (int j = 0; j < wide; j++)
                if (all[i][j] != '#')
                {
                    memset(number, 0, 40);
                    int len = 0;
                    DFS(all, i, j, number, len);
                    all[i][j] += 10;
                }
        printf("%s \n", temp);
    }
    return 0;
}
  • 写回答

1条回答

  • 丨秋水丨 2019-02-26 09:57
    关注

    应该是数组越界了

        if (x + 1 < high && all[x + 1][y] != '#'&&all[x + 1][y] > '0')
        {
            DFS(all, x + 1, y, number, len+1);
            all[x + 1][y] += 10;
        }
    
    这个判断里面没有判断y是否越界
    应该是
    if(x+1 < hight && y < wide && ....){.....}
    其他三个if也一样。
    
    
    还有第一条语句
    number[len]=all[x][y];
    执行这条语句前应该先判断x、y是否越界。
    
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)