#include <string.h>
#include <stdio.h>
#include "windows.h"
#define MAXSIZE 100
typedef struct mapdatax{
int x;
int y;
int r;
}posdata;
typedef struct{
posdata pos_lsit[MAXSIZE];
int top;
}SeqStack;
int print_error(const char *reason);
SeqStack* init_seqstack();
int empty_seqstack(SeqStack* sx);
int push_seqstack(SeqStack* sx,int x,int y,int r);
int pop_seqstack(SeqStack* sx,int* x,int* y,int* r);
void print_path(int path[],int len);
int is_touched(int map[10][10],int row,int col,int maprow,intmapcol);
int main()
{
int step=0;
int show=0;
SeqStack* mapstack=NULL;
mapstack=init_seqstack();
int path[MAXSIZE]={0};
int count=0;
int map[10][10]={
{0,0,0,0,0},
{0,0,0,1,0},
{0,0,0,0,0},
{0,1,0,1,1},
{0,0,0,0,0},
};
int isWin=0;
int sx=0,sy=1;
int now_x=0,now_y=1,now_r=1,fx=0,fy=4;
int row=5,col=5;
printf("请输入起始与终位置\n");
scanf("%d%d",&sx,&sy);
printf("其输入终止位置\n");
scanf("%d%d",&fx,&fy);
printf("开始于(%d , %d) 终止于(%d ,%d)\n",sx,sy,fx,fy);
now_x=sx,now_y=sy;
if(is_touched(map,now_x,now_y,row,col)){
print_error("起始位置无法到达\n");
return 0;
}
map[sx][sy]=-1;
while(!isWin){
//printf("(%d, %d) right: %d step%d\n",now_x,now_y,now_r,step);
if(now_x==fx&&now_y==fy)
{
if(1){
printf("路径步长:%d",step,++count);
print_path(path,step);
printf("\n");
}
if(map[now_x][now_y]==-1)
map[now_x][now_y]=0;
if(empty_seqstack(mapstack))
break;
pop_seqstack(mapstack,&now_x,&now_y,&now_r);
//pop_seqstack(mapstack,&now_x,&now_y,&now_r);
//printf("finishereturn to (%d , %d) right:%d\n",now_x,now_y,now_r);
step--;
show=!show;
continue;
}
if(now_r==5){
if(now_x==sx&&now_y==sy){
print_error("nowway can find!");
}
if(map[now_x][now_y]==-1)
map[now_x][now_y]=0;
if(empty_seqstack(mapstack))
break;
pop_seqstack(mapstack,&now_x,&now_y,&now_r);
//printf("returnto (%d , %d)now_r:%d\n",now_x,now_y,now_r);
path[--step]=0;
continue;
}
switch(now_r){
//left
case1:
if(!is_touched(map,now_x,now_y-1,row,col)){
push_seqstack(mapstack,now_x,now_y,now_r+1);
now_y--,now_r=0;
map[now_x][now_y]=-1;
path[step++]=1;
}
break;
//up
case2:
if(!is_touched(map,now_x-1,now_y,row,col)){
push_seqstack(mapstack,now_x,now_y,now_r+1);
now_x--,now_r=0;
map[now_x][now_y]=-1;
path[step++]=2;
}
break;
//right
case3:
if(!is_touched(map,now_x,now_y+1,row,col)){
push_seqstack(mapstack,now_x,now_y,now_r+1);
now_y++,now_r=0;
map[now_x][now_y]=-1;
path[step++]=3;
}
break;
case4:
//donw
if(!is_touched(map,now_x+1,now_y,row,col)){
push_seqstack(mapstack,now_x,now_y,now_r+1);
now_x++,now_r=0;
map[now_x][now_y]=-1;
path[step++]=4;
}
break;
default:
printf("error->\n");
break;
}
now_r++;
//Sleep(500);
}
return 0;
}
int print_error(const char *reason)
{
printf("error: %s\n",reason);
getchar();
return -1;
}
SeqStack* init_seqstack()
{
int i=0;
SeqStack* sx;
sx=(SeqStack*)malloc(sizeof(SeqStack));
if(sx==NULL)
print_error("bad init !");
for(i=0;i
sx->pos_lsit[i].r=1;
sx->top=-1;
return sx;
}
int empty_seqstack(SeqStack* sx)
{
if(sx->top==-1)
return 1;
else return 0;
}
int push_seqstack(SeqStack* sx,int x,int y,int r)
{
if(sx->top>=MAXSIZE-1)
return 0;
else{
sx->top++;
sx->pos_lsit[sx->top].x=x;
sx->pos_lsit[sx->top].y=y;
sx->pos_lsit[sx->top].r=r;
return 1;
}
}
int pop_seqstack(SeqStack* sx,int* x,int* y,int* r)
{
if(empty_seqstack(sx)) return 0;
else{
*x=sx->pos_lsit[sx->top].x;
*y=sx->pos_lsit[sx->top].y;
*r=sx->pos_lsit[sx->top].r;
sx->top--;
return 1;
}
}
void print_path(int path[],int len){
int i=0;
for(i=0;i
switch(path[i]){
case 1:
printf("向左");
break;
case 2:
printf("向上");
break;
case 3:
printf("向右");
break;
case 4:
printf("向下");
break;
default:
break;
}
if(i
printf("->");
}
}
int is_touched(int map[10][10],int row,int col,int maprow,intmapcol)
{
if(row<0||col<0||row>=maprow||col>=mapcol||map[row][col]==1)
return 1;
if(map[row][col]==-1)
return 2;
return0;
}