poj 1915 Knight Moves【BFS】【简单】
2015-11-18 21:25
447 查看
[align=center]Knight Moves[/align]
Description
题目大意:输入N,表示棋盘大小为N*N,在输入两个坐标,分别是起始点与目标点,问从起始点到目标点的最少步数【马的行走规则与国际象棋中的一样】,
Sample Input
Sample Output
已Accept代码【c++提交】
[align=center]Knight Moves[/align]
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 23541 | Accepted: 11059 |
题目大意:输入N,表示棋盘大小为N*N,在输入两个坐标,分别是起始点与目标点,问从起始点到目标点的最少步数【马的行走规则与国际象棋中的一样】,
Sample Input
3 8 0 0 7 0 100 0 0 30 50 10 1 1 1 1
Sample Output
5 28 0
已Accept代码【c++提交】
#include <cstdio> #include <queue> #include <cstring> using namespace std; int dx[] = {1, 1, -1, -1, 2, 2, -2, -2}; int dy[] = {2, -2, 2, -2, 1, -1, 1, -1}; int n; int dis[301][301]; int a, b, x, y; typedef struct node{ int x, y; node() {} node(int x, int y) : x(x), y(y) {} }node; void BFS() { queue <node> Q; memset(dis, 0x3f3f3f3f, sizeof(dis)); Q.push(node(a, b)); dis[a][b] = 0; while(!Q.empty()) { node k = Q.front(); Q.pop(); int r = k.x; int c = k.y; int ok = 0; for(int i = 0; i < 8; i++) { int nx = r + dx[i]; int ny = c + dy[i]; if(nx < 0 || nx >=n || ny < 0 || ny >= n) continue; if(dis[nx][ny] > dis[r][c] + 1) { dis[nx][ny] = dis[r][c] + 1; Q.push(node(nx, ny)); } } } printf("%d\n", dis[x][y]); } int main() { int t; scanf("%d", &t); while(t--) { scanf("%d", &n); scanf("%d%d%d%d", &a, &b, &x, &y); BFS(); } return 0; }
相关文章推荐
- shell入门-grep-3-egrep
- XMLHttpRequest对象的属性
- 【Head First设计模式-读书笔记】策略模式
- MSXML 解析XML文件
- 事件与委托---实现事件分发中心
- 卡尔曼滤波+opencv 实现跟踪人脸 小demo
- 模型
- AJAX 在IE 下提交到后台乱码,但ff不会
- session 讲解
- R绘图-坐标中断的柱状图
- CodeTyphon自带例子源码:限制运行日期
- ubuntu12.04下搭建android开发环境
- 数据库与MATLAB链接http://blog.sina.com.cn/s/blog_9d0b00a401012spy.html
- 如何增大UDP收包效率
- MongoDB 启动mongo不带DB
- c++ dirname() basename()
- 异常分类
- IntelliJ IDEA快捷键note
- 线程锁ReentrantLock和Condition的使用
- 反射的方法操作记事本添加插件