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>
#include <cstring>
#include <queue>
using namespace std;

const int maxn = 10;

struct Point{
    int r, c, cnt;
    Point(int r, int c, int cnt): r(r), c(c), cnt(cnt){}
};

int vis[maxn][maxn];
int dir[] = {1,-1, 2,-2};
int r1, c1, r2, c2;                //起点、终点的坐标 

int BFS(int x1, int y1, int x2, int y2);

int main(){
    //freopen("input.txt", "r", stdin);
    char s1[2], s2[2];
    while(cin >> s1 >> s2){
        r1 = s1[1] - ‘0‘;
        c1 = s1[0] - ‘a‘ + 1;
        r2 = s2[1] - ‘0‘;
        c2 = s2[0] - ‘a‘ + 1;
        cout << "To get from " << s1 << " to " << s2 << " takes " << BFS(r1, c1, r2, c2) << " knight moves."<< endl;
    } 

    return 0;
}

int BFS(int x1, int y1, int x2, int y2){

    memset(vis, 0, sizeof(vis));
    queue<Point> q;
    q.push(Point(x1, y1, 0));
    vis[x1][y1] = 1;

    while(!q.empty()){
        Point u = q.front();    q.pop();
        if(u.r == r2 && u.c == c2)
            return u.cnt;

        for(int i=0; i<4; i++){                //将可走的结点加入队列
            for(int j=0; j<4; j++){
                if(i != j && -dir[i] != dir[j]){
                    Point v(u.r+dir[i], u.c+dir[j], u.cnt+1);
                    if(v.r>=1 && v.r<=8 && v.c>=1 && v.c<=8 && vis[v.r][v.c]==0){
                        q.push(v);
                        vis[v.r][v.c] = 1;
                    }
                }
            }
        }
    }
}
时间: 2024-10-06 07:36:58

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

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;

poj 1915 Knight Moves 【双向bfs】

Knight Moves Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 22121   Accepted: 10332 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

Knight Moves (双向bfs)

# 10028. 「一本通 1.4 例 3」Knight Moves [题目描述] 编写一个程序,计算一个骑士从棋盘上的一个格子到另一个格子所需的最小步数.骑士一步可以移动到的位置由下图给出. [算法] 双向bfs. [代码] #include <bits/stdc++.h> #define P pair<int,int> #define ff first #define ss second using namespace std; int T,ans; int d1[310][3

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

【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