POJ 1915 Knight Moves BFS
来源:互联网 发布:linux 添加系统调用 编辑:程序博客网 时间:2024/06/05 02:58
Description
Background
Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fast. Can you beat him?
The Problem
Your task is to write a program to calculate the minimum number of moves needed for a knight to reach one point from another, so that you have the chance to be faster than Somurolov.
For people not familiar with chess, the possible knight moves are shown in Figure 1.
Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fast. Can you beat him?
The Problem
Your task is to write a program to calculate the minimum number of moves needed for a knight to reach one point from another, so that you have the chance to be faster than Somurolov.
For people not familiar with chess, the possible knight moves are shown in Figure 1.
Input
The input begins with the number n of scenarios on a single line by itself.
Next follow n scenarios. Each scenario consists of three lines containing integer numbers. The first line specifies the length l of a side of the chess board (4 <= l <= 300). The entire board has size l * l. The second and third line contain pair of integers {0, ..., l-1}*{0, ..., l-1} specifying the starting and ending position of the knight on the board. The integers are separated by a single blank. You can assume that the positions are valid positions on the chess board of that scenario.
Next follow n scenarios. Each scenario consists of three lines containing integer numbers. The first line specifies the length l of a side of the chess board (4 <= l <= 300). The entire board has size l * l. The second and third line contain pair of integers {0, ..., l-1}*{0, ..., l-1} specifying the starting and ending position of the knight on the board. The integers are separated by a single blank. You can assume that the positions are valid positions on the chess board of that scenario.
Output
For each scenario of the input you have to calculate the minimal amount of knight moves which are necessary to move from the starting point to the ending point. If starting point and ending point are equal,distance is zero. The distance must be written on a single line.
Sample Input
380 07 01000 030 50101 11 1
Sample Output
5280
题目使用广搜,思路比较简单
对能够跳到的点进行广搜
代码如下:
#include<iostream>#include<queue>#include<string.h>using namespace std;int ans,n,sx,sy,ex,ey,i,l,map[301][301],dir[8][2]={{-2,-1},{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2}};struct fun{ int x,y,step;};int bfs(){ fun a,next; a.x=sx;a.y=sy;a.step=0; map[a.x][a.y]=1; queue<fun>q; q.push(a); while(!q.empty()) { a=q.front(); q.pop(); if(a.x==ex&&a.y==ey){cout<<a.step<<endl;return 0;} for(i=0;i<8;i++) { int x=a.x+dir[i][0]; int y=a.y+dir[i][1]; if(x>=0&&x<l&&y>=0&&y<l&&!map[x][y]) { map[x][y]=1; next.x=x; next.y=y; next.step=a.step+1; q.push(next); } } }}int main(){ cin>>n; while(n--) { memset(map,0,sizeof(map)); cin>>l>>sx>>sy>>ex>>ey; bfs(); }}
阅读全文
0 0
- POJ 1915 Knight Moves bfs
- POJ 1915Knight Moves(BFS)
- poj 1915 Knight Moves(bfs)
- poj 1915 Knight Moves(bfs)
- POJ 1915 BFS-Knight Moves
- [BFS]poj 1915 Knight Moves
- Poj 1915 Knight Moves ( BFS
- POJ 1915 Knight Moves BFS
- 【BFS】poj 1915 Knight Moves
- POJ 1915 Knight Moves bfs || 双向bfs
- poj 1915 knight moves(bfs 简单)
- poj 1915 knight moves (双向bfs)
- POJ 1915 Knight Moves (BFS)
- poj 1915 Knight Moves(BFS遍历)
- Poj 1915 Knight Moves(BFS)
- POJ 1915 Knight Moves(双向BFS)
- POJ 1915 Knight Moves(DFS/BFS)
- POJ 1915 Knight Moves(BFS+STL)
- struts上传文件,报错:java.io.FileNotFoundException:(系统找不到指定的路劲)
- Fdatool生成的IIR系数在Dsplib中的使用方法
- Android Fragment 你应该知道的一切
- 初学JAVA001 框架
- TCP和UDP以及IP报文格式
- POJ 1915 Knight Moves BFS
- 笔记(按back,activity的小知识)
- 分针网—每日分享:网页布局时CSS无效的常见原因
- 【AngularJS】表格的格式<table>
- mysql5.7-主从设置
- 560. Subarray Sum Equals K
- 微信支付流程
- C语言基础-预处理指令25
- 动态内存分配