中易网

c语言编程的,能答的进

答案:3  悬赏:60  
解决时间 2021-11-05 16:36
编写程序,生成一种贯穿10 * 10字符数组(初始时全为字符'.')的“随机步法”。程序必须随机地从一个元素“走到”另一个元素。每次都是向下,向上,向左或向右移动一个元素位置。已访问过的元素按访问顺序用字母A到Z进行标记。
注意: 不能走到数组外面,不能走到已有字母标记的位置。只要有一个条件不满足,就得尝试换一个方向移动。如果4个方向都堵住了,程序就必须终止了。
利用数组和循环, 堆栈还没有学。最好是给个程序,没有的话,把算法说一下也可以
最佳答案
编译环境: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');
}
}
全部回答
寻路程序嘛。。。 利用堆栈可以完成
编译环境: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'); } }
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
襄樊高中学校那个好。
大连贷款哪家好?听说大连弘信易贷不错,谁能
因为足球是圆的,就什么事也可能发生?
汉服腰带的问题
盐城射阳李文香手机号是多少
梦幻西游全新副本怎么玩 梦幻西游全新副本玩
我什么时候才能嫁出去呀!!!
使用葡萄牙语的国家有哪些呢?
每天自己画的眉毛好难看,想去合肥韩美纹眉,
求问下面图上的电影叫什么名字
排骨炖香菇奶妈可以吃吗
床上用品网上哪里批发好?
谢谢你的上一封来信用英语怎么说
圣堂刺客的D的问题
请教大家关于淘宝代购的问题?
推荐资讯
成都彭州燕子沟要门票吗
就是说农民的土地也就可以自由出售吗
太阳晒过后为什么石头比水热
“无极”什么时候上映阿??
坐墙等红杏去17k了?在那出新书了吗?叫什么
家和万事兴歌词
长期吸入铜粉对身体有什么危害?
云南省曲靖师范学院新校区邮编是多少啊?
三星N900屏幕锁怎么解
佳能60d和600d用来拍摄像哪个更好?
实木家具的加工工艺过程包括哪些主要组成部分
五岁的男孩尿频是不是不正常?
手机登qq时,显示手机磁盘不足,清理后重新登
刺客的套装怎么选啊?