广度优先搜索(Breadth First Search)

Date:2019-07-03 14:29:02

  • 走完一层的所有房间,再走下一层,用队列实现

算法实现

  1 /*--------------------------模版----------------------*/
  2 void BFS(int s)
  3 {
  4     queue<int> q;
  5     q.push(s);
  6     while(!q.empty())
  7     {
  8         //取出队首元素top
  9         //访问队首元素top
 10         //将队首元素出列
 11         //将top的下一层结点中未曾入队的结点全部入队,并设置为已入队
 12     }
 13 }
 14 
 15 /*--------------------------访问-----------------------*/
 16 struct node
 17 {
 18     int data;
 19 }a[10];
 20 
 21 int main()
 22 {
 23     queue<int> q, p;
 24     for(int i=1; i<=3; i++)
 25     {
 26         a[i].data = i;  //a[1]=1,a[2]=2,a[3]=3
 27         q.push(i);      //这里将数组的下标入队,而非数组,可以避免传递形参,而无法修改实参的情况
 28         p.push(a[i]);
 29     }
 30     p.front().data = 100;       //形参无法修改实参
 31     printf("%d\n", a[1].data);  //output:1
 32     a[1] = 50;                      //实参也无法修改形参
 33     printf("%d\n", p.front().data); //output:1
 34 
 35     a[q.front()].data = 100;    //通过传递下标,可以修改实参的值
 36     printf("%d\n", a[1].data);  //output:100
 37     return 0;
 38 }
 39 /*-------------------------矩阵问题--------------------*/
 40 /*
 41 Descriptation:
 42     给出一个m*n的矩阵,矩阵中的元素为0或1。称位置(x,y)与其上下左右四个位置是相邻的。
 43 如果矩阵中有若干个1是相邻的(不必两两相邻),那么称这些1构成了一个“块”。求给定的矩阵
 44 中“块”的个数。
 45 
 46 Sample Input:
 47 0 1 1 1 0 0 1
 48 0 0 1 0 0 0 0
 49 0 0 0 0 1 0 0
 50 0 0 0 1 1 1 0
 51 1 1 1 0 1 0 0
 52 1 1 1 1 0 0 0
 53 
 54 Sample output:
 55 4
 56 */
 57 
 58 #include <cstdio>
 59 #include <queue>
 60 using namespace std;
 61 const int MAX_SIZE = 110;
 62 struct node
 63 {
 64     int x, y;
 65     int data;
 66     bool status;        //status表示是否入队,而非是否已访问,否则会重复访问
 67 }matrix[MAX_SIZE][MAX_SIZE];
 68 
 69 //X[],Y[]存放四个操作方向
 70 int n, m, X[4]={0,0,1,-1}, Y[4]={1,-1,0,0};
 71 queue<node> q;
 72 
 73 void Init()
 74 {
 75     for(int i=0; i<n; i++)
 76         for(int j=0; j<m; j++)
 77         {
 78             scanf("%d", &matrix[i][j].data);
 79             matrix[i][j].status = true;
 80             matrix[i][j].x = i;
 81             matrix[i][j].y = j;
 82         }
 83 }
 84 
 85 void BFS(node x)
 86 {
 87     q.push(x);
 88     while(!q.empty())
 89     {
 90         node s = q.front();
 91         q.pop();
 92         matrix[s.x][s.y].status = false;        //这里不能修改s,需要修改matrix内的元素
 93         if(s.data)
 94         {
 95             for(int i=0; i<4; i++)
 96             {
 97                 int nowX = s.x + X[i];
 98                 int nowY = s.y + Y[i];
 99                 if(nowX<0 || nowX>=n || nowY<0 || nowY>=m)
100                     continue;
101                 if(matrix[nowX][nowY].data && matrix[nowX][nowY].status)
102                     q.push(matrix[nowX][nowY]);
103             }
104         }
105     }
106 }
107 
108 int main()
109 {
110 #ifdef ONLINE_JUDGE
111 #else
112     freopen("Test.txt", "r", stdin);
113 #endif // ONLINE_JUDGE
114 
115     scanf("%d%d", &n, &m);
116     Init();
117     int cnt = 0;
118     for(int i=0; i<n; i++)
119         for(int j=0; j<m; j++)
120         {
121             if(matrix[i][j].data && matrix[i][j].status)
122             {
123                 BFS(matrix[i][j]);
124                 cnt++;
125             }
126         }
127     printf("%d\n", cnt);
128 
129     return 0;
130 }
131 /*-------------------------迷宫问题--------------------*/
132 /*
133 Description:
134     给定一个n*m大小的迷宫,其中*代表不可通过的墙壁,而“.”代表平底,S表示起点,T表示终点。
135 移动过程中,如果当前位置是(x,y)(下标从0开始),且每次只能前往上下左右四个位置的平地,
136 求从起点到达终点的T的最小步数;
137 
138 Sample Input:
139 . . . . .
140 . * . * .
141 . * S * .
142 . * * * .
143 . . . T *
144 */
145 
146 #include <cstdio>
147 #include <queue>
148 using namespace std;
149 const int MAX_SIZE = 110;
150 struct node
151 {
152     char data;
153     bool status;
154     int x, y;
155     int level;
156 }matrix[MAX_SIZE][MAX_SIZE];
157 
158 int n, m, X[4]={0,0,1,-1}, Y[4]={1,-1,0,0};
159 void Init()
160 {
161     scanf("%d%d", &n, &m);
162     getchar();
163     for(int i=0; i<n; i++)
164     {
165         for(int j=0; j<m; j++)
166         {
167             matrix[i][j].data = getchar();
168             getchar();
169             matrix[i][j].status = true;
170             matrix[i][j].x = i;
171             matrix[i][j].y = j;
172         }
173     }
174 
175 }
176 
177 int BFS(node s)
178 {
179     queue<node> q;
180     q.push(s);
181     matrix[s.x][s.y].level = 1;
182     while(!q.empty())
183     {
184         node v = q.front();
185         q.pop();
186         matrix[v.x][v.y].status = false;
187         for(int i=0; i<4; i++)
188         {
189             int nowX = v.x+X[i];
190             int nowY = v.y+Y[i];
191             if(nowX<0 || nowX>=n || nowY<0 || nowY>=m)
192                 continue;
193             if(matrix[nowX][nowY].data=='.' && matrix[nowX][nowY].status)
194             {
195                 q.push(matrix[nowX][nowY]);
196                 matrix[nowX][nowY].level = matrix[v.x][v.y].level+1;
197             }
198             else if(matrix[nowX][nowY].data=='T')
199                 return matrix[v.x][v.y].level;
200         }
201     }
202     return -1;  //无法到达
203 }
204 
205 int main()
206 {
207 #ifdef ONLINE_JUDGE
208 #else
209     freopen("Test.txt", "r", stdin);
210 #endif // ONLINE_JUDGE
211 
212     Init();
213     for(int i=0; i<n; i++)
214         for(int j=0; j<m; j++)
215             if(matrix[i][j].data=='S')
216                 printf("%d\n", BFS(matrix[i][j]));
217 
218     return 0;
219 }

猜你喜欢

转载自www.cnblogs.com/blue-lin/p/11126219.html