# 迷宫小程序

#include "stdafx.h"
#include "iostream"
#include "malloc.h"
using namespace std;
#define row 7
#define column 6
struct node
{
int x;
int y;
int d;
};
typedef struct node_1
{
node move[20];
int top;
}Stack;

void init_stack(Stack &stack)
{
stack.top=-1;
}

void push_stack(Stack &stack,int a,int b,int c)
{
stack.top++;
stack.move[stack.top].x=a;
stack.move[stack.top].y=b;
stack.move[stack.top].d=c;

}

void pop_stack(Stack &stack)
{
stack.top--;
}
int search_d(int p[][column],int m,int n,int d)
{
switch(d)
{
case 0:return p[m][n+1];
break;
case 1:return p[m+1][n+1];
break;
case 2:return p[m+1][n];
break;
case 3:return p[m+1][n-1];
break;
case 4:return p[m][n-1];
break;
case 5:return p[m-1][n-1];
break;
case 6:return p[m-1][n];
break;
case 7:return p[m-1][n+1];
break;
}
}

void print_map(Stack &stack) //打印出当时的路径
{
int i;
for(i=0;i<=stack.top;i++)
{
printf("(%d,%d) ",stack.move[i].x,stack.move[i].y);
}

printf("(%d,%d)\n",row-2,column-2);
}

void move_map(Stack &stack,int p[][column])//小球走的过程
{
int m=1,n=1,d=0;
while(1)
{
while(d<8)
{
if(search_d(p,m,n,d)==0)
{
push_stack(stack,m,n,d);
p[m][n]=-1;
switch(d)
{
case 0:{n++;}
break;
case 1:{m++;n++;}
break;
case 2:{m++;}
break;
case 3:{m++;n--;}
break;
case 4:{n--;}
break;
case 5:{m--;n--;}
break;
case 6:{m--;}
break;
case 7:{m--;n++;}
break;
default:printf("The error:\n");
break;
}
d=0;
}
else d++;
if(m==row-2&&n==column-2) break;

}
if( 8 ==  d)
{
d=stack.move[stack.top].d+1;
m=stack.move[stack.top].x;
n=stack.move[stack.top].y;
pop_stack(stack);
}

if(m==row-2&&n==column-2)
{
print_map(stack);
d=stack.move[stack.top].d+1;
m=stack.move[stack.top].x;
n=stack.move[stack.top].y;
pop_stack(stack);
p[m][n]=0;
}

if(m==1&&n==1) break;
}

}

//void  ssss()
//{
// while (1)
// {
//  while (1)
//  {
//  int a = 0;
//   if(1)
//   {
//   a++;
//    if (a>0)
//    {
//
//     for(int i = 0; i<= 10; i++ )
//      {
//
//       break;    //结束最近的循环，不判断，switch也算是循环
//       continue; //结束最近的循环，判断
//      }
//
//
//     break;
//    }
//    break;
//   }
//  }
// }
//}

int _tmain(int argc, _TCHAR* argv[])
{
/*ssss();*/
Stack stack;
int p[row][column]={{1,1,1,1,1,1},{1,0,0,1,1,1},{1,1,0,0,1,1},{1,1,1,0,0,1},{1,1,1,1,0,1},{1,1,1,1,0,1},{1,1,1,1,1,1}};
init_stack(stack);
move_map(stack,p);
system("pause");
}

• 评论

• 上一篇
• 下一篇