View Code
1 #include <stdio.h> 2 #include <queue> 3 using namespace std; 4 #define N 100 5 #define M 100 6 int n, m; 7 struct State 8 { 9 int x; 10 int y; 11 int step; 12 }; 13 int flags[N + 5][M + 5]; 14 int BFS(State souce) 15 { 16 queue<State> q; 17 State cur, pre; 18 int i, j; 19 for (i = 0; i < n; i++) 20 for (j = 0; j < m; j++) 21 flags[i][j] = 0; 22 q.push(souce); 23 flags[0][0] = 1; 24 while (!q.empty()) 25 { 26 pre = q.front(); 27 q.pop(); 28 //using for circulation 29 { 30 //using 'pre' to get 'cur' 31 cur.step = pre.step + 1; 32 //judege if out of index 33 //judge whether u have traversed the state 34 if (flags[i][j] == 0) 35 { 36 //if u find it 37 return step; 38 q.push(cur); 39 flag[i][j] = 1; 40 } 41 } 42 } 43 return 0; 44 }
well, I know it's not very good, I want to write is the model of BFS. But I find there is a lots situation, well, let's me have a try, I will modify it until I can get the API of the BFS belong to me.