吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2336|回复: 1
收起左侧

[其他转载] 广度优先搜索

[复制链接]
JNAzreal 发表于 2019-4-8 16:32
题目是洛谷上面的题目 题名:马的遍历
链接:https://www.luogu.org/problemnew/show/P1443
[C++] 纯文本查看 复制代码
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#define meset(a) memset(a,0,sizeof(a))
int dir[][2]={-1,2,-1,-2,1,2,1,-2,2,1,-2,1,-2,-1,2,-1};
using namespace std;
int mp[500][500],vis[500][500],m,n,dx,dy;
struct node{
    int x,y,step;
    
    bool check()
    {
        if(x>=1&&x<=m&&y>=1&&y<=n)
        {
            if(!vis[x][y])
            return true;
        }
        return false ;
    }
}tail,head;
int main()
{
    cin>>m>>n>>dx>>dy;
    queue<node> q;
    q.push({dx,dy,0});
    vis[dx][dy]=1;
    while(!q.empty())
    {
        head=q.front();
        q.pop();
        for(int i=0;i<8;i++)
        {
            tail=head;
            tail.x+=dir[i][0];
            tail.y+=dir[i][1];
            tail.step++;
            if(tail.check())
            {
                q.push(tail);
                mp[tail.x][tail.y]=tail.step;
                vis[tail.x][tail.y]=1;
            } 
        }
    }
    for(int i=1;i<=m;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(mp[i][j]==0)
            mp[i][j]=-1;
            if(i==dx&&j==dy)
            mp[i][j]=0; 
            printf("%-5d",mp[i][j]);
        }
        cout<<endl;
    }
} 

免费评分

参与人数 2吾爱币 +4 热心值 +2 收起 理由
苏紫方璇 + 3 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
为爱单身 + 1 + 1 鼓励转贴优秀软件安全工具和文档!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

winna 发表于 2019-4-8 17:14
多谢参考了
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-27 05:55

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表