zoukankan      html  css  js  c++  java
  • uva439 Knight Moves 国际象棋的跳马



     Knight Moves 

    A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knight moves that visits each square of a given set of n squares on a chessboard exactly once. He thinks that the most difficult part of the problem is determining the smallest number of knight moves between two given squares and that, once you have accomplished this, finding the tour would be easy.

    Of course you know that it is vice versa. So you offer him to write a program that solves the "difficult" part.

    Your job is to write a program that takes two squares a and b as input and then determines the number of knight moves on a shortest route from a to b.

    Input Specification

    The input file will contain one or more test cases. Each test case consists of one line containing two squares separated by one space. A square is a string consisting of a letter (a-h) representing the column and a digit (1-8) representing the row on the chessboard.

    Output Specification

    For each test case, print one line saying "To get from xx to yy takes n knight moves.".

    Sample Input

    e2 e4
    a1 b2
    b2 c3
    a1 h8
    a1 h7
    h8 a1
    b1 c3
    f6 f6

    Sample Output

    To get from e2 to e4 takes 2 knight moves.
    To get from a1 to b2 takes 4 knight moves.
    To get from b2 to c3 takes 2 knight moves.
    To get from a1 to h8 takes 6 knight moves.
    To get from a1 to h7 takes 5 knight moves.
    To get from h8 to a1 takes 6 knight moves.
    To get from b1 to c3 takes 1 knight moves.
    To get from f6 to f6 takes 0 knight moves.

    经典bfs。。。

    太笨,我是看题解做的。。。

    #include<iostream>
    #include<cstring>
    #include<queue>
    #define MAX 10
    #define WHITE 0
    #define BLACK 1
    using namespace std;
    
    typedef struct
    {
    	int x, y, count;
    }Point;
    
    Point star, end, cur, next;
    int color[MAX][MAX];
    int ck[8][2] = {-1,-2, 1,-2, 2,1, 2,-1, 1,2, -1,2, -2,1, -2,-1};
    
    int bfs(void)
    {
    	memset(color, WHITE, sizeof (color));
    	color[star.x][star.y] = BLACK;
    	star.count = 0;
    	queue<Point> q;
    	q.push(star);
    	while (!q.empty())
    	{
    		cur = q.front();
    		q.pop();
    		if (cur.x == end.x && cur.y == end.y)
    			return cur.count;
    		for (int i = 0; i < 8; i++)
    		{
    			next.x = cur.x + ck[i][0];
    			next.y = cur.y + ck[i][1];
    			if (color[next.x][next.y]  == BLACK)
    				continue;
    			if (next.x < 1 || next.x > 8 || next.y < 1 || next.y > 8)
    				continue;
    			color[next.x][next.y] = BLACK;
    			next.count = cur.count + 1;
    			if (next.x == end.x && next.y == end.y)
    				return next.count;
    			q.push(next);
    		}
    	}
    	return -1;
    }
    
    int main()
    {
    	char st, en;
    	int s, e, step;
    	while (cin >> st >> s >> en >> e)
    	{
    		star.x = st - 'a' + 1;
    		star.y = s;
    		end.x = en - 'a' + 1;
    		end.y = e;
    		if (star.x == end.x && star.y == end.y)
    			step = 0;
    		else
    			step = bfs();
    		cout << "To get from "<< st << s << " to " << en << e << " takes " << step << " knight moves." << endl;
    	}
    	return 0;
    }



  • 相关阅读:
    逆元(费马小定理求法)
    CodeForces
    lower_bound and upper_bound
    HDU 4825 Xor Sum
    1030: [JSOI2007]文本生成器
    1070: [SCOI2007]修车
    agc 027 B
    P2664 树上游戏
    CF 314 E. Sereja and Squares
    4237: 稻草人
  • 原文地址:https://www.cnblogs.com/java20130723/p/3212172.html
Copyright © 2011-2022 走看看