HDU 1372 Knight Moves
来源:互联网 发布:sublime 知乎 编辑:程序博客网 时间:2024/04/30 16:10
题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1372
这道题目是关于中国象棋的,问中国象棋里面马从一个地方跳到另一个地方最少需要多少步。
我们知道在正常范围内,马可以向8个方向跳。
简单搜索,BFS
题目代码:(代码每一步很清楚)
#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <cstring>#include <string>#include <algorithm>#include <vector>#include <set>#include <map>#include <queue>using namespace std;/*freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);*/int bx,by,ex,ey;bool visit[10][10];//标记是否走过int t[8][2]={-2,-1,-2,1,-1,2,1,2,2,1,2,-1,1,-2,-1,-2};//8个方向struct xh{ int x,y,s;}w,ww;bool test(int x,int y)//判断{ return x>=1&&x<=8&&y>=1&&y<=8&&visit[x][y];}int bfs(){ queue<xh>q; memset(visit,true,sizeof(visit)); w.x=bx; w.y=by; w.s=0; visit[bx][by]=false; q.push(w); while(!q.empty()) { ww=q.front(); q.pop(); if(ww.x==ex&&ww.y==ey) return ww.s; for(int i=0;i<8;i++) { w=ww; w.x+=t[i][0]; w.y+=t[i][1]; if(test(w.x,w.y)) { visit[w.x][w.y]=false; w.s++; q.push(w); } } }}int main(){ int i,j; char s1[5],s2[5]; while(cin>>s1>>s2) { bx=s1[0]-'a'+1; by=s1[1]-'0'; ex=s2[0]-'a'+1; ey=s2[1]-'0'; printf("To get from %s to %s takes ",s1,s2); if(s1==s2) printf("0"); else printf("%d",bfs()); printf(" knight moves.\n"); } return 520;}
- HDU 1372 knight moves
- HDU 1372 Knight Moves
- HDU-1372-Knight Moves
- hdu 1372 Knight Moves
- HDU 1372 Knight Moves
- hdu 1372 Knight Moves
- Hdu 1372 Knight Moves
- HDU 1372 Knight Moves
- HDU 1372 Knight Moves
- HDU-1372Knight Moves
- HDU-1372-Knight Moves
- hdu 1372 Knight Moves
- hdu-1372-Knight Moves
- HDU 1372 Knight Moves
- HDU-1372-knight moves
- hdu(1372) Knight Moves
- hdu 1372 Knight Moves
- hdu 1372 Knight Moves
- .TK免费顶级域名绑定“点点网”详细图文教程
- B.原根
- java基础语法学习
- ps 图层 缩小
- java面试
- HDU 1372 Knight Moves
- C# 学习笔记 数据库 Entity Framework (书:C#与.NET4 第23章)
- TestLink(1.8.4版本)安装及使用说明
- table函数
- Android不生成R文件的一个原因
- Asp.Net底层解析(四)——应用程序生命周期与HttpModule
- 《代码整洁之道》--注释
- java-rpc介绍
- Microsoft 数据访问技术简介