首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

找迷宫路径,有点有关问题

2012-02-29 
找迷宫路径,有点问题测试用迷宫a[7][7]11111111-1000011000001100000110000211111111-1入口2为出口应该得

找迷宫路径,有点问题
测试用迷宫a[7][7]
1   1   1   1   1   1   1  
1   -1   0   0   0   0   1
1   0   0   0   0   0   1  
1   0   0   0   0   0   1
1   0   0   0   0   2   1
1   1   1   1   1   1   1
-1入口   2为出口
应该得到:
1     1     1     1     1     1     1  
1   -1   -2   -3   -4   -5     1
1   -2   -3   -4   -5   -6     1  
1   -3   -4   -5   -6   -7     1
1   -4   -5   -6   -7   -8     1
1     1     1     1     1     1     1
实际却有错误,请高手看看


#include <iostream>
#include <queue>
using   namespace   std;
const   int   H   =   6;
const   int   W   =   6;
struct   Point
{
Point(){x   =   0;y   =   0;}
Point(int   xx,int   yy){x   =   xx;y   =   yy;}
int   x;
int   y;
};
int   main()
{
int   a[H   +   1][W   +   1];
for(int   n   =   0;n   <   W   +   1;n++)
{
a[0][n]   =   1;
a[n][0]   =   1;
a[H][n]   =   1;
a[n][W]   =   1;
}
for(int   i   =   1;i   <   H;i++)
{
for(int   j   =   1;j   <   W;j++)
{
a[i][j]   =   0;
}
}
a[1][1]   =   -1;
a[5][5]   =   2;
for(i   =   0;i   <=   H;i++)
{
for(int   j   =   0;j   <=   W;j++)
{
cout < <a[i][j] < < "   ";
}
cout < <endl;
}

//////////////////////////
queue <Point>   mpoint;
Point   start(1,1);
Point   end(5,5);
mpoint.push(start);
Point   temp;
Point   temp2;
while(!mpoint.empty())
{
temp   =   mpoint.front();
mpoint.pop();
//判断点上面的点
if(a[temp.x   -   1][temp.y]   ==   0)
{
a[temp.x   -   1][temp.y]   =   a[temp.x][temp.y]   -   1;
temp2.x   =   temp.x   -   1;
temp2.y   =   temp.y;
mpoint.push(temp2);
}
if(a[temp.x   -   1][temp.y]   ==   2)
{
a[temp.x   -   1][temp.y]   =   a[temp.x][temp.y]   -   1;
break;
}
//判断点下面的点
if(a[temp.x   +   1][temp.y]   ==   0)
{
a[temp.x   +   1][temp.y]   =   a[temp.x][temp.y]   -   1;
temp2.x   =   temp.x   +   1;
temp2.y   =   temp2.y;
mpoint.push(temp2);
}
if(a[temp.x   +   1][temp.y]   ==   2)
{
a[temp.x   +   1][temp.y]   =   a[temp.x][temp.y]   -   1;
break;
}
                //判断点左面的点

if(a[temp.x][temp.y   -   1]   ==   0)
{
a[temp.x][temp.y   -   1]   =   a[temp.x][temp.y]   -   1;


temp2.x   =   temp.x;
temp2.y   =   temp.y   -   1;
mpoint.push(temp2);
}
if(a[temp.x][temp.y   -   1]   ==   2)
{
a[temp.x][temp.y   -   1]   =   a[temp.x][temp.y]   -   1;
break;
}
//判断点右面的点

if(a[temp.x][temp.y   +   1]   ==   0)
{
a[temp.x][temp.y   +   1]   =   a[temp.x][temp.y]   -   1;
temp2.x   =   temp.x;
temp2.y   =   temp.y   +   1;
mpoint.push(temp2);
}
if(a[temp.x][temp.y   -   1]   ==   2)
{
a[temp.x][temp.y   -   1]   =   a[temp.x][temp.y]   -   1;
break;
}
}
if(a[end.x][end.y]   ==   2)
{
cout < < "没有找到一条路径 " < <endl;
}
for(i   =   0;i   <=   H;i++)
{
for(int   j   =   0;j   <=   W;j++)
{
cout < <a[i][j] < < "   ";
}
cout < <endl;
}
return   0;
}

[解决办法]
......#include <iostream>
#include <queue>
using namespace std;
const int H = 6;
const int W = 6;
struct Point
{
Point(){x = 0;y = 0;}
Point(int xx,int yy){x = xx;y = yy;}
int x;
int y;
};
int main()
{
int a[H + 1][W + 1];
for(int n = 0;n < W + 1;n++)
{
a[0][n] = 1;
a[n][0] = 1;
a[H][n] = 1;
a[n][W] = 1;
}
for(int i = 1;i < H;i++)
{
for(int j = 1;j < W;j++)
{
a[i][j] = 0;
}
}
a[1][1] = -1;
a[5][5] = 2;
for(i = 0;i <= H;i++)
{
for(int j = 0;j <= W;j++)
{
cout < <a[i][j] < < " ";
}
cout < <endl;
}

//////////////////////////
queue <Point> mpoint;
Point start(1,1);
Point end(5,5);
mpoint.push(start);
Point temp;
Point temp2;
while(!mpoint.empty())
{
temp = mpoint.front();
mpoint.pop();
//判断点上面的点
if(a[temp.x - 1][temp.y] == 0)
{
a[temp.x - 1][temp.y] = a[temp.x][temp.y] - 1;
temp2.x = temp.x - 1;
temp2.y = temp.y;
mpoint.push(temp2);
}
if(a[temp.x - 1][temp.y] == 2)
{
a[temp.x - 1][temp.y] = a[temp.x][temp.y] - 1;
break;
}
//判断点下面的点
if(a[temp.x + 1][temp.y] == 0)
{
a[temp.x + 1][temp.y] = a[temp.x][temp.y] - 1;
temp2.x = temp.x + 1;
temp2.y = temp2.y;
mpoint.push(temp2);
}
if(a[temp.x + 1][temp.y] == 2)
{
a[temp.x + 1][temp.y] = a[temp.x][temp.y] - 1;
break;
}
//判断点左面的点

if(a[temp.x][temp.y - 1] == 0)
{
a[temp.x][temp.y - 1] = a[temp.x][temp.y] - 1;
temp2.x = temp.x;
temp2.y = temp.y - 1;
mpoint.push(temp2);
}
if(a[temp.x][temp.y - 1] == 2)
{
a[temp.x][temp.y - 1] = a[temp.x][temp.y] - 1;
break;
}
//判断点右面的点

if(a[temp.x][temp.y + 1] == 0)
{
a[temp.x][temp.y + 1] = a[temp.x][temp.y] - 1;
temp2.x = temp.x;
temp2.y = temp.y + 1;
mpoint.push(temp2);
}
if(a[temp.x][temp.y - 1] == 2)
{
a[temp.x][temp.y - 1] = a[temp.x][temp.y] - 1;
break;
}
}
if(a[end.x][end.y] == 2)
{
cout < < "没有找到一条路径 " < <endl;
}
for(i = 0;i <= H;i++)
{
for(int j = 0;j <= W;j++)


{
cout < <a[i][j] < < " ";
}
cout < <endl;
}
return 0;
}

热点排行