ZOJ_1091-Knight Moves
来源:互联网 发布:Windows测试udp 编辑:程序博客网 时间:2024/04/30 03:19
//这是一道我曾经不会做的题,现在还是能做了,特此记录一下//
AC代码:
#include<stdio.h>#include<string.h>#define max 50char a[max][max];int vis[max][max];int dir[8][2]={{2,1},{-2,1},{2,-1},{-2,-1},{1,2},{1,-2},{-1,2},{-1,-2}};struct node{ int x,y,step;}queue[max*max];char s1[max];char s2[max];int x1,y1,x2,y2;void bfs(int x,int y){ struct node now,pre; queue[0].x=x1; queue[0].y=y1; queue[0].step=0; int e=0,h=1; while(e<h) { pre=queue[e]; if(pre.x==x2&&pre.y==y2) { printf("%d knight moves.\n",pre.step); return; } int i; for(i=0;i<8;i++) { now.x=pre.x+dir[i][0]; now.y=pre.y+dir[i][1]; now.step=pre.step+1; if(now.x>=1&&now.x<=8&&now.y>=1&&now.y<=8&&!vis[now.x][now.y]) { vis[now.x][now.y]=1; queue[h++]=now; } } e++; }}int main(){ while(scanf("%s%s",s1,s2)!=EOF) { memset(vis,0,sizeof(vis)); x1=s1[0]-'a'+1; y1=s1[1]-'0'; x2=s2[0]-'a'+1; y2=s2[1]-'0'; printf("To get from %s to %s takes ",s1,s2); bfs(x1,y1); } return 0;}
0 0
- ZOJ_1091-Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- 【从零开始学NGUI 】 (七)Atlas制作
- 水平凡 创建新的插件
- Android面试---Handler,Looper,MessageQueue
- Html5中的<section>标签
- 加载配置文件
- ZOJ_1091-Knight Moves
- 巡逻机器人(Patrol Roboot,UVa1600)
- Unity3D-client +MinaServer仿微信聊天系统
- Linux基本命令
- 02设计包含min函数的栈
- 如何快速找到下载主机,解决网络拥塞问题。
- Unity3D研究院之C#使用Socket与HTTP连接服务器传输数据包(四十三)
- Maven+SpringMVC整合之helloworld
- 还是回归CSDN吧