首页 > 其他分享 >hdu 1372 Knight Moves 骑士的移动 bfs--马走日

hdu 1372 Knight Moves 骑士的移动 bfs--马走日

时间:2023-09-12 12:00:48浏览次数:42  
标签:hdu y2 sta -- Knight int step && include

#include<stdio.h>
 #include<string.h>
 #include<queue>
 using namespace std;
 char ss[3],ee[3];
 int x1,y1,x2,y2;
 struct pos
 {
int x,y,step;
 }sta,end;
 int f[10][10];
 int dir[8][2]={1,2,1,-2,-1,2,-1,-2,2,1,2,-1,-2,1,-2,-1};
 bool fan(int a,int b)
 {
if(a>0 && a<=8 && b>=1 && b<=8 && f[a][b]==0)
return true;
return false;
 }
 int bfs(int a,int b)
 {
int i;
queue<pos>q;
sta.x=a;
sta.y=b;
f[a][b]=1;
sta.step=0;
q.push(sta);
while(!q.empty())
{
memset(f,0,sizeof(f));
sta=q.front();
q.pop();
if(sta.x==x2 && sta.y==y2)
return sta.step;
for(i=0;i<8;i++)
{
end.x=sta.x+dir[i][0];
end.y=sta.y+dir[i][1];
if(fan(end.x,end.y))
{
end.step=sta.step+1;
f[end.x][end.y]=1;
q.push(end);
}
}
}
 }
 int main()
 {
int i,j,k;
while(scanf("%s%s",ss,ee)!=EOF)
{
x1=ss[0]-'a'+1;
y1=ss[1]-'0';
x2=ee[0]-'a'+1;
y2=ee[1]-'0';
int as=bfs(x1,y1);
printf("To get from %s to %s takes %d knight moves.\n",ss,ee,as);
}
 }

标签:hdu,y2,sta,--,Knight,int,step,&&,include
From: https://blog.51cto.com/u_16244339/7444183

相关文章

  • 如何设计一个极简支付系统
    如何设计一个极简支付系统第一步当用户点击“购买”按钮时,会生成一个支付事件(PaymentEvent)并发送到支付服务。第二步支付服务(PaymentService)将支付事件存储在数据库中。第三步单个支付事件可能包含多个支付订单。比如,我们结账时会从多个卖家选择商品。这时,支付服......
  • 对JS alert弹出框中的文本进行分行
    1.在JS中,直接用\n就行测试代码如下: 测试结果如下:2.在MVC中,要用</br>   参考网址:https://stackoverflow.com/questions/1841452/new-line-in-javascript-alert-box ......
  • Redis - 出现ERROR:WRONGTYPE Operation against a key holding the wrong kind of val
    原因:用的方法与redis服务器中存储数据的类型存在冲突。比如:有一个key的数据存储的是list类型的,但使用redis执行数据操作的时候却使用了非list的操作方法。 对一个Redis键执行不兼容的操作,这个错误通常发生在以下情况:1、类型不匹配:试图执行的操作与键存储的数据类型不匹配。例......
  • JAVA Http Basic auth获取token
    本文主要参考:https://www.cnblogs.com/xiaocandou/p/7991927.html应用在获取token时,可以向api管理平台发起一个HTTPPOST请求,内容如下:•请求地址:https://****.com/token•请求内容:grant_type=client_credentials•请求Content-type:application/x-www-form-url......
  • 折半查找
                   ......
  • vue-i18n
    https://kazupon.github.io/vue-i18n/zh/introduction.html开始如果使用模块系统(例如通过vue-cli),则需要导入Vue和VueI18n,然后调用Vue.use(VueI18n)。格式化在某些情况下,你可能希望将翻译呈现为HTML信息而不是静态字符串。在你的网站上动态插入任意HTML可能......
  • C#中Math.Round(指定四舍五入)、Math.Ceiling和Math.Floor的用法
    1.Math.Round:四舍六入五取偶Math.Round(17.475728155339805,2,MidpointRounding.AwayFromZero)=17.48Math.Round(0.0)//0Math.Round(0.1)//0Math.Round(0.2)//0Math.Round(0.3)//0Math.Round(0.4)//0Math.Round(0.5)//0Math.Round(0.6)//1Math.Round(0.7)//1Math.Roun......
  • 系统设计面试终极指南
    我们精心整理了系统设计面试的模版,覆盖了面试中的各种系统设计问题,包含:负载均衡API网关通信协议内容分发网络(CDN)数据库缓存消息队列唯一ID生成器可扩展性高可用性性能安全性容错性和弹性如果你对细节感兴趣,欢迎留言告诉我。【关注公众号:ByteByteGo】 ......
  • 接口未通时,模拟接口返回数据
    调用接口未接通时,可以用Promise.resolve()或者Promise.reject()模拟成功和失败的返回eg:正常写法exportfunctiongetData(){returnrequest({method:'get',url:'xxx'})}模拟成功exportfunctiongetData(){returnresolve({cod......
  • JavaScript中apply, call和bind的区别
    首先要知道,JavaScript中apply,call和bind的作用基本都是一样的,就是用来改变函数执行时的上下文,或者说改变函数的this对象指向在详细了解它们的区别之前,我们先来看一个例子varname="lucky";constobj={name:"martin",say:function()......