在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;
}