joj2410: The knight problem

简介: #include<iostream>#include<cstdio>#include<queue>using namespace std;typedef struct Point{ int x, y;}POINT;queue <POINT> me;POINT Begin , End;bool visited[9][9
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
typedef struct Point
{
    int x, y;
}POINT;
queue <POINT> me;
POINT Begin , End;
bool visited[9][9];
bool flag;
int ans;
int movei[8]= {2,-2,1,-1,2,-2,1,-1};
int movej[8]= {1,-1,2,-2,-1,1,-2,2};
void init()
{
    int i, j;
    for(i = 0; i < 9; i++)
        for(j = 0; j < 9; j++)
            visited[i][j] = false;
}
bool isok(POINT *p)
{
    if(p->x == End.x && p->y == End.y)
        return true;
    else
        return false;
}
bool isin(POINT *pt)
{
    if(pt->x >= 1 && pt->x <= 8)
        if(pt->y >= 1 && pt->y <= 8)
            return true;
    return false;
}
void change(char *str, POINT *pt)
{
    pt->x = str[0] - 'a' +1;
    pt->y =str[1] - '0';
}
void bfs()
{
    int i, q, casenum;
    POINT pt, tp;
    casenum = 1;
    while(!me.empty())
    {
        ans++;
        q = 0;
        while(casenum--)
        {
            pt = me.front();
            me.pop();
            for( i = 0; i < 8; i++)
            {
                tp.x = pt.x + movei[i];
                tp.y = pt.y + movej[i];
                if(isin(&tp))
                {
                    if(visited[tp.x][tp.y])
                        continue;
                    else
                    {
                        if(isok(&tp))
                        {
                            flag = true;
                            return;
                        }
                        else
                        {
                            visited[tp.x][tp.y] = true;
                            me.push(tp);
                            q++;
                        }
                    }
                }
            }
        }
        casenum = q;
    }
    if(me.empty())
        flag = false;
    return ;
}
int main()
{
    int n, i, casenum;
    POINT tp;
    char str[10];
    casenum = 1;
    while(true)
    {
        while(!me.empty())
        me.pop();
        flag = true;
        ans = 0;
        init();
        scanf("%d", &n);
        if(-1 == n)
            break;
        for(i = 0; i < n; i++)
        {
            scanf("%s", str);
            change(str, &tp);
            visited[tp.x][tp.y] = true;
        }
        scanf("%s", str);
        change(str, &Begin);
        scanf("%s", str);
        change(str, &End);
        me.push(Begin);
        visited[Begin.x][Begin.y] = true;
        if(isok(&Begin))
            printf("Board %d: %d moves\n", casenum, ans);
        bfs();
        if(flag)
            printf("Board %d: %d moves\n", casenum, ans);
        else
            printf("Board %d: not reachable\n", casenum);
        casenum++;
    }
    return 0;
}
题目链接
目录
相关文章
UVa1531 - Problem Bee
UVa1531 - Problem Bee
60 0
uva101 The Blocks Problem
uva101 The Blocks Problem
63 0
|
机器学习/深度学习
1257:Knight Moves 2021-01-09
1257:Knight Moves 2021-01-09
|
Go C++
P1001 A+B Problem
P1001 A+B Problem
122 0
A Knight&#39;s Journey
总时间限制: 1000ms 内存限制: 65536kB描述BackgroundThe knight is getting bored of seeing the same black and white squares again and again and has decided to make a journeyaround the world.
1185 0
|
机器学习/深度学习 算法
917:Knight Moves
题目链接:http://noi.openjudge.cn/ch0205/917/ 原题应该是hdu 1372 总时间限制: 1000ms  内存限制: 65536kB 描述 BackgroundMr Somurolov, fabulous chess-gamer indeed, asserts ...
1054 0
【HDU 5572 An Easy Physics Problem】计算几何基础
2015上海区域赛现场赛第5题。 题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5572 题意:在平面上,已知圆(O, R),点B、A(均在圆外),向量V。
1045 0