这道题曾经写过,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. (转)淘淘商城系列——引用dubbo服务

    http://blog.csdn.net/yerenyuan_pku/article/details/72758663 上文我们一起学习了如何发布一个dubbo服务,本文我就来教大家如何在web工程中 ...

  2. CREATE CONVERSION - 定义一个用户定义的码制转换

    SYNOPSIS CREATE [DEFAULT] CONVERSION name FOR source_encoding TO dest_encoding FROM funcname DESCRIP ...

  3. 在DOS行下设置静态IP

    A.设置静态IP  CMD  netsh  netsh>int  interface>ip  interface ip>set add "本地链接" static ...

  4. CAD参数绘制文字(com接口)

    在CAD设计时,需要绘制文字,用户可以设置设置绘制文字的高度等属性. 主要用到函数说明: _DMxDrawX::DrawText 绘制一个单行文字.详细说明如下: 参数 说明 DOUBLE dPosX ...

  5. VC++检测硬件设备状态

    首先捕捉WM_DEVICECHANGE消息,该消息在usb插拔时均有触发. MFC下 添加消息处理函数afx_msg BOOL OnDeviceChange( UINT nEventType, DWO ...

  6. 第1节 MapReduce入门:11、mapreduce程序的入门-2

    1.5.WordCount示例编写 1.JobMain.java类 package cn.itcast.wordcount; import org.apache.hadoop.conf.Configu ...

  7. JVM优化(中)

    09.垃圾收集器之串行垃圾收集器: 1.-Xms512m 等价于 -XX:InitialHeapSize=512设置JVM初始堆内存大小:-Xmx2048m 等价于 -XX:MaxHeapSize=2 ...

  8. 【牛客小白月赛6】 J 洋灰三角 - 快速幂&逆元&数学

    题目地址:https://www.nowcoder.com/acm/contest/136/J 解法一: 推数学公式求前n项和: 当k=1时,即为等差数列,Sn = n+pn(n−1)/2 当k≠1时 ...

  9. 如何在 CentOS 7 上生成 SSL 证书为 Nginx 加密

    本文首发:开发指南:如何在 CentOS 7 上安装 Nginx Let’s Encrypt 是由 Internet Security Research Group (ISRG) 开发的一个自由.自动 ...

  10. Nginx基础篇(2)- Nginx基本配置文件和变量详解

    Nginx基本配置文件和变量详解 1. 基本配置文件 /etc/nginx/nginx.conf # nginx运行的用户 user nginx; # nginx进程数,建议设置为等于CPU总核心数. ...