uva 439 Knight Moves 骑士移动

这道题曾经写过,bfs。用队列,不多说了,上代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<queue>
using namespace std;
int map[10][10];
int visit[10][10];
int dist[10][10];
int dx[8]={-2,-2,-1,-1,1,1,2,2};
int dy[8]={-1,1,-2,2,-2,2,-1,1};
char a[5];
int x2,y2;
struct node
{
	int x,y;
};
queue<node>q;
int bfs(node T)
{
	if(T.x==x2&&T.y==y2)
	{
		return dist[T.x][T.y];
	}
	else
	{
		while(!q.empty())
		{
			node m = q.front();
			q.pop();
			if(m.x==x2&&m.y==y2)
				return dist[m.x][m.y];
			for(int i=0; i<8; i++)
			{

				int xx = m.x+dx[i];
				int yy = m.y+dy[i];
				if(!visit[xx][yy]&&xx>0&&yy>0&&xx<=8&&yy<=8)
				{
					node n ;
					n.x = xx;
					n.y = yy;
					q.push(n);
					dist[xx][yy] = dist[m.x][m.y]+1;
					visit[xx][yy] = 1;
				}
			}
		}
	}
}
int main()
{
	int x1,y1,i,j;
	while(gets(a))
	{
		y1 = a[0]-‘a‘+1;
		x1 = a[1]-‘0‘;
		y2 = a[3]-‘a‘+1;
		x2 = a[4]-‘0‘;
		//printf("%d %d %d %d\n",x1,y1,x2,y2);
		node T;
		T.x = x1;
		T.y = y1;
		memset(dist,0,sizeof(dist));
		memset(visit,0,sizeof(visit));
		q.push(T);
		bfs(T);
		printf("To get from %c%c to %c%c takes %d knight moves.\n",a[0],a[1],a[3],a[4],dist[x2][y2]);
		while(!q.empty())
		{
			q.pop();
		}
	}
	return 0;
}
 
时间: 2024-10-11 18:13:40

uva 439 Knight Moves 骑士移动的相关文章

UVA - 439 - Knight Moves (BFS)

UVA - 439 Knight Moves Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knigh

UVa 439 Knight Moves(BFS应用)

题意  求国际象棋中骑士从一个位置移东到另一个位置所需最少步数 基础的BFS应用 #include <bits/stdc++.h> using namespace std; int x[] = { -2, -1, -2, -1, 1, 2, 1, 2}; int y[] = { -1, -2, 1, 2, -2, -1, 2, 1}; int d[15][15], sx, sy, ex, ey; pair<int, int> q[105], t; int bfs() { int c

Uva 439 Knight Moves

1 #include <iostream> 2 #include <cstring> 3 #include <queue> 4 using namespace std; 5 6 int IsVis[9][9];//记录位置是否被访问 7 int StaX,StaY,EndX,EndY; 8 char Start[3],End[3]; //起始及结束位置 9 typedef struct node 10 { 11 int x,y,MoveNum; 12 }knight;

UVA 439 Knight Moves 走象棋 (DFS or BFS)

[题目链接]click here~~ [题目大意]类型于中国象棋里面"马"的走法,给你两个坐标,一个初始坐标,一个最终坐标,在保证有解的情况下最小的步数 [思路]BFS的话,直接模拟,因为棋盘比较小 (1)BFS +队列 代码:(3ms) #include <bits/stdc++.h> using namespace std; int dir8[8][2]= {{1,2},{2,1},{-1,2},{-2,1},{1,-2},{2,-1},{-1,-2},{-2,-1}}

【UVa】439 Knight Moves(dfs)

题目 题目 ? ? 分析 没有估价函数的IDA...... ? ? 代码 #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int q,dx[10]={2,2,-2,-2,1,-1,1,-1},dy[10]={1,-1,1,-1,2,2,-2,-2},ans=1<<15; bool vis[11][11]; int x1,y1,x2,y2; bool

Knight Moves (UVa 439) BFS

题目:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=839&page=show_problem&problem=380 思路:用 BFS 求出到终点的最短距离即可. 小技巧:用一个 dir 数组和两个 for 循环将与一个节点连接的八个节点枚举出来. /* Knight Moves (UVa 439) */ #include <iostream> #i

UVa 439骑士的移动(BFS)

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=380 做了这道题之后对BFS总算是有了点认识了. 1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 5 int map[10][10]; 6 typedef struct node 7

UVA 439 BFS 骑士的移动

#include<iostream> #include<cstdio> #include<string> #include<string.h> #include<math.h> #include<queue> #include<map> #include<algorithm> using namespace std; int dx,dy; struct node{ int step; int x; int y;

Sicily Knight Moves(BFS)

1000. Knight Moves                       Time Limit: 1sec    Memory Limit:32MB 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