首页 > 其他分享 >hdu:Knight Moves(bfs)

hdu:Knight Moves(bfs)

时间:2023-08-27 20:11:28浏览次数:41  
标签:a1 hdu takes get int Knight bfs knight moves

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.

bfs

点击查看代码
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> PII;
#define x first
#define y second

int dir[8][2]={{1,2},{1,-2},{-1,2},{-1,-2},
{2,1},{2,-1},{-2,1},{-2,-1}};
int dist[10][10];
bool vis[10][10];
int n=8;
void bfs(int x,int y,int xx,int yy)
{
	memset(vis,0,sizeof vis);
	memset(dist,0,sizeof dist);
	queue<PII> q;
	q.push({x,y});
	dist[x][y]=0;
	while(q.size())
	{
		auto p=q.front();
		q.pop();
		int a=p.x,b=p.y,s=dist[a][b];
		if(a==xx&&b==yy) 
		{
			return ;
		}
		for(int i=0;i<n;++i)
		{
			int _x=a+dir[i][0];
			int _y=b+dir[i][1];
			if(_x>=1&&_x<=n&&_y>=1&&_y<=n&&!vis[_x][_y])
			{
			    vis[_x][_y]=1;
			    q.push({_x,_y});
			    dist[_x][_y]=s+1;	
			}
		}
	}
}

int main()
{
    char t,tt;
    int y,yy;
    while(cin>>t>>y>>tt>>yy)
    {
    	int x=t-'a'+1;
    	int xx=tt-'a'+1;
    	bfs(x,y,xx,yy);
    	cout<<"To get from "<<t<<y<<" to "<<tt<<yy<<" takes "
    	<<dist[xx][yy]<<" knight moves.\n";
    }
    return 0;
}

标签:a1,hdu,takes,get,int,Knight,bfs,knight,moves
From: https://www.cnblogs.com/ruoye123456/p/17660735.html

相关文章

  • hdu:A strange lift(bfs)
    ProblemDescriptionThereisastrangelift.Theliftcanstopcanateveryfloorasyouwant,andthereisanumberKi(0<=Ki<=N)oneveryfloor.Thelifthavejusttwobuttons:upanddown.Whenyouatfloori,ifyoupressthebutton“UP”,youwi......
  • leetcode 题库994——bfs典型解法(队列+递归实现)
     classSolution:deforangesRotting(self,grid:list[list[int]])->int:m,n=len(grid),len(grid[0])queue,good=[],[]defbfs(queue,good,m,n,grid):times=0directions=[(-1,0),(1,0),(0,1),(0,-1)]......
  • hdu:一个人的旅行
    ProblemDescription虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里迷路的人,汗~),但是草儿仍然很喜欢旅行,因为在旅途中会遇见很多人(白马王子,0),很多事,还能丰富自己的阅历,还可以看美丽的风景……草儿想去很多地方,她想要去东京铁塔看夜景,去威尼斯看电影,去阳明山上看海芋,去纽......
  • hdu:悼念512汶川大地震遇难同胞——珍惜现在,感恩生活
    ProblemDescription急!灾区的食物依然短缺!为了挽救灾区同胞的生命,心系灾区同胞的你准备自己采购一些粮食支援灾区,现在假设你一共有资金n元,而市场有m种大米,每种大米都是袋装产品,其价格不等,并且只能整袋购买。请问:你用有限的资金最多能采购多少公斤粮食呢?后记:人生是一个充满了......
  • hdu:Piggy-Bank(背包)
    ProblemDescriptionBeforeACMcandoanything,abudgetmustbepreparedandthenecessaryfinancialsupportobtained.ThemainincomeforthisactioncomesfromIrreversiblyBoundMoney(IBM).Theideabehindissimple.WheneversomeACMmemberhasany......
  • hdu:免费馅饼
    ProblemDescription都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼。说来gameboy的人品实在是太好了,这馅饼别处都不掉,就掉落在他身旁的10米范围内。馅饼如果掉在了地上当然就不能吃了,所以gameboy马上卸下身上的背包去接。但由于小径两侧都不能......
  • hdu:搬寝室
    ProblemDescription搬寝室是很累的,xhd深有体会.时间追述2006年7月9号,那天xhd迫于无奈要从27号楼搬到3号楼,因为10号要封楼了.看着寝室里的n件物品,xhd开始发呆,因为n是一个小于2000的整数,实在是太多了,于是xhd决定随便搬2k件过去就行了.但还是会很累,因为2k也不小是一个不大......
  • hdu:不容易系列之(3)—— LELE的RPG难题
    ProblemDescription人称“AC女之杀手”的超级偶像LELE最近忽然玩起了深沉,这可急坏了众多“Cole”(LELE的粉丝,即”可乐”),经过多方打探,某资深Cole终于知道了原因,原来,LELE最近研究起了著名的RPG难题:有排成一行的n个方格,用红(Red)、粉(Pink)、绿(Green)三色涂每个格子,每格涂一色,要......
  • hdu:畅通工程(并查集)
    ProblemDescription某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇。省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可)。问最少还需要建设多少条道路?Input测试输入包含若干......
  • hdu:田忌赛马(贪心,双指针)
    ProblemDescription“田忌赛马”是中国历史上一个著名的故事。大约2300年前,齐国大将田忌喜欢和国王赛马,并且约定:每赢一场,对方就要付200元。假设已知田忌和国王的各自马匹的速度都不相同,请计算田忌最好的结果是什么。Input输入包含多组测试样例。每组样例的第一行是一个整数......