C语言 数组问题

qq_17562991 2017-11-20 09:03:37
一个Map[12][12]的数组传入函数中时正常,运行完本函数后内容全无



问题出在void AddImpasse(ElemType Map[][12])这个函数里,代码在下面






#define MAX_STACK_SIZE 100;
#define FALSE 1;
#define TRUE 0;
#define ERROR -1;
#include<stdio.h>
#include<stdlib.h>
#include <time.h>
int start_x = 1;
int start_y = 1;
int end_x = 10;
int end_y = 10;
typedef int ElemType;
typedef struct {
int Mx;
int My;
ElemType Data;
}Node;

typedef struct {
Node *base;
Node *top;
int stacksize;
}Stack;
/*typedef struct {
int X;
int Y;
struct LNode *next;
}LNode, *LinkList;*/

void InitMap(ElemType Map[][12])
{
for (int i = 0; i < 12; i++) {
for (int j = 0; j < 12; j++) {
Map[i][j] = 1;
}
}

Map[start_x][start_y] = 2;
Map[end_x][end_x] = 3;
}
int InitStack(Stack sta)
{
sta.base = (Node*)malloc(sizeof(Node));
if (sta.base == NULL) {
return FALSE;
}
sta.top = sta.base;
sta.stacksize = MAX_STACK_SIZE;
return TRUE;
}
void Push(Stack sta, Node p)
{
}


int InitPoint(ElemType Map[][12],ElemType Point[][3])
{
int randPointNum = rand() % 6+5;

for (int i = 0; i < randPointNum; i++) {
Point[i][0] = rand() % 10 + 1;
Point[i][1] = rand() % 10 + 1;
if (Map[Point[i][0]][Point[i][1]] == 0||Map[Point[i][0]][Point[i][1]] == 2||Map[Point[i][0]][Point[i][1]] == 3) {
Point[i][2] = 0;
}
else if (Map[Point[i][0] - 1][Point[i][1]] == 0 || Map[Point[i][0] + 1][Point[i][1]] == 0 || Map[Point[i][0]][Point[i][1] - 1] == 0 || Map[Point[i][0]][Point[i][1] + 1] == 0) {
Point[i][2] = 0;
Map[Point[i][0]][Point[i][1]] = 0;
}
else if (Map[Point[i][0] - 1][Point[i][1]] == 1 && Map[Point[i][0] +1 ][Point[i][1]] == 1 && Map[Point[i][0]][Point[i][1] - 1] == 1 && Map[Point[i][0]][Point[i][1] + 1] == 1) {
Point[i][2] = 1;
}
}

return randPointNum;
}
int ConnectJudge(ElemType Map[][12],int yNow,int xNow,int randnext)
{
switch (randnext) {
case 0: {
if (Map[yNow - 2][xNow] == 0 || Map[yNow - 1][xNow + 1] == 0 || Map[yNow - 1][xNow - 1] == 0) {
return TRUE;
}
}
break;
case 1: {
if (Map[yNow - 1][xNow + 1] == 0 || Map[yNow][xNow + 2] == 0 || Map[yNow + 1][xNow + 1] == 0) {
return TRUE;
}
}
break;
case 2: {
if (Map[yNow + 1][xNow + 1] == 0 || Map[yNow + 2][xNow] == 0 || Map[yNow + 1][xNow - 1] == 0) {
return TRUE;
}
}
break;
case 3: {
if (Map[yNow - 1][xNow - 1] == 0 || Map[yNow + 1][xNow - 1] == 0 || Map[yNow][xNow - 2] == 0) {
return TRUE;
}
}
break;
}

}
void AddRoad(ElemType Map[][12], int i, int j)
{
while (1) { //0上1右2下3左
int randnext = rand() % 4;
if (randnext == 0 && i > 1 && Map[i - 1][j] == 1) {
if (ConnectJudge(Map, i, j, randnext) == 0) {
i--;
Map[i][j] = 0;
break;
}
i--;
Map[i][j] = 0;
}
else if (randnext == 1 && j < 10 && Map[i][j + 1] == 1) {
if (ConnectJudge(Map, i, j, randnext) == 0) {
j++;
Map[i][j] = 0;
break;
}
j++;
Map[i][j] = 0;
}
else if (randnext == 2 && i < 10 && Map[i + 1][j] == 1) {
if (ConnectJudge(Map, i, j, randnext) == 0) {
i++;
Map[i][j] = 0;
break;
}
i++;
Map[i][j] = 0;
}
else if (randnext == 3 && j > 1 && Map[i][j - 1] == 1) {
if (ConnectJudge(Map, i, j, randnext) == 0) {
j--;
Map[i][j] = 0;
break;
}
j--;
Map[i][j] = 0;
}
}
}
void AddImpasse(ElemType Map[][12])
{
ElemType Point[][3]= {0};
int Num=InitPoint(Map,Point);
for (int i = 0; i < Num; i++) {
if (Point[i][2] == 1) {
AddRoad(Map,Point[i][0],Point[i][1]);
}
}
}

