UVA439 POJ2243 HDU1372 ZOJ1091 Knight Moves

问题链接:UVA439 POJ2243 HDU1372 ZOJ1091
Knight Moves
。基础级练习题,用C++语言编写程序。

题意简述:给出国际象棋棋盘中的两个点,求马从一个点跳到另一个点的最少步数。

问题分析:典型的BFS问题。在BFS搜索过程中,马跳过的点就不必再跳了,因为这次再跳下去不可能比上次步数少。

程序中,使用了一个队列来存放中间节点,但是每次用完需要清空。

这里给出两个版本的程序,有一个是设置了边界的。

AC的C++语言程序如下:

/* UVA439 POJ2243 HDU1372 ZOJ1091 Knight Moves */

#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;

#define MAXN 8

#define DIRECTSIZE 8

struct direct {
    int drow;
    int dcol;
} direct[DIRECTSIZE] =
    {{-2, 1}, {-1, 2}, {1, 2}, {2, 1}, {2, -1}, {1, -2}, {-1, -2}, {-2, -1}};

char grid[MAXN][MAXN];

int startcol, startrow, endcol, endrow;
int ans;

struct node {
    int row;
    int col;
    int level;
};

queue<node> q;

void bfs()
{
    memset(grid, ' ', sizeof(grid));

    ans = 0;
    node start;
    start.row = startrow;
    start.col = startcol;
    start.level = 0;
    q.push(start);

    while(!q.empty()) {
        node front = q.front();
        q.pop();

        if(front.row == endrow && front.col == endcol) {
            ans = front.level;
            break;
        }

        for(int i=0; i<DIRECTSIZE; i++) {
            int nextrow = front.row + direct[i].drow;
            int nextcol = front.col + direct[i].dcol;

            if(0 <= nextrow && nextrow < MAXN && 0 <= nextcol && nextcol < MAXN)
                if(grid[nextrow][nextcol] == ' ') {
                    node v;
                    v.row = nextrow;
                    v.col = nextcol;
                    v.level = front.level + 1;
                    q.push(v);
                }
        }

        grid[front.row][front.col] = '*';
    }
}

int main(void)
{
    char startc, endc;

    while(scanf("%c%d %c%d", &startc, &startrow, &endc, &endrow) != EOF) {
        getchar();

        while(!q.empty())
            q.pop();

        startcol = startc - 'a';
        startrow -= 1;
        endcol = endc - 'a';
        endrow -= 1;

        bfs();

        printf("To get from %c%d to %c%d takes %d knight moves.\n", startc, startrow+1, endc, endrow+1, ans);
    }

    return 0;
}

另外一版AC的C++语言程序如下:

/* UVA439 POJ2243 HDU1372 ZOJ1091 Knight Moves */

#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;

#define MAXN 8

#define DIRECTSIZE 8

struct direct {
    int drow;
    int dcol;
} direct[DIRECTSIZE] =
    {{-2, 1}, {-1, 2}, {1, 2}, {2, 1}, {2, -1}, {1, -2}, {-1, -2}, {-2, -1}};

char grid[MAXN+4][MAXN+4];

int startcol, startrow, endcol, endrow;
int ans;

struct node {
    int row;
    int col;
    int level;
};

queue<node> q;

void bfs()
{
    ans = 0;
    node start;
    start.row = startrow;
    start.col = startcol;
    start.level = 0;
    q.push(start);

    while(!q.empty()) {
        node front = q.front();
        q.pop();

        if(front.row == endrow && front.col == endcol) {
            ans = front.level;
            break;
        }

        for(int i=0; i<DIRECTSIZE; i++) {
            int nextrow = front.row + direct[i].drow;
            int nextcol = front.col + direct[i].dcol;
            if(grid[nextrow][nextcol] == ' ') {
                node v;
                v.row = nextrow;
                v.col = nextcol;
                v.level = front.level + 1;
                q.push(v);
            }
        }

        grid[front.row][front.col] = '*';
    }
}

int main(void)
{
    int i;
    char startc, endc;

    while(scanf("%c%d %c%d", &startc, &startrow, &endc, &endrow) != EOF) {
        getchar();

        while(!q.empty())
            q.pop();

        startcol = startc - 'a' + 2;
        startrow += 1;
        endcol = endc - 'a' + 2;
        endrow += 1;

        memset(grid, ' ', sizeof(grid));
        for(i=0; i<MAXN+4; i++) {
            grid[0][i] = '*';
            grid[1][i] = '*';
            grid[MAXN+2][i] = '*';
            grid[MAXN+3][i] = '*';

            grid[i][0] = '*';
            grid[i][1] = '*';
            grid[i][MAXN+2] = '*';
            grid[i][MAXN+3] = '*';
        }

        bfs();

        printf("To get from %c%d to %c%d takes %d knight moves.\n", startc, startrow-1, endc, endrow-1, ans);
    }

    return 0;
}
时间: 2025-01-01 20:47:07

UVA439 POJ2243 HDU1372 ZOJ1091 Knight Moves的相关文章

poj2243&amp;&amp;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 y

poj2243 &amp;amp;&amp;amp; hdu1372 Knight Moves(BFS)

转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接: POJ:http://poj.org/problem?id=2243 HDU: pid=1372">http://acm.hdu.edu.cn/showproblem.php? pid=1372 Problem Description A friend of you is doing research on t

HDU1372:Knight Moves(经典BFS题)

HDU1372:Knight Moves(BFS) Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu 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 v

Knight Moves(hdu1372 bfs模板题)

http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6731    Accepted Submission(s): 4059 Problem Description A friend of you is doing res

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

宽搜--knight moves hdu1372

写出来之后觉得是一道简单的宽搜题,但是之前写的时候遇到很多细节处理的小问题.刚刚开始,还需要更加努力. 首先是题目意思,骑士移动原来是走日字,智商捉急. 其次是方向处理问题,之前一直没转过弯.普遍方向表达有两种 第一种: int h[8][2]={{-2,-1},{-2,1},{-1,2},{-1,-2},{1,-2},{1,2},{2,-1},{2,1}}; for(int i=0;i<8;i++){ next.a=head.a+h[i][0]; next.b=head.b+h[i][1];

HDU1372:Knight Moves(BFS)

Knight Moves Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 21   Accepted Submission(s) : 17 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description A friend of you is doing

HDU - 1372 Knight Moves(bfs入门)

HDU - 1372 Knight Moves 题目链接:https://vjudge.net/problem/HDU-1372#author=swust20141567 题目: 在象棋王国,尼古拉斯.火山是一匹英俊的马,他非常幸运迎娶了白马王国的公主,他们将度蜜月,你现在是他们的女仆,火山会问你去一些地方最少需要多少步,这么简单的事当然难不倒你.由于火山是一匹马,他的移动方式将会遵守国际象棋马的走法. 输入: 输入包含一个或多个输入样例.每个测试样例将会有两个坐标,表示现在的位置和将要到达的地

POJ 1915 Knight Moves

Knight Moves Description Background Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fast. Can you beat him? The Problem Your task is to write a program to calculate the mini