求一个c语言编的迷宫代码
求一个c语言编的迷宫代码
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"
#define MAP_SIZE 50 //迷宫的大小
struct node {
char dir; //方向标记
struct list_head list;
};
/***************************
* 数据文件格式:
* 文件的第一行应该给出迷宫的宽和高n,m
* 接下来的m行表示了迷宫的形态
* "#"表示障碍物,"."表示可通过区域,"S"表示起点,"E"表示出口
* 如果存在起点到出口的路径,输出用"*"表示路线
***************************/
int main(int argc, char* argv[])
{
struct node trace, *tmp;
struct list_head *ltmp, *q;
FILE *fp;
char filename[255];
char pos[4][2] = {{0,1},{1,0},{-1,0},{0,-1}}; //方向向量
char map[MAP_SIZE][MAP_SIZE];
char found, old;
int count, width, height;
int i, j, x, y;
if(argc==1) {
printf("please specify the data file: ");
scanf("%s", filename);
}
else
strcpy(filename, argv[1]);
fp = fopen(filename, "r");
if(!fp) {
fprintf(stderr, "open file %s failed.\n", filename);
return 1;
}
memset(map, 0, sizeof(char)*MAP_SIZE*MAP_SIZE);
INIT_LIST_HEAD(&trace.list);
trace.dir = -1;
fscanf(fp, "%d%d", &width, &height);
for(i=0; i<height; ++i) {
getc(fp);
for(j=0; j<width; ++j) {
map[i][j] = getc(fp);
if(map[i][j] == 'S') {
x = i;
y = j;
}
}
}
fclose(fp);
count = 0;
found = 0;
i = 0;
old = 0;
while(!found){
tmp = list_entry(trace.list.prev, struct node, list);
for(; i<4; ++i){
if(i != 3 - tmp->dir &&
0<=x+pos[i][0] && x+pos[i][0]<height &&
0<=y+pos[i][1] && y+pos[i][1]<width &&
map[x+pos[i][0]][y+pos[i][1]] != '#' &&
map[x+pos[i][0]][y+pos[i][1]] != '@') {
x += pos[i][0];
y += pos[i][1];
if(!old) {
tmp = (struct node*)malloc(sizeof(struct node));
list_add_tail(&(tmp->list), &(trace.list));
}
tmp->dir = i;
++count;
if(map[x][y] == 'E')
found = 1;
map[x][y] = '@';
break;
}
}
if(i == 4) {
if(list_empty(&trace.list)) {
found = 1;
}
else {
x -= pos[tmp->dir][0];
y -= pos[tmp->dir][1];
list_del(trace.list.prev);
free(tmp);
--count;
i = (list_entry(trace.list.prev, struct node, list) -> dir) + 1;
old = 1;
}
}
else {
i = 0;
old = 0;
}
}
if(i==4) {
printf("no way to get the exit.\n");
}
else {
for(i=0; i<height; ++i)
for(j=0; j<width; ++j)
if(map[i][j]=='@')
map[i][j] = '.';
list_for_each_prev(ltmp, &trace.list) {
tmp = list_entry(ltmp, struct node, list);
map[x][y] = '*';
x -= pos[tmp->dir][0];
y -= pos[tmp->dir][1];
}
map[x][y] = '*';
printf("found it\n");
for(i=0; i<height; ++i) {
for(j=0; j<width; ++j)
printf("%c", map[i][j]);
printf("\n");
}
}
list_for_each_safe(ltmp, q, &trace.list){
tmp= list_entry(ltmp, struct node, list);
list_del(ltmp);
free(tmp);
}
return 0;
}
程序代码:这是我以前刚学不久写的迷宫!可能很粗糙!!!
/*
时间:time 迷宫:labyrinth 初始:initial 地点:location 执行:execute 人:person
上:up 下:down 左:left 右:right 终点:destination 空:empty 横:horizontal
竖:vertical 长:length 宽:width 坐标:coordinate
*/
#include<stdio.h>
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define ESC 0x011b
/*--------------------------迷宫地图-------------------------*/
int horizontal=1,vertical=0;
;
int length=20,width=70;
int person=2,map[20][70]={
{'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'},
{ 2 ,' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#','#'},
{'#',' ','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#',' ','#',' ','#','#','#','#','#','#','#',' ','#',' ','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#',' ','#'},
{'#',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ','#','#',' ',' ',' ',' ','#',' ','#',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#','#','#',' ','#'},
{'#',' ','#',' ','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#',' ','#','#',' ','#',' ','#','#',' ',' ',' ','#',' ','#','#','#','#','#','#','#','#','#','#','#','#',' ','#','#','#','#','#','#','#','#','#','#','#','#','#',' ','#','#','#',' ','#'},
{'#',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#','#',' ','#',' ','#','#','#','#','#','#',' ','#','#',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ','#',' ','#'},
{'#',' ','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#',' ','#',' ',' ',' ',' ',' ',' ',' ',' ','#','#',' ','#','#','#','#','#','#','#',' ','#','#','#','#','#','#','#','#','#','#','#','#','#',' ','#','#','#','#','#',' ','#'},
{'#',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ','#',' ','#','#','#','#','#','#','#',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ','#',' ',' ',' ','#',' ','#'},
{'#',' ','#',' ','#',' ','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#',' ','#',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ','#','#','#',' ','#','#','#','#','#','#','#',' ','#','#','#','#','#','#','#','#','#',' ','#',' ','#',' ','#',' ','#',' ','#'},
{'#',' ','#',' ','#',' ','#',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ','#',' ','#','#','#','#','#','#','#','#','#','#','#','#','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ','#','#','#',' ','#'},
{'#',' ','#',' ','#',' ',' ',' ','#',' ','#','#','#',' ','#',' ','#','#','#','#','#','#','#','#',' ','#',' ',' ',' ','#','#','#',' ',' ',' ','#',' ',' ',' ',' ',' ','#','#','#','#','#',' ','#','#','#','#','#','#','#','#','#','#','#','#',' ',' ','#','#','#','#','#','#','#',' ','#'},
{'#',' ','#',' ','#','#','#','#','#','#','#','#',' ',' ','#',' ','#',' ',' ',' ',' ','#','#','#',' ','#','#','#','#','#','#','#',' ','#',' ','#',' ','#','#','#',' ','#',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#','#','#',' ','#'},
{'#',' ','#',' ','#',' ',' ',' ',' ',' ',' ','#',' ','#','#',' ','#',' ','#','#',' ','#','#','#',' ',' ',' ',' ',' ','#','#','#',' ','#',' ',' ',' ','#',' ','#',' ','#',' ','#',' ','#',' ','#','#','#','#','#','#','#','#','#','#','#','#',' ','#','#','#','#',' ',' ',' ','#',' ','#'},
{'#',' ','#',' ','#',' ','#','#','#','#',' ','#',' ',' ',' ',' ','#',' ',' ','#',' ','#','#','#','#','#','#','#','#',' ',' ',' ',' ','#','#','#','#',' ',' ','#',' ','#',' ','#',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#','#','#','#','#','#','#','#',' ','#'},
{'#',' ','#',' ','#',' ',' ',' ',' ','#',' ','#','#','#','#','#','#','#',' ','#',' ','#','#','#','#','#','#','#','#',' ','#','#',' ','#','#','#','#',' ','#','#',' ','#',' ','#',' ','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#',' ',' ',' ',' ',' ','#',' ','#'},
{'#',' ','#',' ','#','#','#','#',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#','#',' ',' ',' ',' ',' ',' ','#','#',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#','#','#','#','#',' ','#'},
{'#',' ','#',' ',' ',' ',' ',' ',' ','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#',' ',' ',' ',' ','#',' ','#'},
{'#',' ','#','#','#','#','#','#','#',' ',' ',' ','#',' ',' ',' ','#',' ',' ',' ','#',' ',' ',' ','#',' ',' ',' ',' ','#',' ',' ',' ','#','#','#','#','#','#','#','#',' ',' ',' ','#',' ',' ',' ','#',' ',' ',' ','#','#','#',' ',' ',' ','#','#','#','#','#',' ','#','#',' ','#',' ','#'},
{'#',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ','#',' ',' ',' ','#',' ',' ',' ','#',' ',' ',' ','#','#',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ','#',' ',' ',' ','#',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','*'},
{'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'}
};
/*------------------------------------------------------------*/
void execute();/*执行函数声明*/
void maps(); /*地图函数声明*/
void interface_main();/*主界面*/
void interface_options();/*胜利语*/
void interface_last(); /*退出语*/
void up(); /*向上执行函数声明*/
void down(); /*向下执行函数声明*/
void left(); /*向左执行函数声明*/
void right(); /*向右执行函数声明*/
void soul(); /*灵魂函数声明*/
void main() /*主函数*/
{
/* interface_main();
maps();
*/
execute(); /*执行方向操作函数调用*/
maps();
interface_last(); /*退出语*/
getch();
}
void interface_main() /*主界面函数*/
{
printf("--------------wlecome use----------------\n");
printf(" time 09.07.20 \n");
printf(" author snoopy \n");
printf("ESC(shut)\n");
}
void interface_options() /*胜利语*/
{
printf("congratulate pass a test ! ! ! ^-^");
getch();
exit(0);
}
void interface_last() /*退出语*/
{
printf("good bay ! ! ! ^-^");
}
void execute() /*方向执行函数*/
{
int key;
do
{
system("cls");
interface_main();
maps();
key=bioskey(0);
system("cls");
interface_main();
if(key==UP||key=='8') /*当按上按键时执行返回值为1*/
{
up(); /*调用上方向执行函数*/
}
if(key==DOWN)
{
down();
}
if(key==LEFT)
{
left();
}
if(key==RIGHT)
{
right();
}
soul(); /*调用灵魂函数*/
}while(key!=ESC);
}
void maps() /*迷宫输出函数*/
{
int i,j;
for(i=0;i<length;i++)
{
printf(" ");
for(j=0;j<width;j++)
{
printf("%c",map[i][j]);
}
printf("\n");
}
}
void up() /*上方向执行函数*/
{
int t;
if(map[horizontal-1][vertical]=='*') /*当即将要走到得地方为‘*‘时则返回1*/
{
map[horizontal-1][vertical]=map[horizontal][vertical];
map[horizontal][vertical]=' ';
maps();
interface_options();
}
if(map[horizontal-1][vertical]==' ')
{
horizontal=horizontal-1;
t=map[horizontal][vertical];
map[horizontal][vertical]=map[horizontal+1][vertical];
map[horizontal+1][vertical]=t;
}
maps();
}
void down() /*下方向执行函数*/
{
int t;
if(map[horizontal+1][vertical]=='*')
{
map[horizontal+1][vertical]=map[horizontal][vertical];
map[horizontal][vertical]=' ';
maps();
interface_options();
}
if(map[horizontal+1][vertical]==' ')
{
horizontal=horizontal+1;
t=map[horizontal][vertical];
map[horizontal][vertical]=map[horizontal-1][vertical];
map[horizontal-1][vertical]=t;
}
maps();
}
void left() /*左方向执行函数*/
{
int t;
if(map[horizontal][vertical-1]=='*')
{
map[horizontal][vertical-1]=map[horizontal][vertical];
map[horizontal][vertical]=' ';
maps();
interface_options();
}
if(map[horizontal][vertical-1]==' ')
{
vertical=vertical-1;
t=map[horizontal][vertical];
map[horizontal][vertical]=map[horizontal][vertical+1];
map[horizontal][vertical+1]=t;
}
maps();
}
void right() /*右方向执行函数*/
{
int t;
if(map[horizontal][vertical+1]=='*')
{
map[horizontal][vertical+1]=map[horizontal][vertical];
map[horizontal][vertical]=' ';
maps();
interface_options();
}
if(map[horizontal][vertical+1]==' ')
{
vertical=vertical+1;
t=map[horizontal][vertical];
map[horizontal][vertical]=map[horizontal][vertical-1];
map[horizontal][vertical-1]=t;
}
maps();
}
void soul()
{
if(map[18][63]==2)
{
map[18][67]='#';
}
if(map[18][65]==2)
{
map[18][64]='#';
map[1][68]=' ';
}
if(map[18][58]==2)
{
map[18][67]=' ';
map[18][64]=' ';
}
if(map[16][68]==2)
{
map[15][68]='#';
map[4][68]='#';
}
if(map[18][68]==2)
{
map[18][69]='#';
map[5][69]='*';
map[5][67]=' ';
map[11][2]=' ';
}
if(map[5][65]==2)
{
map[5][67]='#';
map[9][67]=' ';
map[9][66]=' ';
}
if(map[9][66]==2)
{
map[9][67]='#';
map[14][67]=' ';
}
if(map[15][28]==2)
{
map[14][29]='#';
map[12][29]=' ';
}
if(map[12][36]==2)
{
map[10][36]='#';
map[11][38]=' ';
}
if(map[14][67]==2)
{
map[12][67]=' ';
map[13][68]='#';
}
if(map[7][59]==2)
{
map[7][60]='#';
}
if(map[9][58]==2)
{
map[9][59]='#';
}
if(map[13][58]==2)
{
map[12][59]='#';
}
if(map[12][67]==2)
{
map[5][69]='#';
map[1][0]='*';
}
}