这道题曾经写过,bfs。用队列,不多说了,上代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<queue>
using namespace std;
int map[10][10];
int visit[10][10];
int dist[10][10];
int dx[8]={-2,-2,-1,-1,1,1,2,2};
int dy[8]={-1,1,-2,2,-2,2,-1,1};
char a[5];
int x2,y2;
struct node
{
int x,y;
};
queue<node>q;
int bfs(node T)
{
if(T.x==x2&&T.y==y2)
{
return dist[T.x][T.y];
}
else
{
while(!q.empty())
{
node m = q.front();
q.pop();
if(m.x==x2&&m.y==y2)
return dist[m.x][m.y];
for(int i=0; i<8; i++)
{ int xx = m.x+dx[i];
int yy = m.y+dy[i];
if(!visit[xx][yy]&&xx>0&&yy>0&&xx<=8&&yy<=8)
{
node n ;
n.x = xx;
n.y = yy;
q.push(n);
dist[xx][yy] = dist[m.x][m.y]+1;
visit[xx][yy] = 1;
}
}
}
}
}
int main()
{
int x1,y1,i,j;
while(gets(a))
{
y1 = a[0]-'a'+1;
x1 = a[1]-'0';
y2 = a[3]-'a'+1;
x2 = a[4]-'0';
//printf("%d %d %d %d\n",x1,y1,x2,y2);
node T;
T.x = x1;
T.y = y1;
memset(dist,0,sizeof(dist));
memset(visit,0,sizeof(visit));
q.push(T);
bfs(T);
printf("To get from %c%c to %c%c takes %d knight moves.\n",a[0],a[1],a[3],a[4],dist[x2][y2]);
while(!q.empty())
{
q.pop();
}
}
return 0;
}

uva 439 Knight Moves 骑士移动的更多相关文章

  1. UVA 439 Knight Moves(BFS)

    Knight Moves option=com_onlinejudge&Itemid=8&category=11&page=show_problem&problem=3 ...

  2. UVA 439 Knight Moves --DFS or BFS

    简单搜索,我这里用的是dfs,由于棋盘只有8x8这么大,于是想到dfs应该可以过,后来由于边界的问题,TLE了,改了边界才AC. 这道题的收获就是知道了有些时候dfs没有特定的边界的时候要自己设置一个 ...

  3. UVA 439 Knight Moves

      // 题意:输入标准国际象棋棋盘上的两个格子,求马最少需要多少步从起点跳到终点 BFS求最短路: bfs并维护距离状态cnt, vis记录是否访问过 #include<cstdio> ...

  4. 【UVa】439 Knight Moves(dfs)

    题目 题目     分析 没有估价函数的IDA......     代码 #include <cstdio> #include <cstring> #include <a ...

  5. Knight Moves UVA - 439

    A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the sh ...

  6. UVa 439骑士的移动(BFS)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  7. UVA Knight Moves

    题目例如以下: Knight Moves  A friend of you is doing research on the Traveling Knight Problem (TKP) where ...

  8. UVA439 骑士的移动 Knight Moves

    #include<bits/stdc++.h> using namespace std; char a,c; int b,d; ][]; ]={,,,-,,-,-,-}; ]={,-,,, ...

  9. 题解 UVA439 骑士的移动 Knight Moves

    前言 最近板子题刷多了-- 题意 一个 \(8\times 8\) 的棋盘,问马从起点到终点的最短步数为多少. \(\sf Solution\) 要求最短路径嘛,显然 bfs 更优. 读入 这个读入处 ...

随机推荐

  1. PHP流程控制考察点

    php遍历数组的三种方法及各自的区别 php遍历数组的方式主要有三种: for循环 foreach循环 while.list().each()组合循环 其中: for循环只能遍历索引数组,foreac ...

  2. 5-Java-C(小题答案)

    1. a[i][j]=a[i-1][j-1]+a[i-1][j] 2.1835421 3.93 4.1572836

  3. 解决docker pull镜像速度慢的问题

    直接下载Docker镜像时,由于种种原因,经常下载失败,即使连接成功也是很慢,怎么办呢 目前我知道可以提升速度的办法:DaoCloud 提供Docker Hub Mirror服务 用户可以进入Dock ...

  4. 四种方案解决ScrollView嵌套ListView问题 [复制链接]

    以下文章转自@安卓泡面 在工作中,曾多次碰到ScrollView嵌套ListView的问题,网上的解决方法有很多种,但是杂而不全.我试过很多种方法,它们各有利弊. 在这里我将会从使用ScrollVie ...

  5. js中sync、defer、async的区别

    <script src="script.js"></script> 没有 defer 或 async,浏览器会默认为同步sync,会立即加载并执行指定的脚本 ...

  6. HDU - 2612 Find a way(BFS搜索)

    题目: 链接 思路: 用BFS分别以‘Y’和‘M’的位置为起点进行两次搜索,并把这两次的搜索结果在一个二维数组中保存下来,在对地图遍历遇到‘@’更行最小值. PS: 如果用‘Y’和‘M’点分别去搜每个 ...

  7. CentOS7-Git安装以及使用

    2018-09-14 Git安装 在bash终端中输入命令sudo yum install git回车. (出乎意料的顺利) 在随后出现的交互式对话中输入y即可. 随后,当任务执行完后,在bash中键 ...

  8. VSCODE插件开发:用户输入输出

    阅读这篇文章之前,假设你已经具有开发helloworld的插件的能力. vscode.window 简介 vscode.window 负责当前激活窗口的输入输出,比如展示信息,和用户输入等功能都是用v ...

  9. dbeaver能执行存储过程,db2命令编辑器里面不行

  10. c:foreach 标签 varStatus的使用

    <c:forEach items="${MedicalDoctoList }" var="medicalDoctor" varStatus="s ...