c++解决迷宫寻路问题(一)

2014-11-24 07:38:47 · 作者: · 浏览: 2
[cpp]
// time.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include
#include
#include
#include
using namespace std;
int box[8][10]={{1,1,1,1,1,1,1,1,1,1},
{1,0,1,1,1,0,1,1,1,1},
{1,1,0,1,0,1,1,1,1,1},
{1,0,1,0,0,0,0,0,1,1},
{1,0,1,1,1,0,1,1,1,1},
{1,1,0,0,1,1,0,0,0,1},
{1,0,1,1,0,0,1,1,0,1},
{1,1,1,1,1,1,1,1,1,1}};
struct sPoint
{
sPoint(int x1,int y1){x=x1;y=y1;}
int x;
int y;
};
void calc(int *box,int width,int height,sPoint start,sPoint end)
{
list s;
box[start.x*width+start.y]=2;
int x=start.x,y=start.y;
s.push_back(sPoint(x,y));
while(1)
{
if(x==end.x && y==end.y)
{
while (!s.empty())
{
cout<
s.pop_front();
}
break;
}
else
{
//cout<
}
bool bGo=false;
for(int i=0;i<8;i++)
{
switch(i)
{
case 0:
{
if(y>0 && box[(y-1)*width+x]==0)
{
y=y-1;
box[y*width+x]=2;
s.push_back(sPoint(x,y));
bGo=true;
}
break;
}
case 1:
{
if(x0 && box[(y-1)*width+x+1]==0)
{
x=x+1;
y=y-1;
box[y*width+x]=2;
s.push_back(sPoint(x,y));
bGo=true;
}
break;
}
case 2:
{
if(x
{
x=x+1;
box[y*width+x]=2;
s.push_back(sPoint(x,y));
bGo=true;
}
break;
}
case 3:
{
if(x
{
x=x+1;
y=y+1;
box[y*width+x]=2;
s.push_back(sPoint(x,y));
bGo=true;
}
break;
}
case 4:
{
if(y
{
y=y+1;
box[y*width+x]=2;
s.push_back(sPoint(x,y));
bGo=true;
}
break;
}
case 5:
{
if(x>0 && y
{
x=x-1;
y=y+1;
box[y*width+x]=2;
s.push_back(sPoint(x,y));
bGo=true;
}
break;
}
case 6:
{
if(x>0 && box[y*width+x-1]==0)
{
x=x-1;
box[y*width+x]=2;
s.push_back(sPoint(x,y));
bGo=true;
}
break;
}
case 7:
{
if(x>0 && y>0 && box[(y-1)*width+x-1]==0)
{
x=x-1;
y=y-1;
box[y*width+x]=2;
s.push_back(sPoint(x,y));
bGo=true;
}
break;
}