• poj2243 && hdu1372 Knight Moves(BFS)


    转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents

    题目链接:

    POJ:http://poj.org/problem?id=2243

    HDU: http://acm.hdu.edu.cn/showproblem.php?

    pid=1372


    Problem Description
    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
    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
    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.

    题意:用象棋中跳马的走法。从起点到目标点的最小步数;

    代码例如以下:

    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    #include <queue>
    #include <cstring>
    using namespace std;
    #define M 1017
    struct node
    {
    	int x, y;
    	int step;
    };
    int xx[8] = {1,2,2,1,-1,-2,-2,-1};
    int yy[8] = {2,1,-1,-2,-2,-1,1,2};
    bool vis[M][M];
    int n, ansx, ansy;
    queue<node>q;
    int BFS(int x, int y)
    {
    	
    	if(x == ansx && y == ansy)
    		return 0;
    	int dx, dy, i;
    	node front, rear;
    	front.x = x, front.y = y, front.step = 0;
    	q.push(front);
    	vis[x][y] = true;
    	while(!q.empty())
    	{
    		front = q.front();
    		q.pop();
    		for(i = 0; i < 8; i++)
    		{
    			dx = front.x+xx[i];
    			dy = front.y+yy[i];
    			if(dx>=1&&dx<=8&&dy>=1&&dy<=8&&!vis[dx][dy])
    			{
    				vis[dx][dy] = true;
    				if(dx == ansx && dy == ansy)
    				{
    					return front.step+1;
    				}
    				rear.x = dx, rear.y = dy, rear.step = front.step+1;
    				q.push(rear);
    			}
    		}
    	}
    
    }
    int main()
    {
    	char s,e;
    	int a1,a2;
    	while(~scanf("%c%d %c%d",&s,&a1,&e,&a2))
    	{
    		getchar();
    		while(!q.empty())
    			q.pop();
    		memset(vis,0,sizeof(vis));
    		int s1 = s-'a'+1;
    		int e1 = e-'a'+1;
    		ansx = e1, ansy = a2;
    		int ans = BFS(s1,a1);
    		printf("To get from %c%d to %c%d takes %d knight moves.
    ",s,a1,e,a2,ans);
    	}
    	return 0;
    }


  • 相关阅读:
    弃用!Github 上用了 Git.io 缩址服务的都注意了
    创建一个unicloud项目步骤
    win10 文件夹右键卡死解决办法
    微信公众号微信CRM和公众号管理软件 侯斯特
    openlayer,地图底图白色 修改为深色显示
    WPF 通用权限开发框架 (ABP)
    chromuim内核video audio标签自动播放
    字符串,16进制互转
    虚拟机VMware16+Ubuntu20.04配置LOAMLIVOX
    curl centos 使用详解
  • 原文地址:https://www.cnblogs.com/yutingliuyl/p/7044079.html
Copyright © 2020-2023  润新知