hrbust 1181 移动【邻接阵,bfs】



#include<stdio.h> #include<string.h> #define INF 0xfffffff struct point { int x; int y; }; int step[301][301]; struct point que[30000001]; int main() { struct point p1, p2; while(scanf("%d %d %d %d", &p1.x, &p1.y, &p2.x, &p2.y) != EOF) { // memset(step, 0, sizeof(step)); for(int i = 0; i <= 300; i++) { for(int j = 0; j <= 300; j++) { step[i][j] = INF; } } step[p1.x][p1.y] = 0; struct point *qb, *qe; qb = qe = que; qe->x = p1.x; qe->y = p1.y; qe++; while(qb < qe) { struct point *tmp = qb++; if(tmp->x + 2 >= 0 && tmp->x + 2 <= 300 && tmp->y - 1 >= 0 && tmp->y - 1 <= 300 && step[tmp->x][tmp->y]+1 < step[tmp->x + 2][tmp->y - 1]) { step[tmp->x + 2][tmp->y - 1] = step[tmp->x][tmp->y] + 1; qe->x = tmp->x + 2; qe->y = tmp->y - 1; qe++; } if(tmp->x + 2 >= 0 && tmp->x + 2 <= 300 && tmp->y + 1 >= 0 && tmp->y + 1 <= 300 && step[tmp->x][tmp->y]+1 < step[tmp->x + 2][tmp->y + 1]) { step[tmp->x + 2][tmp->y + 1] = step[tmp->x][tmp->y] + 1; qe->x = tmp->x + 2; qe->y = tmp->y + 1; qe++; } if(tmp->x - 2 >= 0 && tmp->x - 2 <= 300 && tmp->y - 1 >= 0 && tmp->y - 1 <= 300 && step[tmp->x][tmp->y]+1 < step[tmp->x - 2][tmp->y - 1]) { step[tmp->x - 2][tmp->y - 1] = step[tmp->x][tmp->y] + 1; qe->x = tmp->x - 2; qe->y = tmp->y - 1; qe++; } if(tmp->x - 2 >= 0 && tmp->x - 2 <= 300 && tmp->y + 1 >= 0 && tmp->y + 1 <= 300 && step[tmp->x][tmp->y]+1 < step[tmp->x - 2][tmp->y + 1]) { step[tmp->x - 2][tmp->y + 1] = step[tmp->x][tmp->y] + 1; qe->x = tmp->x - 2; qe->y = tmp->y + 1; qe++; } if(tmp->x + 1 >= 0 && tmp->x + 1 <= 300 && tmp->y + 2 >= 0 && tmp->y + 2 <= 300 && step[tmp->x][tmp->y]+1 < step[tmp->x + 1][tmp->y + 2]) { step[tmp->x + 1][tmp->y + 2] = step[tmp->x][tmp->y] + 1; qe->x = tmp->x + 1; qe->y = tmp->y + 2; qe++; } if(tmp->x - 1 >= 0 && tmp->x - 1 <= 300 && tmp->y + 2 >= 0 && tmp->y + 2 <= 300 && step[tmp->x][tmp->y]+1 < step[tmp->x - 1][tmp->y + 2]) { step[tmp->x - 1][tmp->y + 2] = step[tmp->x][tmp->y] + 1; qe->x = tmp->x - 1; qe->y = tmp->y + 2; qe++; } if(tmp->x + 1 >= 0 && tmp->x + 1 <= 300 && tmp->y - 2 >= 0 && tmp->y - 2 <= 300 && step[tmp->x][tmp->y]+1 < step[tmp->x + 1][tmp->y - 2]) { step[tmp->x + 1][tmp->y - 2] = step[tmp->x][tmp->y] + 1; qe->x = tmp->x + 1; qe->y = tmp->y - 2; qe++; } if(tmp->x - 1 >= 0 && tmp->x - 1 <= 300 && tmp->y - 2 >= 0 && tmp->y - 2 <= 300 && step[tmp->x][tmp->y]+1 < step[tmp->x - 1][tmp->y - 2]) { step[tmp->x - 1][tmp->y - 2] = step[tmp->x][tmp->y] + 1; qe->x = tmp->x - 1; qe->y = tmp->y - 2; qe++; } } printf("%d\n", step[p2.x][p2.y]); } return 0; }

  题目连接:http://acm.hrbust.edu.cn/index.php?m=Status&a=showCode&run_id=44125

posted @ 2012-10-31 23:36  小猴子、  阅读(363)  评论(2编辑  收藏  举报