Knight Moves
来源:互联网 发布:ipad清理空间软件 编辑:程序博客网 时间:2024/09/21 08:57
Knight Moves
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 60000/30000K (Java/Other)
Total Submission(s) : 2 Accepted Submission(s) : 2
Problem 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
思路:BFS ,访问过的不在访问,用vist[][]标记。
Source
PKU
代码如下:
#include<iostream>#include<queue>#include<string.h>#include <cstdio>using namespace std;int vist[500][500];struct node{int x,y,step;};int dx[8]={-2,-2,-1,-1,1,1,2,2};int dy[8]={-1,1,-2,2,-2,2,-1,1};int l;node start,end;int bfs(){ queue<node>q ; vist[start.x][start.y] = 1; start.step = 0; if(start.x == end.x && start.y == end.y) return 0; q.push(start); node now; while(!q.empty()) { node n = q.front(); q.pop(); for(int i = 0; i < 8; i ++) { now.x = n.x + dx[i]; now.y = n.y + dy[i]; now.step = n.step+1; if(now.x == end.x && now.y == end.y) { return now.step; } else if(now.x >= 0&&now.x<l&&now.y>=0&&now.y<l&&vist[now.x][now.y]==0) { vist[now.x][now.y] = 1; q.push(now); } } } return 0;}int main(){int cases;scanf("%d",&cases);while(cases--){for(int i = 0; i < 500; i ++) { for(int j = 0; j < 500; j ++) { vist[i][j] = 0; } }scanf("%d",&l);scanf("%d %d",&start.x , &start.y);scanf("%d %d",&end.x , &end.y);printf("%d\n",bfs());}}
0 0
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Java反编译工具(Java Decompiler)
- 多维数组做函数参数的表示方法
- 理解FLASH 8中的矩阵变换
- ASP.NET 的亲密朋友之--JavaScript
- ASP常见的安全漏洞
- Knight Moves
- C#——标志枚举示例学习
- 浅析ASP内置组件
- 字典树讲解
- Linux——安装中文输入法
- 函数控制最小化窗口
- 表中的列与现有主键或UNIQUE约束不匹配
- leetcode第一刷_Add Two Numbers
- Java学习笔记2:理解运行例程