zoukankan      html  css  js  c++  java
  • UVA

     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基础题目,刚开始弄成DFS了。。。唉
    #include <bits/stdc++.h>
    using namespace std;
    
    struct Dot{
    	int r;
    	int c;
    	Dot(int r = 0, int c = 0) : r(r), c(c) {};
    };
    
    int table[12][12];
    int cnt;
    Dot enddot;
    bool is_in(Dot & x)
    {
    	int r = x.r;
    	int c = x.c;
    	return r <= 8 && r >= 1 && c <= 8 && c >= 1;
    }
    
    Dot walk(Dot & x, int i)
    {
    	if (i == 0) return Dot(x.r + 1, x.c + 2);
    	if (i == 1) return Dot(x.r + 2, x.c + 1);
    	if (i == 2) return Dot(x.r + 1, x.c - 2);
    	if (i == 3) return Dot(x.r + 2, x.c - 1);
    	if (i == 4) return Dot(x.r - 2, x.c + 1);
    	if (i == 5) return Dot(x.r - 1, x.c + 2);
    	if (i == 6) return Dot(x.r - 2, x.c - 1);
    	if (i == 7) return Dot(x.r - 1, x.c - 2);
    }
    
    void bfs(int r, int c)
    {
    	queue<Dot> qu;
    	qu.push(Dot(r,c));
    	while (!qu.empty())
    	{
    		Dot u = qu.front(); qu.pop();
    		if (u.r==enddot.r && u.c==enddot.c)
    			return ;
    		for (int i = 0; i < 8; i++){
    			Dot v = walk(u, i);
    			if (is_in(v) && table[v.r][v.c] < 0){
    				table[v.r][v.c] = table[u.r][u.c] + 1;
    				qu.push(v);
    			}
    		}
    	}
    }
    
    int main()
    {
    	string b, e;
    	while (memset(table, -1, sizeof(table)), cin >> b >> e)
    	{
    		cnt = 0;
    		int r = b[1] - '0', c = b[0] - 'a' + 1;
    		enddot.r = e[1] - '0', enddot.c = e[0] - 'a' + 1;
    		table[r][c] = 0;
    		bfs(r, c);
    		printf("To get from %s to %s takes %d knight moves.
    ", b.c_str(), e.c_str(), table[enddot.r][enddot.c]);
    	}
    	return 0;
    }


  • 相关阅读:
    [转]UIWebView 监控 XMLHttpRequest
    viewDidMoveToWindow:shouldAppearOrDisappear:
    [转]iAP Cracker for iPhone/iPod/iPad
    关于替换 UIWebView 网络模块的一些初步想法
    iOS & Max_OS_X Debugging Magic
    [转]定制 iOS 键盘
    Disable & Enable xcode Indexing
    把工作做好,为了自己,不是为别人,调整下心态!
    Oracle数据库自身也提供了一种MTS的技术
    2011年终随想
  • 原文地址:https://www.cnblogs.com/kunsoft/p/5312776.html
Copyright © 2011-2022 走看看