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 更优. 读入 这个读入处 ...
随机推荐
- idea 一些设置
idea 中编码格式:VM options 中输入:-Dfile.Encoding=UTF-8 添加tomcat 当+号中没有tomcat时,先看有没有tomcat插件,没有则进行添加,然后 ...
- LINUX:解压问题tar: Child returned status
解压某个文件时 #tar -zxvf xxxxx.tar.gz 出现下面的错误提示: gzip: stdin: not in gzip formattar: Child returned status ...
- Learning Discriminative and Transformation Covariant Local Feature Detectors实验环境搭建详细过程
依赖项: Python 3.4.3 tensorflow>1.0.0, tqdm, cv2, exifread, skimage, glob 1.安装tensorflow:https://www ...
- 谷歌应用商店chrome扩展程序和APP的发布流程
互联网上有很多大牛,他们再工作中需要一些难题,再找到解决办法后,如果会使用js的话,大多数人就可以自己动手写一个chrome插件,而且非常容易.开发人员都喜欢与大家分享自己的成就!google是一个全 ...
- 去面试Python工程师,这几个基础问题一定要能回答,Python面试题No4
今天的面试题以基础为主,去面试Python工程师,这几个基础问题不能答错 第1题:列表和元组有什么不同? 列表和元组是Python中最常用的两种数据结构,字典是第三种. 相同点: 都是序列 都可以存储 ...
- Python之模块和包导入
Python之模块和包导入 模块导入: 1.创建名称空间,用来存放模块XX.py中定义的名字 2.基于创建的名称空间来执行XX.py. 3.创建名字XX.py指向该名称空间,XX.名字的操作,都是以X ...
- 【Codeforces 1031C】Cram Time
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 如果找到最大的n使得1+2+...+n<=a+b 然后第一天输出1,2.3,4....t1 这里1+2+..+t1<=a 这还远远 ...
- 关于Filter中ServletRequest和ServletResponse强转HttpServletRequest和HttpServletResponse
---转载自:https://www.cnblogs.com/mei0619/p/8341159.html request对象的生成方式不是ServletRequest request = new S ...
- shit IE & no table `border-collapse: collapse;`
shit IE no table border-collapse: collapse; /* IE & shit table & border-collapse: collapse; ...
- MTK TP手势添加
old: #include "tpd.h" #include "tpd_custom_gt9xx.h" #ifndef TPD_NO_GPIO #include ...