uva 439 Knight Moves 骑士移动
这道题曾经写过,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 骑士移动的更多相关文章
- UVA 439 Knight Moves(BFS)
Knight Moves option=com_onlinejudge&Itemid=8&category=11&page=show_problem&problem=3 ...
- UVA 439 Knight Moves --DFS or BFS
简单搜索,我这里用的是dfs,由于棋盘只有8x8这么大,于是想到dfs应该可以过,后来由于边界的问题,TLE了,改了边界才AC. 这道题的收获就是知道了有些时候dfs没有特定的边界的时候要自己设置一个 ...
- UVA 439 Knight Moves
// 题意:输入标准国际象棋棋盘上的两个格子,求马最少需要多少步从起点跳到终点 BFS求最短路: bfs并维护距离状态cnt, vis记录是否访问过 #include<cstdio> ...
- 【UVa】439 Knight Moves(dfs)
题目 题目 分析 没有估价函数的IDA...... 代码 #include <cstdio> #include <cstring> #include <a ...
- Knight Moves UVA - 439
A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the sh ...
- UVa 439骑士的移动(BFS)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA Knight Moves
题目例如以下: Knight Moves A friend of you is doing research on the Traveling Knight Problem (TKP) where ...
- UVA439 骑士的移动 Knight Moves
#include<bits/stdc++.h> using namespace std; char a,c; int b,d; ][]; ]={,,,-,,-,-,-}; ]={,-,,, ...
- 题解 UVA439 骑士的移动 Knight Moves
前言 最近板子题刷多了-- 题意 一个 \(8\times 8\) 的棋盘,问马从起点到终点的最短步数为多少. \(\sf Solution\) 要求最短路径嘛,显然 bfs 更优. 读入 这个读入处 ...
随机推荐
- python3爬取微博评论并存为xlsx
python3爬取微博评论并存为xlsx**由于微博电脑端的网页版页面比较复杂,我们可以访问手机端的微博网站,网址为:https://m.weibo.cn/一.访问微博网站,找到热门推荐链接我们打开微 ...
- Okhttp3发送xml、json、文件的请求方法
1.引入依赖 <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okht ...
- clusterdb - 对一个PostgreSQL数据库进行建簇
SYNOPSIS clusterdb [ connection-option...] [ --table | -t table] [ dbname] clusterdb [ connection-op ...
- JavaSE-13 内部类
学习要点 内部类的定义 内部类的应用 内部类 定义 Java的一个类中包含着另一类. A类和B类是C类的外部类.B类是C类的外部类.A类也称为顶层类. 如何使用内部类 public class MyF ...
- JavaSE-08 封装
学习要点 封装 访问控制符 包 封装 没有封装的代码有何缺陷? 例如:对狗狗的健康值赋值为-100.如何避免?——使用封装. 封装的概念 将类的某些信息隐藏在类内部,不允许外部程序直接访问,而是通过该 ...
- vue解决IOS10低版本白屏问题
一.方案一 在build文件的webpack.prod.conf.js文件添加以下代码 new UglifyJsPlugin({ uglifyOptions: { compress: { warnin ...
- MySQL 日志初探
目录 MySQL 日志初探 零.概述 一.Error Log(错误日志) 二.General Query Log(通用查询日志) 三.Slow Query Log (慢查询日志) 四.Binary L ...
- 第二次:Ubuntu16.04 安装Docker
sudo apt-get update, 就这一个命令执行了多半天,不知道网络缘故还是怎么的,管他呢,装完总是好的. # step 1: 安装必要的一些系统工具 sudo apt-get update ...
- Windows下Eclipse+PyDev安装Python开发环境
.简介 Eclipse是一款基于Java的可扩展开发平台.其官方下载中包括J2EE方向版本.Java方向版本.C/C++方向版本.移动应用方向版本等诸多版本.除此之外,Eclipse还可以通过安装插件 ...
- PHP:验证手机号码合法性
文章来源:http://www.cnblogs.com/hello-tl/p/7592333.html /** * [verifyPhone description] 效验手机号码合法性 * @par ...