迷宫的最短路径 (c++)

实验题目(共10题, 第9题)


标题: 迷宫的最短路径
时 限: 1000 ms
内存限制: 10000 K
总时限: 3000 ms
描述: 设计一个算法找一条从迷宫入口到出口的最短路径。
输入: 迷宫的行和列m n
迷宫的布局
输出: 最短路径
输入样例: 请输入迷宫的行和列:6 8
请输入迷宫的布局:
0 1 1 1 0 1 1 1
1 0 1 0 1 0 1 0
0 1 0 0 1 1 1 1
0 1 1 1 0 0 1 1
1 0 0 1 1 0 0 0
0 1 1 0 0 1 1 0
输出样例:

最短路径为:
(6,8)(5,7)(4,6) (4,5)(3,4) (3,3) (2,2)(1,1)  

提示:  
来源:
 
 
View Code
  1 #include <stdio.h>
2 #include <stdlib.h>
3 #define QUEUE_INIT_SIZE 100
4 #define QUEUEINCREMENT 10
5 typedef struct Point
6 {
7 int x;
8 int y;
9 } Point;
10 typedef struct QElemType
11 {
12 int pre;
13 Point pos;
14 } QElemType;
15 struct SqQueue
16 {
17 QElemType *base;
18 int front;
19 int rear;
20 int size;
21 };
22
23 void InitQueue(SqQueue&lq)
24 {
25 lq.base=(QElemType*)malloc(QUEUE_INIT_SIZE*sizeof(QElemType));
26 lq.front=lq.rear=0;
27 lq.size=QUEUE_INIT_SIZE;
28
29 }
30 void EnQueue(SqQueue&lq,QElemType e)
31 {
32 if(lq.rear>=lq.size)
33 {
34 lq.base=(QElemType*)realloc(lq.base,(lq.size+QUEUEINCREMENT)*sizeof(QElemType));
35 lq.size+=QUEUEINCREMENT;
36 }
37 *(lq.base+lq.rear)=e;
38 lq.rear++;
39 }
40 int QueueEmpty(SqQueue lq)
41 {
42 if(lq.front==lq.rear)
43 {
44 return 1;
45 }
46 else return 0;
47 }
48 void DeQueue(SqQueue&lq,QElemType&e)
49 {
50 e=*(lq.base+lq.front);
51 lq.front++;
52
53 }
54 Point NextPos( int**a,Point prePos,int i)
55 {
56 switch(i)
57 {
58 case 1:
59 prePos.y++;
60 break;
61 case 2:
62 prePos.x++;
63 prePos.y++;
64 break;
65 case 3:
66 prePos.x++;
67 break;
68 case 4:
69 prePos.x++;
70 prePos.y--;
71 break;
72 case 5:
73 prePos.y--;
74 break;
75 case 6:
76 prePos.y--;
77 prePos.x--;
78 break;
79 case 7:
80 prePos.x--;
81 break;
82 case 8:
83 prePos.x--;
84 prePos.y++;
85 break;
86 }
87 return prePos;
88
89 }
90 int ShortestPath(int**a,SqQueue&lq,Point StartPos,Point end,int m,int n)
91 {
92 QElemType e;
93 int idx,i;
94 Point prePos,curPos;
95 InitQueue(lq);
96 curPos = StartPos;
97 e.pos = curPos;
98 e.pre = -1;
99 EnQueue(lq,e);
100 a[curPos.x][curPos.y]=4;
101 while (!QueueEmpty(lq))
102 {
103 idx = lq.front;
104 DeQueue(lq,e);
105 prePos = e.pos;
106 for (i=1; i<=8; i++)
107 {
108 curPos = NextPos(a,prePos,i);
109 if (0<=curPos.x&&curPos.x<m&&curPos.y>=0&&curPos.y<n&&a[curPos.x][curPos.y]==0)
110 {
111 e.pos = curPos;
112 e.pre = idx;
113 EnQueue(lq, e);
114 a[curPos.x][curPos.y]=4;
115 }
116 if (curPos.x == end.x && curPos.y == end.y) return 1;
117 }
118 }
119 return 0;
120 }
121 void PrintPath( SqQueue&lq)
122 {
123 int i;
124 QElemType e;
125 i=lq.rear-1;
126 do
127 {
128 e=*(lq.base+i);
129 printf("(%d,%d)\n",e.pos.x+1,e.pos.y+1);
130
131 i = e.pre;
132
133 }
134 while(i!=-1);
135 }
136
137 int main()
138 {
139 int m,n,**a,i,j;
140 SqQueue lq;
141 Point StartPos,end;
142 scanf("%d%d",&m,&n);
143 StartPos.x=0;
144 StartPos.y=0;
145 end.x=m-1;
146 end.y=n-1;
147 a=(int**)malloc(m*sizeof(int*));
148 for(i=0; i<m; i++)
149 {
150 a[i]=(int*)malloc(n*sizeof(int));
151 }
152 for(i=0; i<m; i++)
153 {
154 for(j=0; j<n; j++)
155 {
156 scanf("%d",&a[i][j]);
157 }
158 }
159 ShortestPath(a,lq,StartPos,end,m,n);
160 PrintPath(lq);
161
162
163 return 0;
164 }


posted @ 2011-05-24 17:54  itbird  Views(5669)  Comments(3Edit  收藏  举报