int RoadJudge(ElemType Map[][12], int yNow, int xNow, int Direction)
{
switch (Direction) {
case 0: {
if (Map[yNow - 2][xNow] == 0 || Map[yNow - 1][xNow + 1] == 0 || Map[yNow - 1][xNow - 1] == 0) {
return FALSE;
}
}
break;
case 1: {
if (Map[yNow - 1][xNow + 1] == 0 || Map[yNow][xNow + 2] == 0 || Map[yNow + 1][xNow + 1] == 0) {
return FALSE;
}
}
break;
case 2: {
if (Map[yNow + 1][xNow + 1] == 0 || Map[yNow + 2][xNow] == 0 || Map[yNow + 1][xNow - 1] == 0) {
return FALSE;
}
}
break;
case 3: {
if (Map[yNow - 1][xNow - 1] == 0 || Map[yNow + 1][xNow - 1] == 0 || Map[yNow][xNow - 2] == 0) {
return FALSE;
}
}
break;
}
}
int JamJudge(ElemType Map[][12], int yNow, int xNow, int Direction)
{
if (Direction == 0&&xNow <=9) {
return TRUE;
}
else if(Direction == 0&&xNow >9) {
return FALSE;
}
else if (Direction == 3&&yNow>=3) {
return TRUE;
}
else if(Direction == 3&&yNow <3){
return FALSE;
}
else if (Direction == 3&&yNow<=8) {
return TRUE;
}
else if(Direction == 3&&yNow >9){
return FALSE;
}

}
void CreateMap(ElemType Map[][12], Stack Road)
{
Node p;
int i = 1; //y
int j = 1; //x
int Direction = -1;
int Jam = 0; //卡死计数
while (Map[i][j] != 3) { //0上1右2下3左

//if (Jam > 40) {
// CreateMap(Map, Road);
// break;
//}
system("cls");
for (int testy = 0; testy < 12; testy++) {
for (int testx = 0; testx < 12; testx++) {
printf("%d ", Map[testy][testx]);
}
printf("\n");
}
int randnext = rand() % 6;
if (Map[i + 1][j] == 3) {
i++;
p.Mx = j;
p.My = i;
p.Data = 3;
Push(Road, p);
}
else if (Map[i][j + 1] == 3) {
j++;
p.Mx = j;
p.My = i;
p.Data = 3;
Push(Road, p);
}
else if (Map[i][j + 1] != 3 || Map[i + 1][j] != 3){
if (randnext == 0 && i > 1 && Map[i - 1][j] == 1 && JamJudge(Map, i, j, randnext) == 0) {
Direction = 0;
if (RoadJudge(Map, i, j, Direction) != 1) {
i--;
Map[i][j] = 0;
p.Mx = j;
p.My = i;
p.Data = 0;
Push(Road, p);
}
}
else if ((randnext == 1 || randnext == 4) && j < 10 && Map[i][j + 1] == 1) {
Direction = 1;
if (RoadJudge(Map, i, j, Direction) != 1) {
j++;
Map[i][j] = 0;
p.Mx = j;
p.My = i;
p.Data = 0;
Push(Road, p);
}
}
else if ((randnext == 2 || randnext == 5) && i < 10 && Map[i + 1][j] == 1) {
Direction = 2;
if (RoadJudge(Map, i, j, Direction) != 1) {
i++;
Map[i][j] = 0;
p.Mx = j;
p.My = i;
p.Data = 0;
Push(Road, p);
}
}
else if (randnext == 3 && j > 1 && Map[i][j - 1] == 1 && JamJudge(Map, i, j, randnext) == 0) {
Direction = 3;
if (RoadJudge(Map, i, j, Direction) != 1) {
j--;
Map[i][j] = 0;
p.Mx = j;
p.My = i;
p.Data = 0;
Push(Road, p);
}
}
//else {
// Jam++;
// printf("%d\n", Jam);
//}
}
}
AddImpasse(Map);
system("cls");
for (int testy = 0; testy < 12; testy++) {
for (int testx = 0; testx < 12; testx++) {
printf("%d ", Map[testy][testx]);
}
printf("\n");
}
}

void main()
{
Stack Road;
Road.stacksize = 1;
srand((unsigned)time(NULL));
ElemType Map[12][12];
InitStack(Road);
InitMap(Map);
CreateMap(Map, Road);
system("pause");
}
...全文
123 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2017-11-21
  • 打赏
  • 举报
回复
http://edu.csdn.net/course/detail/2516 C语言指针与汇编内存地址-三.数组和二维数组
自信男孩 2017-11-21
  • 打赏
  • 举报
回复
#define MAX_STACK_SIZE 100;
#define FALSE 1;
#define TRUE 0;
#define ERROR -1;
第一个问题,宏定义需要都去掉分号;
int InitStack(Stack *sta)
{
    sta->base = (Node*)malloc(sizeof(Node) * MAX_STACK_SIZE);
    if (sta->base == NULL) {
        return FALSE;
    }
    sta->top = sta->base;
    sta->stacksize = MAX_STACK_SIZE;
    return TRUE;
}
第二个问题,应该传地址到InitStack函数,这样才能获取申请的空间; 并且申请的大小应该乘以MAX_STACK_SIZE;
FoolCarpe 2017-11-20
  • 打赏
  • 举报
回复
ElemType Point[][3]= {0}; 等价于 Point[1][3] = {0}; InitPoint 会出现越界访问数组Point 可能原本的想法是 ElemType Point[12][3] = {0};
qq_17562991 2017-11-20
  • 打赏
  • 举报
回复


69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