UVA-439, Knight Moves(深度优先搜索)
#include<iostream>
#include<queue>
#include<cstring>
#include<string>
#include<cstdio>
using namespace std;
struct Point
{
int x_, y_;
int route;
};
int dic[8][2] = {-1,2 ,1,2 ,2,1 ,2,-1 ,1,-2 ,-1,-2 ,-2,-1 ,-2,1};
int vis[10][10]; /*判断是否访问过*/
string fir, sec;
int ans; /*记录最短路*/
int x1, y1, x2, y2;
void dfs()
{
if(x1 == x2 && y1 == y2)
{
ans = 0;
return ;
}
Point p;
p.x_ = x1;
p.y_ = y1;
vis[x1][y1] = 1;
p.route = 0;
queue<Point> rr;
rr.push(p);
while(!rr.empty())
{
// if(x1 == x2 && y1 == y2)
// break;
Point q;
for(int i = 0; i < 8; i++)
{
q.x_ = rr.front().x_ + dic[i][0];
q.y_ = rr.front().y_ + dic[i][1];
q.route = rr.front().route + 1;
if(vis[q.x_][q.y_] == 1 || q.x_ < 0 || q.y_ < 0 || q.x_ > 7 || q.y_ > 7)
continue;
else
{
vis[q.x_][q.y_] = 1;
rr.push(q);
if(q.x_ == x2 && q.y_ == y2)
{
ans = q.route;
break; /*找到了*/
}
}
}
if(q.x_ == x2 && q.y_ == y2)
break;
rr.pop();
}
}
int main()
{
while(cin >> fir >> sec)
{
memset(vis, 0, sizeof(vis));
x1 = (int)fir[0] - 'a';
y1 = (int)fir[1] - '0' - 1;
x2 = (int)sec[0] - 'a';
y2 = (int)sec[1] - '0' - 1;
dfs();
cout << "To get from " << fir << " to " << sec << " takes " << ans << " knight moves." << endl;
}
}
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 骑士移动
这道题曾经写过,bfs.用队列,不多说了,上代码: #include<stdio.h> #include<stdlib.h> #include<string.h> ...
- 【UVa】439 Knight Moves(dfs)
题目 题目 分析 没有估价函数的IDA...... 代码 #include <cstdio> #include <cstring> #include <a ...
- hdu1372 Knight Moves BFS 搜索
简单BFS题目 主要是读懂题意 和中国的象棋中马的走法一样,走日字型,共八个方向 我最初wa在初始化上了....以后多注意... 代码: #include <iostream> #incl ...
- Knight Moves UVA - 439
A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the sh ...
- POJ---2243 Knight Moves 使用A*算法的广度优先搜索
题目链接:http://poj.org/problem?id=2243 启发式搜索:启发式搜索就是在状态空间中的搜索对每一个搜索的位置进行评估,得到最好的位置,再从这个位置进行搜索直到目标.这样可以省 ...
- [宽度优先搜索] HDU 1372 Knight Moves
Knight Moves Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
随机推荐
- python办公自动化(一)PPTX
简介: python-pptx是python处理PPT的一个库,注重的是读和写,无法导出,没有渲染功能. 办公自动化,说的是大了一点,但是最常见的office三件套,word,excel,ppt.这还 ...
- 【ML基础】t-SNE(t-distributed stochastic neighbor embedding)原理及推导
前言 参考 1. t-SNE原理与推导: 完
- Linux下安装配置rocketmq
1.安装jdk,如果系统有原来的系统自带的先删掉,因为很多库不全,自己需要到jdk官网下载包. 卸载CentOS自带的OpenJdk: [root@centos-lx /]# rpm -qa | gr ...
- MySQL报错Packet for query is too large问题解决
今天用java写了批量插入运行时,报错: Error updating database. Cause: com.mysql.cj.jdbc.exceptions.PacketTooBigExcep ...
- ES-PHP向ES批量添加、查询文档报 No alive nodes found in your cluster
转自: https://blog.csdn.net/itfootball/article/details/53637238 问题描述为了提高保存数据到es消耗的时间,采取积攒到3000条文档的时候才保 ...
- 029 SSM综合练习05--数据后台管理系统--订单分页查询
1.PageHelper介绍 PageHelper是国内非常优秀的一款开源的mybatis分页插件,它支持基本主流与常用的数据库,例如mysql.oracle.mariaDB.DB2.SQLite.H ...
- [转帖]很遗憾,没有一篇文章能讲清楚ZooKeeper
很遗憾,没有一篇文章能讲清楚ZooKeeper https://os.51cto.com/art/201911/606571.htm [51CTO.com原创稿件]互联网时代是信息爆发的时代,信息的高 ...
- 基于hystrix的线程池隔离
hystrix进行资源隔离,其实是提供了一个抽象,叫做command,就是说,你如果要把对某一个依赖服务的所有调用请求,全部隔离在同一份资源池内 对这个依赖服务的所有调用请求,全部走这个资源池内的资源 ...
- selenium中元素操作之浏览器窗口滚动&网页日期控件操作(js操作)(五)
js的滚动条scrollIntoView() Arguments[] - python与js之间的羁绊 1.移动到元素element对象的“底端”,与当前窗口的“底部”对齐: driver.execu ...
- django.db.utils.InternalError: (1060, "Duplicate column name 'user_id'")迁移报错解决方法
django.db.utils.InternalError: (1060, "Duplicate column name 'user_id'")迁移报错解决方法 django.db ...