POJ 3984 迷宫问题.docVIP

  • 2
  • 0
  • 约4.24千字
  • 约 9页
  • 2018-01-01 发布于河南
  • 举报
POJ 3984 迷宫问题

POJ 3984 迷宫问题 // 定义一个二维数组: // // int maze[5][5] = { // 0, 1, 0, 0, 0, // 0, 1, 0, 1, 0, // 0, 0, 0, 0, 0, // 0, 1, 1, 1, 0, // 0, 0, 0, 1, 0, // }; // // 它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。 #include iostream using namespace std; class Maze //迷宫类 { public: void SetMaze(int [][5],int,int);//申明是必须指定二维数组的长度y void SetMaze_pot(int,int,int,int);//输入迷宫,以及入口位置,出口位置 void GetMaze_way(); private: int maze[5][5]; int way[100][2];//路径数组 int num; int num1; int num2;//计数器 int total_choise[100];//总共的可供选择数 int ori[5][5][4]; int start_x; int start_y; int finish_x; int finish_y; int Maze_pro(int,int,int,int );//输入当前节点坐标以及前一节点坐标,输出下一节点坐标和当前坐标 }; int main() { // int maze[5][5] ={ // 0, 1, 0, 0, 0, // 0, 1, 0, 1, 0, // 0, 0, 0, 0, 0, // 0, 1, 1, 1, 0, // 0, 0, 0, 1, 0, // }; int maze[5][5] ={ 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, };//你需要测试的迷宫 Maze M; M.SetMaze(maze,5,5); /* M.SetMaze_pot(0,0,4,4);*/ M.SetMaze_pot(0,1,3,4);//测试迷宫所用的出入口(x,y) M.GetMaze_way(); return 0; } void Maze::SetMaze(int arr[][5],int m,int n)//将二维数组传入函数中 { for (int i=0;im;i++) { for (int j=0;jn;j++) { maze[i][j] = arr[i][j]; } } } void Maze::SetMaze_pot(int x1,int y1,int x2,int y2) { start_x = x1; start_y = y1; finish_x = x2; finish_y = y2; num = 0; way[0][0] = 0; way[0][1] = 0; for (int i = 0; i 5;i++) { for (int j = 0;j 5;j++) { ori[i][j][0] = 1;//1表示未走过,0表示已走过 ori[i][j][1] = 1; ori[i][j][2] = 1; ori[i][j][3] = 1; } } } int Maze::Maze_pro(int x,int y,int fx,int fy) { int tempx,tempy; tempx = x; tempy = y; total_choise[num] = (maze[x+1][y]!=1x+1!=fxx+15ori[tempx][tempy][0]!=0) +(maze[x-1][y]!=1x-1!=fxx-1=0ori[tempx][tempy][1]!=0) +(maze[x][y+1]!=1y+1!=fyy+15ori[tempx][tempy][2]!=0) +(maze[x][y-1]!=1y-1!=fyy-1=0ori[tempx][tempy][3]!=0); if (x == finish_x y == finish_y)//无路可走 { num1 = num; cout需要走num1+1步endl; cou

文档评论(0)

1亿VIP精品文档

相关文档