| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 734 人关注过本帖
标题:要交作业了啊请帮下忙吧
只看楼主 加入收藏
gaga
Rank: 1
等 级:新手上路
威 望:2
帖 子:307
专家分:0
注 册:2006-4-5
收藏
 问题点数:0 回复次数:4 
要交作业了啊请帮下忙吧
有没有迷宫问题的源代码啊
我说的是用C编写的
而且是可以运行的
已经调试好的
我在此谢谢了啊
搜索更多相关主题的帖子: 作业 
2006-05-02 21:37
findlyhl
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2006-5-6
收藏
得分:0 

看看这个行吗?
#include <stdio.h>
#include <stdlib.h>
#include "random.h"
#include "random.c"

typedef struct node{
int x_pos;
int y_pos;
int dir;
int mark;
struct node *next;
}*NODE;
typedef struct stack{
NODE head;
NODE tail;
}STACK;

int x_start, y_start, x_end, y_end;

void init_stack(STACK *stack);
void push(STACK *stack, NODE node);
NODE pop(STACK *stack);
void maze_create(int n);
void out_way_print(STACK stack, int n);

int main()
{
int n_dimension;
STACK valid_path_stack;
init_stack(&valid_path_stack);

printf ("please input the dimensions of the maze: ");
scanf ("%d", &n_dimension);
maze_create (n_dimension);

printf("\nNow computer will find if there is a path to the exit!\n");
out_way_print(valid_path_stack, n_dimension);
}

void init_stack(STACK *stack)
{
stack->head = stack->tail = NULL;
}

void push(STACK *stack, NODE node)
{
if (stack->tail == NULL) {
stack->head = stack->tail = node;
}else {
stack->head->next = node;
stack->head = node;
}
}

NODE pop(STACK *stack)
{
NODE temp;
temp = stack->tail;
if (stack->tail == stack->head) {
free(stack->head);
printf("\nthe stack is empty!\n");
return NULL;
} else {
while (temp->next != stack->head) {
temp = temp->next;
}
printf("%d %d",stack->head->x_pos,stack->head->y_pos);
free(stack->head);
stack->head = temp;
stack->head->next = NULL;
return temp;
}
}

void maze_create(int n)
{
int i, j, matrix[n][n];
FILE *fp;
if ((fp = fopen("1.dat", "w+")) == NULL) {
printf("Create the file error!\n");
exit(0);
}
Randomize();
for (i = 0; i < n; i++) {
matrix[0][i] = 1; /*修改成memset库函数*/
}
for (i = 1; i < n-1; i++) {
matrix[i][0] = matrix[i][n-1] = 1;
for (j = 1; j < n-1; j++) {
matrix[i][j] = RandomInteger(0,1);
}
}
for (i = 0; i < n; i++) {
matrix[n-1][i] = 1; /*修改成memset库函数*/
}

printf("\nPlease input the position of entrance as follow x,y: ");
scanf("%d,%d", &x_start, &y_start);
matrix[x_start-1][y_start-1] = 0;
printf("\nPlease input the position of exit as follow x,y: ");
scanf("%d,%d", &x_end, &y_end);
matrix[x_end-1][y_end-1] = 0;

for(i = 0; i < n; i++){
for(j = 0; j < n; j++){
printf(" %d", matrix[i][j]);
fprintf(fp, " %d",matrix[i][j]);
}
putchar('\n');
fprintf(fp, "\n");
}
if(fclose(fp)) {
printf("File close error!\n");
exit(0);
}
}

