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 ...
随机推荐
- istio1.0安装
1. istio1.0安装 创建 istio 目录 [root@centos-110 ~]# mkdir istio [root@centos-110 ~]# cd istio 1.1 获取安装包 链 ...
- Centos7快速安装Rancher
通过docker,我们可以快速安装rancher 安装步骤如下:[root@localhost ~]# #run运行,-d后台模式 --restart=always跟随docker启动,-p映射端口, ...
- salt远程实现go编译重启
使用salt实现jenkins发版时,远程对go项目编译重启 1.go 版本1.12 使用go mod 2.设置go镜像 GOPROXY="https://goproxy.io" ...
- Tomcat 的端口被占用的解决办法
在dos下,输入 netstat -ano|findstr 8080 //说明:查看占用8080端口的进程 显示占用端口的进程 taskkill /pid 6856 /f //说明,运行 ...
- SQL Server sp_monitor使用
SQL Server提供了sp_monitor存储过程可以方便我们查看SQL Server性能统计信息,包括CPU/Network/IO,通过这些信息可以对自己的数据库性能状况有一个大致的了解. 下面 ...
- [转帖]Zookeeper入门看这篇就够了
Zookeeper入门看这篇就够了 https://my.oschina.net/u/3796575/blog/1845035 Zookeeper是什么 官方文档上这么解释zookeeper,它是一个 ...
- Python列表添加元素
Python列表添加元素 1.appent() 在列表尾部添加一个元素 >>>my_list.append("append方法") >>>my_ ...
- SAS学习笔记1
数据采样 简单随机抽样,从sashelp数据集中air数据文件中选取30个数 数据探索 数字特征的探索:均值.频数.最大值.最小值.众数.中位数.方差.标准差 数字分布的探索:是否服从正态分布 连续型 ...
- es常用操作
1.查看所有索引 _cat/indices?v 2.删除索引 DELETE my_index 3.查询缓存 curl /my_index/_search?request_cache=true' -d' ...
- win7安装镜像注入USB3.0,NVMe驱动
现在的新款主板和笔记本因为原生自带了USB3.0和NVMe,在安装WIN7的时候会出现进入安装界面后不识别USB设备且在硬盘列表中无法读取M.2类型的固态硬盘信息.导致这个现象的原因就是在WIN7安装 ...