编写程序,生成一种贯穿10 * 10字符数组(初始时全为字符'.')的“随机步法”。程序必须随机地从一个元素“走到”另一个元素。每次都是向下,向上,向左或向右移动一个元素位置。已访问过的元素按访问顺序用字母A到Z进行标记。
注意: 不能走到数组外面,不能走到已有字母标记的位置。只要有一个条件不满足,就得尝试换一个方向移动。如果4个方向都堵住了,程序就必须终止了。
利用数组和循环, 堆栈还没有学。最好是给个程序,没有的话,把算法说一下也可以
c语言编程的, 能答的进
答案:3 悬赏:20
解决时间 2021-03-21 06:08
- 提问者网友:夜微涼
- 2021-03-20 15:53
最佳答案
- 二级知识专家网友:浪女动了心
- 2021-03-20 16:53
编译环境:VC++6.0,文件名:xxx.cpp
#include <stdio.h>
#include <memory.h>
#include <stdlib.h>
#include <time.h>
void main()
{
bool df[4];
int dmove[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
bool spcBreak = false;
srand(time(NULL));
char map[10][10];
int i,j;
for (i=0;i<10;i++)
{
for (j=0;j<10;j++)
{
map[i][j] = '.';
}
}
int cx=rand()%10;
int cy=rand()%10;
char mark = 'A';
while (mark<='Z' && !spcBreak)
{
map[cx][cy] = mark;
mark++;
memset(df,0,sizeof(df));
if (cx==0)
{
df[1] = true;
}
if (cy==0)
{
df[3] = true;
}
if (cx==9)
{
df[0] = true;
}
if (cy==9)
{
df[2] = true;
}
while (1)
{
for (i=0;i<4 && df[i];i++);
if (i==4)
{
spcBreak = true;
break;
}
int dirction = rand()%4;
while (df[dirction])
dirction = rand()%4;
if (map[cx+dmove[dirction][0]][cy+dmove[dirction][1]]!='.')
{
df[dirction] = true;
}
else
{
cx = cx+dmove[dirction][0];
cy = cy+dmove[dirction][1];
break;
}
}
}
for (i=0;i<10;i++)
{
for (j=0;j<10;j++)
{
putwchar(map[i][j]);
putwchar(' ');
}
putwchar('\n');
}
}
#include <stdio.h>
#include <memory.h>
#include <stdlib.h>
#include <time.h>
void main()
{
bool df[4];
int dmove[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
bool spcBreak = false;
srand(time(NULL));
char map[10][10];
int i,j;
for (i=0;i<10;i++)
{
for (j=0;j<10;j++)
{
map[i][j] = '.';
}
}
int cx=rand()%10;
int cy=rand()%10;
char mark = 'A';
while (mark<='Z' && !spcBreak)
{
map[cx][cy] = mark;
mark++;
memset(df,0,sizeof(df));
if (cx==0)
{
df[1] = true;
}
if (cy==0)
{
df[3] = true;
}
if (cx==9)
{
df[0] = true;
}
if (cy==9)
{
df[2] = true;
}
while (1)
{
for (i=0;i<4 && df[i];i++);
if (i==4)
{
spcBreak = true;
break;
}
int dirction = rand()%4;
while (df[dirction])
dirction = rand()%4;
if (map[cx+dmove[dirction][0]][cy+dmove[dirction][1]]!='.')
{
df[dirction] = true;
}
else
{
cx = cx+dmove[dirction][0];
cy = cy+dmove[dirction][1];
break;
}
}
}
for (i=0;i<10;i++)
{
for (j=0;j<10;j++)
{
putwchar(map[i][j]);
putwchar(' ');
}
putwchar('\n');
}
}
全部回答
- 1楼网友:爱情是怎么炼成的
- 2021-03-20 19:37
寻路程序嘛。。。
利用堆栈可以完成
- 2楼网友:浪女动了心
- 2021-03-20 18:23
编译环境:VC++6.0,文件名:xxx.cpp
#include <stdio.h>
#include <memory.h>
#include <stdlib.h>
#include <time.h>
void main()
{
bool df[4];
int dmove[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
bool spcBreak = false;
srand(time(NULL));
char map[10][10];
int i,j;
for (i=0;i<10;i++)
{
for (j=0;j<10;j++)
{
map[i][j] = '.';
}
}
int cx=rand()%10;
int cy=rand()%10;
char mark = 'A';
while (mark<='Z' && !spcBreak)
{
map[cx][cy] = mark;
mark++;
memset(df,0,sizeof(df));
if (cx==0)
{
df[1] = true;
}
if (cy==0)
{
df[3] = true;
}
if (cx==9)
{
df[0] = true;
}
if (cy==9)
{
df[2] = true;
}
while (1)
{
for (i=0;i<4 && df[i];i++);
if (i==4)
{
spcBreak = true;
break;
}
int dirction = rand()%4;
while (df[dirction])
dirction = rand()%4;
if (map[cx+dmove[dirction][0]][cy+dmove[dirction][1]]!='.')
{
df[dirction] = true;
}
else
{
cx = cx+dmove[dirction][0];
cy = cy+dmove[dirction][1];
break;
}
}
}
for (i=0;i<10;i++)
{
for (j=0;j<10;j++)
{
putwchar(map[i][j]);
putwchar(' ');
}
putwchar('\n');
}
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