void out_way_print(STACK stack, int n)
{
int i, j, x, y;
long temp;
NODE walker,p_temp;
char matrix[n][n];
temp = 5L; /*用以隔5个字节的去取数*/
FILE *fp;
if((fp = fopen("1.dat", "r+")) == NULL) {
printf("Open the file error!\n");
exit(0);
}

/*从1.dat中读出了迷宫的原貌并且存放到了matrix二维数组中*/
for(i = 0; i < n; i++) {
for(j = 0; j < n; j++) {
fseek(fp, temp, 0);
printf(" ");
putchar(matrix[i][j] = fgetc(fp));
temp += 6L;
}
temp -= 5L;
fseek(fp, temp, 0);
putchar(fgetc(fp));
temp += 6L;
}
/*改变方向寻找路径*/
walker = (NODE)malloc(sizeof(struct node));
walker->x_pos = x_start;
walker->y_pos = y_start;
walker->dir = 1;
walker->mark = 1;
walker->next = NULL;
push(&stack, walker);

while (walker->x_pos != x_end || walker->y_pos != y_end) {

switch(walker->dir){
case 1:
if (matrix[walker->x_pos-1][walker->y_pos] == '0') {
x = walker->x_pos;
y = walker->y_pos+1;
walker->dir++;
p_temp = (NODE)malloc(sizeof(struct node));
walker->next = p_temp;
walker = p_temp;
walker->x_pos = x;
walker->y_pos = y;
walker->dir = 1;
walker->mark = 1;
walker->next = NULL;
push(&stack, walker);
break;
}else {
walker->dir++;
break;
}
break;
case 2:
if (matrix[walker->x_pos][walker->y_pos-1] == '0') {
x = walker->x_pos+1;
y = walker->y_pos;
walker->dir++;
p_temp = (NODE)malloc(sizeof(struct node));
walker ->next = p_temp;
walker = p_temp;
walker->x_pos = x;
walker->y_pos = y;
walker->dir = 1;
walker->mark = 1;
walker->next = NULL;
push(&stack, walker);
break;
}else {
walker->dir++;
break;
}
break;
case 3:
if (walker->y_pos-2>=0 &&
matrix[walker->x_pos-1][walker->y_pos-2] == '0' &&
walker->mark != 1) {
x = walker->x_pos;
y = walker->y_pos-1;
walker->dir++;
p_temp = (NODE)malloc(sizeof(struct node));
walker ->next = p_temp;
walker = p_temp;
walker->x_pos = x;
walker->y_pos = y;
walker->dir = 1;
walker->mark = 1;
walker->next = NULL;
push(&stack, walker);
break;
}else {
walker->dir++;
break;
}
break;
case 4:
if (walker->x_pos-2>=0 &&
matrix[walker->x_pos-2][walker->y_pos-1] == '0' &&
walker->mark != 1) {
x = walker->x_pos-1;
y = walker->y_pos;
walker->dir++;
p_temp = (NODE)malloc(sizeof(struct node));
walker ->next = p_temp;
walker = p_temp;
walker->x_pos = x;
walker->y_pos = y;
walker->dir = 1;
walker->mark = 1;
walker->next = NULL;
push(&stack, walker);
break;
} else if((walker = pop(&stack)) != NULL) {
break;
} else {
printf("\nThere is no way to the exit!!!");
exit(0);
}
break;
default:
if((walker = pop(&stack)) != NULL) {
break;
}else {
printf("\nThere is no way to the exit!!!");
exit(0);
}
break;
}
}
/*打印出来,只是缺少添加到.dat文档的代码*/
if(walker->x_pos == x_end && walker->y_pos == y_end) {
printf("There is a path exit in the maze!\n");
walker = stack.tail;
do{
printf("(%d , %d)->", walker->x_pos, walker->y_pos);
walker = stack.tail->next;
free(stack.tail);
stack.tail = walker;
}while (walker != stack.head);
printf("(%d , %d)", walker->x_pos, walker->y_pos);
free(walker);
}
if(fclose(fp)) {
printf("\nClose file error!\n");
exit(0);
}
}

2006-05-06 17:02
gaga
Rank: 1
等 级:新手上路
威 望:2
帖 子:307
专家分:0
注 册:2006-4-5
收藏
得分:0 
GOOD

明天的明天还有明天。 可是今天却只有一个。 public Copy from 无缘今生
2006-05-06 22:54
冬日阳光
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2006-5-9
收藏
得分:0 
真是谢谢,我们也要这个作业~~
2006-05-09 12:27
gaga
Rank: 1
等 级:新手上路
威 望:2
帖 子:307
专家分:0
注 册:2006-4-5
收藏
得分:0 

我看了看
不行啊
你写的那个#include "random.h"
#include "random.c"
我从来没有听说过啊


明天的明天还有明天。 可是今天却只有一个。 public Copy from 无缘今生
2006-05-09 13:39
快速回复:要交作业了啊请帮下忙吧
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016353 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved