UVA 439(p177)----Knight Moves
2016-02-25 22:55
901 查看
#include<iostream> #include<cstdio> #include<cstring> const int dx[8] = {-2,-2,-1,-1,1,1,2,2}; const int dy[8] = {-1,1,-2,2,-2,2,-1,1}; typedef struct { int x,y,st; } point; point q[10000]; char st[10],en[10]; int v[10][10]; using namespace std; void solve(int xx,int yy,int xxx,int yyy) { int head=0,tail=1; q[1].x=xx; q[1].y=yy; q[1].st=0; v[xx][yy]=true; while(head<=tail) { head++; for(int i=0; i<8; i++) { int x=q[head].x+dx[i]; int y=q[head].y+dy[i]; //cout<<x<<" "<<y<<endl; // cout<<x<<" "<<y<<endl; if(!v[x][y]&&x>=1&&x<=8&&y>=1&&y<=8) { tail++; q[tail].x=x; q[tail].y=y; q[tail].st=q[head].st+1; v[x][y]=true; } if(x==xxx&&y==yyy) { printf("To get from %s to %s takes %d knight moves.\n",st,en,q[tail].st); return; } } } } int main() { while(scanf("%s%s",st,en)!=EOF) { int x,y,xx,yy; memset(q,0,sizeof(q)); memset(v,0,sizeof(v)); x=st[1]-'0'; y=st[0]-'a'+1; xx=en[1]-'0'; yy=en[0]-'a'+1; // cout<<x<<" "<<y<<" "<<xx<<" "<<yy<<endl; if(x==xx&&y==yy) printf("To get from %s to %s takes %d knight moves.\n",st,en,0); else solve(x,y,xx,yy); } return 0; }题目地址:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=6&page=show_problem&problem=380
相关文章推荐
- Servlet_02_浏览器访问Servlet
- Lazarus 1.44升级到1.6 UTF8处理发生变化了
- 非参数检验
- Web应用工作原理、动态网页技术
- [Lintcode]Reverse Words in a String
- Simulated Annealing
- 纯洁和智慧来自于努力,无知和纵欲来自懒惰
- RHEL7.1安装VNC
- UVA 437(p269)----The Tower of Babylon
- 4.3循环语句
- 关于Android设备中,网页端上传操作导致浏览器刷新的问题
- javascript在IE下遇到的中文乱码与缓存问题
- 字段数据修改历史框架
- 诅咒JavaScript之:Jquery ajax提交内容异常
- ArcGIS的许可文件问题
- tslib移植笔记(1)
- ArcGIS二次开发之读取遥感图像像素值的做法
- 工作小记-如何发送UDP组播
- swift基本语法(总结提炼版)之010 swift 之函数
- spring的学习小结6_bean的生命周期