UVA 439 Knight Moves
2016-02-27 18:15
274 查看
题意:8*8的棋盘,给定一个起点和终点,求马至少需要多少步可以从起点跳到终点。
思路:bfs模板。注意坐标判重,判断坐标越界。
思路:bfs模板。注意坐标判重,判断坐标越界。
#include <iostream> #include <cstdio> #include <cstring> #include <queue> using namespace std; #define Clean(x,y) memset(x,y,sizeof(x)) const int dx[8] = {-2,-2,-1,-1,1,1,2,2}; const int dy[8] = {1,-1,2,-2,2,-2,1,-1}; int st_x,st_y,ed_x,ed_y; bool flag[10][10]; char temp[20]; int main() { while( gets(temp) ) { queue<int> step,x,y; st_x = temp[0] - 'a' + 1; ed_x = temp[3] - 'a' + 1; st_y = temp[1] - '0'; ed_y = temp[4] - '0'; Clean(flag,true); flag[st_x][st_y] = false; x.push(st_x); y.push(st_y); step.push(0); int tx,ty,xx,yy,ss; while( !x.empty() ) { xx = x.front(); yy = y.front(); ss = step.front(); x.pop(); y.pop(); step.pop(); if ( xx == ed_x && yy == ed_y ) { printf("To get from %c%c to %c%c takes %d knight moves.\n",temp[0],temp[1],temp[3],temp[4],ss); break; } for(int i = 0; i < 8; i++ ) { tx = xx + dx[i]; ty = yy + dy[i]; if ( tx>=1 && tx<=8 && ty>=1 && ty<=8 && flag[tx][ty] ) { flag[tx][ty] = false; x.push(tx); y.push(ty); step.push(ss+1); } } } } return 0; }
相关文章推荐
- spring学习笔记 - 面试题
- 猴子吃桃子的问题
- FPGA知识大梳理(二)verilogHDL语法入门(1)
- CodeForces 612 D. The Union of k-Segments(水~)
- javascript高级程序设计笔记-第三章(基本概念)
- codeforces 628D 数位dp
- 绘画基础研究(一)-画面的对象组成与形状组成
- 如何获取 yum 安装的 rpm 包位置---详解
- CF 50A Domino piling
- C语言中的类型转换
- 学习《Python核心编程》做一下知识点提要,方便复习(一)
- POJ 2376 Cleaning Shifts (贪心区间最少覆盖)
- Connection timed out: connect. If you are behind an HTTP proxy, please configure the proxy settings
- Android 手势检测实战 打造支持缩放平移的图片预览效果(下)
- iOS图标抖动效果
- ssh服务优化与客户端命令使用
- bzoj1196 公路修建问题
- 【leetcode】Array——Rotate Image(48)
- iOS 隐藏键盘的几种常见方法
- Android绚丽加载效果视图(loading)控件