POJ 1915Knight Moves
来源:互联网 发布:非农数据图 编辑:程序博客网 时间:2024/06/06 14:12
Knight Moves
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 21322 Accepted: 9901
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可能比较方便。DFS也可以。BFS用普通的C语言实现了这种由这个状态向可能的状态转化,如果不是,再继续转化,直到达到目标。。。的功能,的确很神奇啊。
#include<stdio.h>#include<cstring>#include<queue>#include<string.h>using namespace std;int T,cot,l,i;int dir[8][2]={{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1},{-2,1},{-1,2}};typedef struct { int x; int y; int step; }Node; Node source,target; int visited[400][400];void bfs(){queue<Node> q1;Node temp1,temp2; memset(visited,0,sizeof(visited)); source.step=0; target.step=l*l; q1.push(source); visited[source.x][source.y]=1; while(!q1.empty()) { temp1=q1.front(); q1.pop(); for(i=0;i<8;i++) {temp2.x=temp1.x+dir[i][0]; temp2.y=temp1.y+dir[i][1]; temp2.step=temp1.step+1; if(temp2.x<0||temp2.x>=l||temp2.y<0||temp2.y>=l) {continue;} if(visited[temp2.x][temp2.y]) { continue; } else { visited[temp2.x][temp2.y]=1; q1.push(temp2); if(temp2.x==target.x&&temp2.y==target.y) if(temp2.step<target.step) {target.step=temp2.step; } } } } }int main(){scanf("%d",&T);while(T--){scanf("%d",&l);scanf("%d %d",&source.x,&source.y);scanf("%d %d",&target.x,&target.y); if(source.x==target.x&&source.y==target.y) { printf("0\n");} else {bfs(); printf("%d\n",target.step); } } return 0; }
0 0
- 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
- httpd: Could not reliably determine the server's fully qualified domain name
- 【学习笔记】struts-tag与jstl的异同
- 黑马程序员-java方法
- How to Write Good Requirements
- LeetCode Reorder List
- POJ 1915Knight Moves
- SQL语句
- Lua: 给 Redis 用户的入门指导
- 黑马程序员-抽象类借口
- Android相对布局属性全集
- oracle备忘录(二)--基础知识基本语法
- uva10905
- Avoiding Cross-Site Scripting (XSS) Attacks With AntiXSS in MVC 4
- POJ 2060 Taxi Cab Scheme DAG最小路径覆盖