COJ 1224 ACM小组的古怪象棋

本文分享了一段C语言实现的骑士在棋盘上从起点到终点的最短路径问题,通过广度优先搜索(BFS)算法进行求解,并特别强调了在处理输入输出时的注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

大概是以后一定要注意输入输出

比如 题目最小给的坐标是从(1,1)开始的

那再去判断是否数组越界的时候

就需要特别注意了

道理我懂

然后因为这个问题以及在给输入坐标减1的时候

一不小心把m,n也算进去了

然后就炸了我一个小时

妈个鸡

今天打rank的第一道也是这样

拿到题目我考虑到了行

却没有考虑到列

炸了2h

我他妈日狗的心都有了再见



#include <stdio.h>
#include <queue>
#include <string.h>
#include <stdlib.h>
//#include<iostream>
using namespace std;

int n,m,sx,sy,ex,ey;
int Map[21][21];

int dir[8][2]={{-1,2},{1,2},//向右跳 0,1
                {2,1},{2,-1},//向上跳 2,3
               {1,-2},{-1,-2},//向左跳 4,5
               {-2,-1},{-2,1}//向下跳 6,7
               };

struct Node
{
    int x,y,step;
};


 int check(int x,int y)
{
  if(x>=0&&x<m&&y>=0&&y<n&&Map[x][y]==0)
    return 1;
  else
    return 0;
}

int BFS(int x,int y)
{
    Node temp, next;
    queue<Node> Queue;
    temp.x=x;
    temp.y=y;
    temp.step=0;
    Map[x][y]=1;
    Queue.push(temp);
    while(!Queue.empty())
    {
        temp=Queue.front();
        Queue.pop();
        if(temp.x==ex&&temp.y==ey)
        {
            return temp.step;
        }
        else
            if(temp.x==ex&&temp.y+1==ey)//up
            {
                for (int i=0; i<8;i++)
                {
                    if(i==2||i==3)continue;
                    next.x=temp.x+dir[i][0];
                    next.y=temp.y+dir[i][1];
                   if(check(next.x,next.y)==1)
                {
                    next.step=temp.step+1;
                    Map[next.x][next.y]=1;
                    if(next.x==ex&&next.y==ey)
                        return next.step;

                    Queue.push(next);
                }}}

            else if(temp.x==ex&&temp.y-1==ey)//down
            {
                for (int i=0; i<8;i++)
                {if(i==6||i==7)continue;
                next.x=temp.x+dir[i][0];
                next.y=temp.y+dir[i][1];
                if(check(next.x,next.y)==1)
            {
                next.step=temp.step+1;
                Map[next.x][next.y]=1;
                if(next.x==ex&&next.y==ey)
                        return next.step;
                Queue.push(next);
            }}}

            else if(temp.x+1==ex&&temp.y==ey)//right
              {
                for (int i=0; i<8;i++)
                {if(i==0||i==1)continue;
                next.x=temp.x+dir[i][0];
                next.y=temp.y+dir[i][1];
                if(check(next.x,next.y)==1)
            {
                next.step=temp.step+1;
                Map[next.x][next.y]=1;
                if(next.x==ex&&next.y==ey)
                        return next.step;
                Queue.push(next);
            }}}

            else if(temp.x-1==ex&&temp.y==ey)//left
             {
                for (int i=0; i<8;i++)
                {if(i==4||i==5)continue;
                next.x=temp.x+dir[i][0];
                next.y=temp.y+dir[i][1];
                if(check(next.x,next.y)==1)
            {
                next.step=temp.step+1;
                Map[next.x][next.y]=1;
                if(next.x==ex&&next.y==ey)
                        return next.step;
                Queue.push(next);
            }}}

            else
            for (int i=0; i<8;i++)
            {
            next.x=temp.x+dir[i][0];
            next.y=temp.y+dir[i][1];

            if(check(next.x,next.y)==1)
            {
                next.step=temp.step+1;
                Map[next.x][next.y]=1;
                Queue.push(next);
            }
        }
    }
    return -1;
}

int main()
{

//int ex,ey,sx,sy;
    while(scanf("%d %d %d %d %d %d",&m,&n,&sx,&sy,&ex,&ey)!=EOF)
    {
        memset(Map,0,sizeof(Map));
        sx--;sy--;ex--;ey--;

        printf("%d\n",BFS(sx,sy));
    }
    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值