UVa439

Knight Moves

题意:骑士巡游到某位置的最少步数

#include <stdio.h>
#include <string.h>

int s[20][20];
int rear, front;
int min;
char a, c;
int b, d;

struct
{
    int x, y;
    int p;
}Susake[300000];

void bfs(int n, int m)
{
    int t;
    rear++;
    Susake[rear].x = n;
    Susake[rear].y = m;
    Susake[rear].p = 0;
    if(n == c - 96 && m == d)
    {
        min = Susake[rear].p;
        return ;
    }
    while(front != rear)
    {
        front++;
        t = front;
        if(s[Susake[t].x - 2][Susake[t].y + 1])
        {
            rear++;
            Susake[rear].x = Susake[t].x - 2;
            Susake[rear].y = Susake[t].y + 1;
            Susake[rear].p = Susake[t].p + 1;
            if(Susake[rear].x == c - 96 && Susake[rear].y == d)
            {
                min = Susake[rear].p;
                return ;
            }
        }
        if(s[Susake[t].x + 2][Susake[t].y + 1])
        {
            rear++;
            Susake[rear].x = Susake[t].x + 2;
            Susake[rear].y = Susake[t].y + 1;
            Susake[rear].p = Susake[t].p + 1;
            if(Susake[rear].x == c - 96 && Susake[rear].y == d)
            {
                min = Susake[rear].p;
                return ;
            }
        }
        if(s[Susake[t].x - 1][Susake[t].y + 2])
        {
            rear++;
            Susake[rear].x = Susake[t].x - 1;
            Susake[rear].y = Susake[t].y + 2;
            Susake[rear].p = Susake[t].p + 1;
            if(Susake[rear].x == c - 96 && Susake[rear].y == d)
            {
                min = Susake[rear].p;
                return ;
            }
        }
        if(s[Susake[t].x - 2][Susake[t].y - 1])
        {
            rear++;
            Susake[rear].x = Susake[t].x - 2;
            Susake[rear].y = Susake[t].y - 1;
            Susake[rear].p = Susake[t].p + 1;
            if(Susake[rear].x == c - 96 && Susake[rear].y == d)
            {
                min = Susake[rear].p;
                return ;
            }
        }
        if(s[Susake[t].x - 1][Susake[t].y - 2])
        {
            rear++;
            Susake[rear].x = Susake[t].x - 1;
            Susake[rear].y = Susake[t].y - 2;
            Susake[rear].p = Susake[t].p + 1;
            if(Susake[rear].x == c - 96 && Susake[rear].y == d)
            {
                min = Susake[rear].p;
                return ;
            }
        }
        if(s[Susake[t].x + 1][Susake[t].y - 2])
        {
            rear++;
            Susake[rear].x = Susake[t].x + 1;
            Susake[rear].y = Susake[t].y - 2;
            Susake[rear].p = Susake[t].p + 1;
            if(Susake[rear].x == c - 96 && Susake[rear].y == d)
            {
                min = Susake[rear].p;
                return ;
            }
        }
        if(s[Susake[t].x + 2][Susake[t].y - 1])
        {
            rear++;
            Susake[rear].x = Susake[t].x + 2;
            Susake[rear].y = Susake[t].y - 1;
            Susake[rear].p = Susake[t].p + 1;
            if(Susake[rear].x == c - 96 && Susake[rear].y == d)
            {
                min = Susake[rear].p;
                return ;
            }        }
        if(s[Susake[t].x + 1][Susake[t].y + 2])
        {
            rear++;
            Susake[rear].x = Susake[t].x + 1;
            Susake[rear].y = Susake[t].y + 2;
            Susake[rear].p = Susake[t].p + 1;
            if(Susake[rear].x == c - 96 && Susake[rear].y == d)
            {
                min = Susake[rear].p;
                return ;
            }
        }
    }
}

int main(int argc, char *argv[])
{
    int i, j;
    while(scanf("%c%d%*c%c%d%*c", &a, &b, &c, &d) != EOF)
    {
        memset(s, 0, sizeof(s));
        front = rear = 0;
        for(i = 1; i <= 8; i++)
            for(j = 1; j <= 8; j++)
                s[i][j] = 1;
        bfs(a - 96, b);
        printf("To get from %c%d to %c%d takes %d knight moves.\n", a, b, c, d, min);
    }
    return 0;
}

UVa439,布布扣,bubuko.com

时间: 2024-10-25 08:08:54

UVa439的相关文章

UVA439 POJ2243 HDU1372 ZOJ1091 Knight Moves

问题链接:UVA439 POJ2243 HDU1372 ZOJ1091 Knight Moves.基础级练习题,用C++语言编写程序. 题意简述:给出国际象棋棋盘中的两个点,求马从一个点跳到另一个点的最少步数. 问题分析:典型的BFS问题.在BFS搜索过程中,马跳过的点就不必再跳了,因为这次再跳下去不可能比上次步数少. 程序中,使用了一个队列来存放中间节点,但是每次用完需要清空. 这里给出两个版本的程序,有一个是设置了边界的. AC的C++语言程序如下: /* UVA439 POJ2243 HD

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

[bfs][A star][入门题]uva439 knightMoves

A*初步 A*要保证最短路一定要估价函数小于实际值,越接近越好 /*jerryRey 转载请注明出处,谢谢 http://www.cnblogs.com/jerryRey/ */ #include<cstdio> #include<queue> #include<cstring> #include<algorithm> using namespace std; const int N = 8; bool C[N][N]; struct node { int

UVa439——骑士的移动

简单bfs 1 #include <iostream> 2 #include <cstring> 3 #include <string> 4 #include <map> 5 #include <set> 6 #include <algorithm> 7 #include <fstream> 8 #include <cstdio> 9 #include <cmath> 10 #include <