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 cx, cy, r, c, front = 0, rear = 0;
    memset(d, -1, sizeof(d));
    d[sx][sy] = 0;
    q[rear++] = make_pair(sx, sy);
    while(front < rear)
    {
        t = q[front++];
        cx = t.first, cy = t.second;
        if(cx == ex && cy == ey) return d[cx][cy];
        for(int i = 0; i < 8; ++i)
        {
            r = cx + x[i], c = cy + y[i];
            if(r < 0 || r > 7 || c < 0 || c > 7 || d[r][c] >= 0) continue;
            d[r][c] = d[cx][cy] + 1;
            q[rear++] = make_pair(r, c);
        }
    }
}

int main()
{
    char a[5], b[5];
    while(~scanf("%s%s", a, b))
    {
        sx = a[0] - 'a', sy = a[1] - '1';
        ex = b[0] - 'a', ey = b[1] - '1';
        printf("To get from %s to %s takes %d knight moves.\n", a, b, bfs());
    }
    return 0;
}

 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.

时间: 2024-12-24 09:56:17

UVa 439 Knight Moves(BFS应用)的相关文章

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 走象棋 (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

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 骑士移动

这道题曾经写过,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

POJ 2243 || HDU 1372:Knight Moves(BFS)

Knight Moves Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11223 Accepted: 6331 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 visit

POJ 1915 Knight Moves(BFS+STL)

 Knight Moves Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 20913   Accepted: 9702 Description Background Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fa

【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

POJ 1915 Knight Moves [BFS]

Knight Moves Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 26844   Accepted: 12663 Description Background  Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fas

UVa439 Knight Moves (BFS求最短路)

链接:http://acm.hust.edu.cn/vjudge/problem/19436分析:BFS跑一次最短路,状态转移有8个. 1 #include <cstdio> 2 #include <queue> 3 #include <cstring> 4 using namespace std; 5 6 struct Point { 7 char r, c; 8 Point(char r = ' ', char c = ' '): r(r), c(c) {}; 9