栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

poj 1915 Knight Moves

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

poj 1915 Knight Moves

#include <stdio.h>#include <queue>#include <string.h>#include <iostream>using namespace std;typedef struct node{    int x;    int y;    int step;}Node;int Map[310][310];int dir[8][2]={-1,2,1,2,2,1,2,-1,1,-2,-1,-2,-2,-1,-2,1};void BFS(int n, int sx, int sy, int ex, int ey){    if (sx == ex && sy == ey)    {        printf("0n");        return;    }    Node temp;    queue<Node> Queue;    temp.x = sx;    temp.y = sy;    temp.step = 0;    Map[sx][sy] = 1;    Queue.push(temp);    while(!Queue.empty())    {        temp.x = Queue.front().x;        temp.y = Queue.front().y;        temp.step = Queue.front().step;        Queue.pop();        if (temp.x == ex && temp.y == ey)        { printf("%dn", temp.step); return;        }        for (int i = 0; i < 8; i++)        { Node Next; Next.x = temp.x + dir[i][0]; Next.y = temp.y + dir[i][1]; Next.step = temp.step + 1; if (Next.x >= 0 && Next.x < n && Next.y >= 0 && Next.y < n && Map[Next.x][Next.y] == 0) {     Map[Next.x][Next.y] = 1;     Queue.push(Next); }        }    }}int main(){    int nCase;    int n;    int sx, sy, ex, ey;    scanf("%d", &nCase);    for (int i = 0; i < nCase; i++)    {        memset(Map, 0, sizeof(Map));        scanf("%d", &n);        scanf("%d%d%d%d", &sx, &sy, &ex, &ey);        BFS(n, sx, sy, ex, ey);    }    return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/377204.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号