1 #include <iostream> 2 #include <queue> 3 using namespace std; 4 const int INF=100000000; 5 typedef pair<int,int> P; 6 /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 7 8 9 //int bfs(); 10 int main(int argc, char** argv) { 11 int m,n;//migong size 12 13 int sx,sy;//init position 14 int dx,dy;//destination 15 16 int nx,ny; 17 18 int mx[4]={1,0,-1,0}; 19 int my[4]={0,1,0,-1}; 20 21 cout<<"input m,n"<<endl; 22 cin>>m>>n; 23 int d[m][n];//distance 24 int i,j; 25 char maze[m][n];//migong 26 for(i=0;i<m;i++){ 27 for(j=0;j<n;j++){ 28 cin>>maze[i][j]; 29 } 30 } 31 int cc,dd; 32 for(cc=0;cc<m;cc++){ 33 for(dd=0;dd<n;dd++){ 34 cout<<maze[cc][dd]<<" "; 35 } 36 cout<<endl; 37 } 38 cout<<"input sx,sy"<<endl; 39 cin>>sx>>sy; 40 cout<<sx<<" "<<sy<<endl; 41 cout<<"input dx,dy"<<endl; 42 cin>>dx>>dy; 43 cout<<dx<<" "<<dy<<endl; 44 int a,b; 45 for(a=0;a<m;a++){ 46 for(b=0;b<n;b++){ 47 d[a][b]=INF; 48 49 } 50 } 51 52 queue<P> que; 53 que.push(P(sx,sy)); 54 d[sx][sy]=0; 55 56 while(que.size()){ 57 58 P p=que.front(); 59 que.pop(); 60 61 62 63 if(p.first==dx&&p.second==dy) break; 64 int x; 65 for(x=0;x<4;x++){ 66 nx=p.first+mx[x]; 67 ny=p.second+my[x]; 68 69 70 if(0<=nx&&nx<=n&&ny<=m&&ny>=0&&maze[nx][ny]!='#'&&d[nx][ny]==INF){ 71 que.push(P(nx,ny)); 72 d[nx][ny]=d[p.first][p.second]+1; 73 cout<<"d[nx][ny]="<<d[nx][ny]<<endl; 74 } 75 76 } 77 } 78 int hh,jj; 79 for(hh=0;hh<m;hh++){ 80 for(jj=0;jj<n;jj++){ 81 cout<<d[hh][jj]<<" "; 82 83 } 84 cout<<endl; 85 } 86 cout<<endl; 87 cout<<d[dx][dy]<<endl; 88 return 0; 89 }
改进版
1 #include <iostream> 2 #include <queue> 3 using namespace std; 4 const int INF=100000000; 5 typedef pair<int,int> P; 6 /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 7 8 9 //int bfs(); 10 int main(int argc, char** argv) { 11 int m,n;//migong size 12 13 int sx,sy;//init position 14 int dx,dy;//destination 15 16 int nx,ny; 17 18 int mx[4]={1,0,-1,0}; 19 int my[4]={0,1,0,-1}; 20 21 cout<<"input m,n"<<endl; 22 cin>>m>>n; 23 int d[m][n];//distance 24 int i,j; 25 char maze[m][n];//migong 26 for(i=0;i<m;i++){ 27 for(j=0;j<n;j++){ 28 cin>>maze[i][j]; 29 } 30 } 31 int cc,dd; 32 for(cc=0;cc<m;cc++){ 33 for(dd=0;dd<n;dd++){ 34 cout<<maze[cc][dd]<<" "; 35 } 36 cout<<endl; 37 } 38 cout<<"input sx,sy"<<endl; 39 cin>>sx>>sy; 40 cout<<sx<<" "<<sy<<endl; 41 sx=sx-1; 42 sy=sy-1; 43 cout<<"input dx,dy"<<endl; 44 cin>>dx>>dy; 45 cout<<dx<<" "<<dy<<endl; 46 dx=dx-1; 47 dy=dy-1; 48 int a,b; 49 for(a=0;a<m;a++){ 50 for(b=0;b<n;b++){ 51 d[a][b]=INF; 52 53 } 54 } 55 56 queue<P> que; 57 que.push(P(sx,sy)); 58 d[sx][sy]=0; 59 60 while(que.size()){ 61 62 P p=que.front(); 63 que.pop(); 64 65 66 67 if(p.first==dx&&p.second==dy) break; 68 int x; 69 for(x=0;x<4;x++){ 70 nx=p.first+mx[x]; 71 ny=p.second+my[x]; 72 73 74 if(0<=nx&&nx<=n&&ny<=m&&ny>=0&&maze[nx][ny]!='#'&&d[nx][ny]==INF){ 75 que.push(P(nx,ny)); 76 d[nx][ny]=d[p.first][p.second]+1; 77 cout<<"d[nx][ny]="<<d[nx][ny]<<endl; 78 } 79 80 } 81 } 82 int hh,jj; 83 for(hh=0;hh<m;hh++){ 84 for(jj=0;jj<n;jj++){ 85 cout<<d[hh][jj]<<" "; 86 87 } 88 cout<<endl; 89 } 90 cout<<endl; 91 cout<<d[dx][dy]<<endl; 92 return 0; 93 }