【POJ 1915】Knight Moves
来源:互联网 发布:linux 网站密码破解 编辑:程序博客网 时间:2024/06/16 03:42
问题描述
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.
输入
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.
输出
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.
样例输入
3
8
0 0
7 0
100
0 0
30 50
10
1 1
1 1
样例输出
5
28
0
算法讨论
就是一个广搜,要注意的是它的编号是0到n-1的。
#include<cstdio>#include<cstring>using namespace std;int dx[9]={0,-1,-2,-2,-1,1,2,2,1};int dy[9]={0,-2,-1,1,2,2,1,-1,-2};int state[100000][3];bool v[305][305];int n,t,sx,sy,lx,ly,ans;bool check(int x,int y){ if ((x<0)||(x>=n)||(y<0)||(y>=n)||(v[x][y])) return true; return false;}void bfs(){ int hd,x,y=0; int tl=1; state[1][1]=sx; state[1][2]=sy; state[1][3]=0; v[sx][sy]=true; while (hd!=tl) { hd++; for (int i=1;i<=8;i++) { x=state[hd][1]+dx[i]; y=state[hd][2]+dy[i]; if (check(x,y)) continue; tl++; v[x][y]=true; state[tl][1]=x; state[tl][2]=y; state[tl][3]=state[hd][3]+1; if ((x==lx)&&(y==ly)) ans=state[tl][3]; } }}int main(){ scanf("%d",&t); for (int k=1;k<=t;k++) { memset(v,0,sizeof(v)); memset(state,0,sizeof(state)); scanf("%d%d%d%d%d",&n,&sx,&sy,&lx,&ly); if ((sx==lx)&&(sy==ly)) { printf("%d\n",0); continue; } ans=0; bfs(); printf("%d\n",ans); } return 0;}
Pixiv ID:58073708
- poj 1915 Knight Moves
- poj 1915 Knight Moves
- poj 1915 Knight Moves
- POJ 1915 Knight Moves
- poj 1915 Knight Moves
- POJ 1915 Knight Moves
- POJ 1915 Knight Moves
- POJ 1915 Knight Moves
- POJ 1915 Knight Moves
- poj 1915 Knight Moves
- poj 1915 Knight Moves
- poj 1915Knight moves
- POJ 1915 Knight Moves
- POJ 1915Knight Moves
- POJ 1915: Knight Moves
- POJ 1915 Knight Moves
- POJ 1915 Knight Moves
- POJ 1915 Knight Moves
- 通过Eclipse(CDT)使用OpenCV
- 毕业了,好久没有更新,,,
- $(document).ready(function(){}),$().ready(function(){})和$(function(){})三个有区别么
- poj3617 生成字典序最小字符串
- BZOJ4384: [POI2015]Trzy wieże 记忆化搜索
- 【POJ 1915】Knight Moves
- SpringMVC 配置定时执行任务
- Linux实战问题解决方案(1):Could not get lock
- 自己加密并解密数据
- 今天新学,通过Nutz来对数据库表格进行增删改查。更加简便。
- jvm原理
- 串行和并行与异步和同步的思考
- 629. K Inverse Pairs Array 自制答案
- HTML/CSS快速入门与提高