首页 > 其他分享 >poj2243 Knight Moves--A*

poj2243 Knight Moves--A*

时间:2022-12-06 20:02:36浏览次数:77  
标签:&& include return cur -- Knight int nex Moves


原题链接:​​http://poj.org/problem?id=2243​


题意:给定一个起始点和目标点,求按照“日”字走法,最少的步骤。


#define _CRT_SECURE_NO_DEPRECATE 

#include<iostream>
#include<vector>
#include<cstring>
#include<queue>
#include<stack>
#include<algorithm>
#include<cmath>
#define INF 99999999
#define eps 0.0001
using namespace std;

struct Node
{
int x, y, step;
int f, g, h;
bool operator < (const Node & node)const//末尾要加const
{
return f > node.f;
}
};

int dir[8][2] = { { -2,-1 },{ -2,1 },{ 2,-1 },{ 2,1 },{ -1,-2 },{ -1,2 },{ 1,-2 },{ 1,2 } };
int sx, sy;
int dx, dy;
bool vis[10][10];

int getH(const Node & node)
{
return (abs(sx - node.x) + abs(sy - node.y)) * 10;
}

int aStar(int x, int y)
{
priority_queue<Node> Q;
Node cur, nex;
vis[x][y] = 1;
cur.x = x;
cur.y = y;
cur.step = 0;
cur.g = 0;
cur.h = getH(cur);
cur.f = cur.g + cur.h;
Q.push(cur);
while (!Q.empty())
{
cur = Q.top();
Q.pop();
if (cur.x == dx&&cur.y == dy)
return cur.step;

for (int i = 0; i < 8; i++)
{
nex.x = dir[i][0] + cur.x;
nex.y = dir[i][1] + cur.y;
//if (nex.x >= 1 && nex.x <= 8 && nex.y >= 1 && nex.y <= 7 && !vis[nex.x][nex.y])
if (nex.x >= 1 && nex.x <= 8 && nex.y >= 1 && nex.y <= 8 && !vis[nex.x][nex.y])
{
vis[nex.x][nex.y] = 1;
nex.step = cur.step + 1;
nex.g = cur.g + 23;
nex.h = getH(nex);
nex.f = nex.g + nex.h;
Q.push(nex);
}
}
}
return -1;
}
int main()
{
char ch1, ch2;
int t1, t2;
while (~scanf("%c%d %c%d", &ch1, &t1, &ch2, &t2))
{
getchar();
sx = t1;
sy = ch1 - 'a' + 1;
dx = t2;
dy = ch2 - 'a' + 1;

memset(vis, 0, sizeof(vis));
printf("To get from %c%d to %c%d takes %d knight moves.\n", ch1, t1, ch2, t2, aStar(sx, sy));
}
return 0;
}





标签:&&,include,return,cur,--,Knight,int,nex,Moves
From: https://blog.51cto.com/u_11937443/5916614

相关文章

  • 最爱的依然是你——Vim
    当我刚刚开始用vi文本编辑器的时候,我憎恨它!我认为这是有史以来设计上最痛苦和反人类的编辑器。但我还是决定我必须学会它,因为如果你使用的是Unix,vi无处不在并且是唯......
  • poj3074 Sudoku--舞蹈链数独
    原题链接:​​http://poj.org/problem?id=3074​​题意:给定一个9*9的数独,求解。#define_CRT_SECURE_NO_DEPRECATE#include<iostream>#include<vector>#include<cstring>#in......
  • LEEP节点无线链路质量评估实验
    一、实验目的理解节点通信链路质量的影响因素理解提升链路质量的一般方法学习TinyOS系统中CC2530/CC2538节点发送功率的设置体验不同发送功率、不同通信距离、不......
  • Serverless元测试与它的冷启动
    Serverless元测试与它的冷启动坚持原创,写好每一篇文章Serverless的单元测试问你个问题,你平时会写写单元测试吗?你知道如果对一个Serverless应用进行单元测试需要注意什......
  • 第七次打靶
    靶机介绍1)靶机地址:https://download.vulnhub.com/admx/AdmX_new.7z2)靶机难度:中3)打靶目标:取得2个flag+root权限4)涉及攻击方法:主机发现、端口扫描、WEB路径爆破......
  • Python (os模块 相对路径使用方法)
    导入os模块importos返回路径path1=os.path.abspath(__file__)print(path1)#当前文件的绝对路径game_folder=os.path.dirname(__file__)print(game_folder)#当前文件的相......
  • Mysql8.0.25安装过程
    步骤一.下载mysql8.0.25步骤二.下载完解压后如下图:  图片中显示可知,并没有exe用来安装,那么请看第三步步骤三.创建一个txt文本文件,将下边的......
  • vue3-watch、watchEffect侦听器
    watch是用来对动态绑定的数据的变化进行监听和操作的一个API。使用格式为:watch(监听的字面量,(新值,旧值)=>{do()},{deep:true}//可选{flush:sync,pre,po......
  • 基础IO
    写在前面现在我们要开始Linux的第二个大模块,基础IO.在这里你将真正的认识到文件究竟是什么,了解到文件系统,更可以解决我们之前一直避而不谈的缓冲区,我们一起来看看吧.文......
  • 【jmeter基本配置及属性介绍】
    一、jmeter切换为中文1、从图像界面,options》chooselanguage》Chinese-----只能临时修改图像界面语言,重启后还会变为英文2、持久化为中文配置:将jmeter安装目录下b......