POJ-1915 Knight Moves

来源:互联网 发布:舔女生尿道口 知乎 编辑:程序博客网 时间:2024/05/17 01:24
Knight Moves
Time Limit: 1000MS Memory Limit: 30000K 

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.

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.

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
————————————————————集训4.2的分割线————————————————————

前言:POJ-2243(ZOJ-1091)和这道题基本是一样的,但是棋盘的大小在2243中固定为8×8,这道题是n <= 300,所以(也许)就必须使用双向BFS了。但是我2243也是用的双向广搜……

还有一个AOJ-510,棋盘大小是20 000 000 × 20 000 000,图是根本存不下来了,但是设想两个点距离很远很远,可以先贪心到一个范围内再进行搜索。

思路:双向广搜可以减少数倍的空间使用量,(尽管时间上不一定占优势)。关键在于同时对两个队列进行扩展,vis数组有三个值来表示:0-没走过,1-q1走过,2-q2走过。当一个队列走到另一个队列走过的点的时候,起点和终点的骑士相遇得到解。写两遍bfs,用一个数组his来储存曾经走到这个点上的时候用到的步数,相遇就直接加上去返回,走过的不能走,没走过就更新his。

代码如下:

/*ID: j.sure.1PROG: LANG: C++*//****************************************/#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <cmath>#include <stack>#include <queue>#include <vector>#include <map>#include <string>#include <iostream>using namespace std;/****************************************/#define LIM (nx >= 0 && nx < n && ny >= 0 && ny < n)int sx, sy, ex, ey, n;const int N = 305;char vis[N][N];int his[N][N];const int d[8][2] = {{-2, -1}, {-2, 1}, {-1, 2}, {1, 2}, {2, 1}, {2, -1}, {1, -2}, {-1, -2}};struct Node{int x, y, step;};queue <Node> qs, qe;int bfs(){while(!qs.empty() || !qe.empty()) {Node ts, te;if(!qs.empty()) {ts = qs.front(); qs.pop();for(int dd = 0; dd < 8; dd++) {int nx = ts.x + d[dd][0], ny = ts.y + d[dd][1];if(LIM && vis[nx][ny] != 1) {if(vis[nx][ny] != 0)return ts.step + 1 + his[nx][ny];else {Node ns;ns.step = ts.step + 1;ns.x = nx; ns.y = ny;his[nx][ny] = ns.step;vis[nx][ny] = 1;qs.push(ns);}}}}if(!qe.empty()) {te = qe.front(); qe.pop();for(int dd = 0; dd < 8; dd++) {int nx = te.x + d[dd][0], ny = te.y + d[dd][1];if(LIM && vis[nx][ny] != 2) {if(vis[nx][ny] != 0)return te.step + his[nx][ny] + 1;else {Node ne;ne.step = te.step + 1;ne.x = nx; ne.y = ny;his[nx][ny] = ne.step;vis[nx][ny] = 2;qe.push(ne);}}}}}return -1;}int main(){#ifndef ONLINE_JUDGEfreopen("1915.in", "r", stdin);freopen("1915.out", "w", stdout);#endifint T;scanf("%d", &T);while(T--) {scanf("%d", &n);scanf("%d%d%d%d", &sx, &sy, &ex, &ey);if(sx == ex && sy == ey)printf("0\n");else {memset(vis, 0, sizeof(vis));memset(his, 0, sizeof(his));while(!qs.empty()) qs.pop();while(!qe.empty()) qe.pop();Node st, et;st.x = sx; st.y = sy; st.step = 0;et.x = ex; et.y = ey; et.step = 0;qs.push(st); qe.push(et);vis[sx][sy] = 1; vis[ex][ey] = 2;printf("%d\n", bfs());}}return 0;}


0 0
原创粉丝点击