小墨在努力!!
吗了个b的。。。。。
#include <cstdio>
#include <queue>
using namespace std ;

int dir[8][2] = {
    -2 ,-1 ,-2 ,1 ,-1 ,2 ,1 ,2 ,2 ,1 ,2 ,-1 ,1 ,-2 ,-1 ,-2
} ;
int obstacle[4][2] = {
    -1 ,0 ,0 ,1 ,1 ,0 ,0 ,-1
} ;
typedef struct coord{
    int x ,y ,step;
}coord ;
bool map[100][100] ,visit[100][100] ;
int p ,q ;

int bfs(coord start ,coord end) {

    queue<coord> que ;
    int x ,y ,step ,i;

    que.push(start) ;
    visit[start.x][start.y] = true ;

    while(!que.empty()) {

         coord now = que.front() ;
         que.pop() ;
         //找到返回结果
         if(now.x == end.x && now.y == end.y) {
              return now.step ;
         }

         for(i = 0 ;i < 8 ;i++) {
              //看是否别马腿
              x = now.x + obstacle[i / 2][0] ;
              y = now.y + obstacle[i / 2][1] ;
              if(x < p && x > -1 && y < q && y > -1 && map[x][y] == true) {
                   x = now.x + dir[i][0] ;
                   y = now.y + dir[i][1] ;
                   step = now.step + 1 ;
                   if(x < p && x > -1 && y < q && y > -1 && map[x][y] == true && visit[x][y] == false) {
                       coord temp ;
                       temp.x = x ;
                       temp.y = y ;
                       temp.step = step ;
                       que.push(temp) ;
                       visit[x][y] = true ;
                   }
              }
         }
    }
    return -1 ;
}

void init(int p ,int q) {
    for(int i = 0 ;i < p ;i++) {
        for(int j = 0 ;j < q ;j++) {
            visit[i][j] = false ;
            map[i][j] = true ;
        }
    }
}

void input_wrok () {
    int n ,m ,obstacle_x ,obstacle_y ,ans;
    coord start ,end ;
    scanf("%d" ,&n) ;
    while(n--) {
        scanf("%d %d" ,&p ,&q) ;
        init(p ,q) ;
        scanf("%d %d %d %d" ,&start.x ,&start.y ,&end.x ,&end.y) ;
        start.x-- ;
        start.y-- ;
        end.x-- ;
        end.y-- ;
        scanf("%d" ,&m) ;
        while(m--) {
            scanf("%d %d" ,&obstacle_x ,&obstacle_y) ;
            map[obstacle_x - 1][obstacle_y - 1] = false ;
        }
        start.step = 0 ;
        ans = bfs(start ,end) ;
        if(ans == -1)
            printf("can not reach!\n") ;
        else
            printf("%d\n" ,ans) ;
    }
}

int main() {
    input_wrok() ;
    return 0 ;
}
posted on 2012-09-25 20:41  小墨在努力!!  阅读(248)  评论(0编辑  收藏  举报