搜索题:

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <queue>
  5 #include <algorithm>
  6 using namespace std;
  7 
  8 int n, m;
  9 char MAP[550][550];
 10 int turn[550][550]; //记录每个点的最少转弯次数
 11 int dir[4][2] = {{-1, 0},{1, 0},{0, 1},{0, -1}};
 12 int x_1, y_1;
 13 int x2, y2;
 14 int time;
 15 
 16 struct Node
 17 {
 18     int x;
 19     int y;
 20 };
 21 
 22 queue<Node >Q;
 23 
 24 void clean_queue()
 25 {
 26     while( !Q.empty() )
 27     {
 28         Q.pop();
 29     }
 30 }
 31 
 32 bool out_line(int l, int r)
 33 {
 34     if( l >= 0 && l <= n + 1 && r >= 0 && r <= m + 1 && MAP[l][r] != 'X' )
 35     {
 36         return true;
 37     }
 38     else
 39     {
 40         return false;
 41     }
 42 }
 43 
 44 int BFS()
 45 {
 46     turn[x_1][y_1] = 0;
 47     clean_queue();
 48     Node p, s;
 49     p.x = x_1;
 50     p.y = y_1;
 51     //p.step = 0;
 52     Q.push(p);
 53     while( !Q.empty() )
 54     {
 55         p = Q.front();
 56         Q.pop();
 57         for( int i = 0; i < 4; i++ )
 58         {
 59             int k = turn[p.x][p.y] + 1;
 60             int xx = p.x + dir[i][0];
 61             int yy = p.y + dir[i][1];
 62             //int px = p.x, py = p.y;
 63             while( ( out_line(xx, yy) && turn[xx][yy] >= turn[p.x][p.y] + 1 ) || ( xx == x2 && yy == y2 ) )
 64             {
 65                 if( turn[xx][yy] > turn[p.x][p.y] + 1 )
 66                 {
 67                     turn[xx][yy] = k;
 68                     s.x = xx;
 69                     s.y = yy;
 70                     Q.push(s);
 71                 }
 72                 xx = xx + dir[i][0];
 73                 yy = yy + dir[i][1];
 74             }
 75         }
 76     }
 77     return turn[x2][y2];
 78 }
 79 
 80 int main()
 81 {
 82     int num = 0;
 83     while( cin>>m>>n )
 84     {
 85         getchar();
 86         if( n == 0 && m == 0 )
 87         {
 88             break;
 89         }
 90         memset(MAP, ' ', sizeof(MAP));
 91         for( int i = 1; i <= n; i++ )
 92         {
 93             for( int j = 1; j <= m; j++ )
 94             {
 95                 scanf("%c",&MAP[i][j]);
 96             }
 97             getchar();
 98         }
 99         cout<<"Board #"<<++num<<":"<<endl;
100         int Case = 0;
101         for( int Case = 1; ; Case++ )
102         {
103             memset(turn, 0x3f, sizeof(turn));
104             cin>>y_1>>x_1>>y2>>x2;
105             if( x_1 == 0 && y_1 == 0 && x2 == 0 && y2 == 0 )
106             {
107                 break;
108             }
109             time = BFS();
110             if( time < 0x3f3f3f3f )
111             {
112                 cout<<"Pair "<<Case<<": "<<time<<" segments."<<endl;
113             }
114             else
115             {
116                 cout<<"Pair "<<Case<<":"<<" impossible."<<endl;
117             }
118         }
119         cout<<endl;
120     }
121     return 0;
122 }
View Code


网上看了别人说的,好像说AC也未必对。

可能就是水过去了。。。

PS:第一次写博客。。贴代码。 超害怕错误的