描述问题
输入测试例子数T,每个例子输入棋盘大小m行n列, (1<=m,n<=500),再输入a,b,c,d表示(a,b)–>(c,d),(1<=a,c<=m)且(1<=b,d<=n)且两点不是同一点,若马走的到,输出按照马的走法(日字走法)的 最短路径的步数,否则输出0。
分析问题
最短路径,让人联想到bfs,于是我们可以用队列,若未访问过,将可走的点放入队列中,直到队列为空之前,若走到则输出最短路径所走的步数,若队列已经空而仍然未到达目标,则输出0;
代码 + 注释
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int x;
int y;
int len;
}node;
typedef node* Node;
int main() {
int test_num;
scanf("%d",&test_num);
while(test_num --) {
int m , n;
scanf("%d %d",&m,&n);
int **mark = (int **)calloc((m+1),sizeof(int *));
for(int i = 0;i < m + 1;i ++) {
mark[i] = (int *)calloc((n+1),sizeof(int));
}
int a,b,c,d;
scanf("%d %d %d %d",&a,&b,&c,&d);
mark[a][b] = 1;
int move_x[] = {-2,-1,1,2,2,1,-1,-2};
int move_y[] = {1,2,2,1,-1,-2,-2,-1};
Node queue = (Node)calloc(m*n+1,sizeof(node));
int head = 0,tail = 1;
queue[1].x = a;
queue[1].y = b;
queue[1].len = 0;
int tag = 0;
while(head <= tail) {
head ++;
for(int i = 0;i < 8;i ++) {
if(queue[head].x + move_x[i] > 0
&& queue[head].y + move_y[i] > 0
&& mark[queue[head].x + move_x[i]][queue[head].y + move_y[i]] == 0) {
tail ++;
queue[tail].x = queue[head].x + move_x[i];
queue[tail].y = queue[head].y + move_y[i];
mark[queue[tail].x][queue[tail].y] = 1;
queue[tail].len = queue[head].len + 1;
if(queue[tail].x == c && queue[tail].y == d) {
printf("%d\n",queue[tail].len);
tag = 1;
break;
}
}
}
if(tag) break;
}
if(tag == 0) printf("0\n");
for(int i = 0;i < m + 1;i ++) {
free(mark[i]);
}
free(mark);
free(queue);
}
return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)