poj 1915 Knight Moves

#include 
#include 
using namespace std;
const int MAX = 310;
int map[MAX][MAX], row, r, l, sx, sy, ex, ey;
bool vis[MAX][MAX];
//坐标的变换,即是可以移动的方格 
int dir[8][2] = {{-2, -1}, {-2, 1}, {-1, -2}, {-1, 2}, {1, -2}, {2, -1}, {2, 1}, {1, 2}};

struct Info{
       int x, y;
       int step;
}info[MAX*MAX];


void solve(int x, int y)
{
     if (x >= 0 && y >= 0 && x < row && y < row && !vis[x][y]){
         info[r].x = x;
         info[r].y = y;
         info[r].step = info[l].step + 1;
         vis[x][y] = 1;
         r++;
     }
}


void bfs()
{
     int i, tmpx, tmpy;
     info[0].x = sx;
     info[0].y = sy;
     vis[sx][sy] = 1;
     l = 0;
     r = 1;
     while (l != r){
            if (info[l].x == ex && info[l].y == ey){
                 cout << info[l].step << endl;
                 break;
            }
            //由上一步得到下一步  
            for (i = 0; i < 8; i++){
                tmpx = info[l].x + dir[i][0];
                tmpy = info[l].y + dir[i][1];
                solve(tmpx, tmpy);
            }
            l++;
     }
}


int main()
{
    int tc;
    cin >> tc;
    while (tc--){
          cin >> row >> sx >> sy >> ex >> ey;
          memset(vis, 0, sizeof(vis));
          bfs();
    }
    
    system("pause");
}

你可能感兴趣的:(POJ,include,struct,system)