SDNU 1027 马踏飞燕(续) 【BFS】

Description

上次的马踏飞燕是不是没玩够?无聊的lg准备编写一款游戏,就是加强版的“马踏飞燕”,在这款游戏中有个一个2000*2000的坐标,把马放在任意一个坐标点,再把燕子放在任意一个坐标点,并且燕子不会移动,马只能按照象棋规则走“日”。若200步之内能“踏”到燕子,则成功。lg不知道该怎么去写,现在请你帮助他。
走“日”说明:当马的坐标为(5,5)的时候,马下一步可以走的坐标有8个点,分别为(4,3)(6,3)(3,4)(7,4)(3,6)(7,6)(4,7)(6,7)

Input

第一行两个整数,马的起始坐标x,y (0<x,y<2000)
第一行两个整数,燕子的坐标 m,n (0<m,n<2000)

Output

若200步之内能“踏”到燕子,则输出“Y”
若200步之内不能“踏”到燕子,则输出“N”

Sample Input

5 5
7 4

Sample Output

Y

题意:给你马和燕子的坐标,问马能否在200步之内踏到燕子,燕子是静止不动的。

思路:不断搜索,看在200步之内能否找到目标

AC代码:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
const int maxn = 2005;
int vis[maxn][maxn];
int dir[][2] = {1, 2, 1, -2, 2, 1, 2, -1, -1, 2, -1, -2, -2, 1, -2, -1};
int main()
{
    int x, y;
    int m, n;
    int flag;
    scanf("%d%d%d%d",&x, &y, &m, &n);
    memset(vis, 0, sizeof(vis));
    flag = 0;
    pair<int, int> pa;
    queue<pair<int, int> > q;
    pa.first = x;
    pa.second = y;
    q.push(pa);
    while(!q.empty())
    {
        if(flag) break;
        pair<int, int> pb;
        pb = q.front();
        q.pop();
        if(vis[pb.first][pb.second] <= 199)
            for(int i = 0; i < 8; ++i)
            {
                pa.first = pb.first + dir[i][0];
                pa.second = pb.second + dir[i][1];
                if(pa.first < 0 || pa.first >= 2000 || pa.second < 0 || pa.second >= 2000)
                    continue;
                if(vis[pa.first][pa.second]) continue;
                if(pa.first == m && pa.second == n)
                {
                    flag = 1;
                    break;
                }
                vis[pa.first][pa.second] = vis[pb.first][pb.second] + 1;
                q.push(pa);
            }
    }
    if(flag == 0)
        cout << "N" << endl;
    else
        cout << "Y" << endl;
    return 0;
}

前段时间把搜索搞定,想找题来练练手,然后看上了这个,结果各种不顺..

之前的马踏飞燕我当深搜用递归就过了,然而到了续这里却TLE,换为队列后还是不行。参考了sunshineYG师哥的博客,发现用了goto,记得紫书上说这个不好用。前天发现pair比struct更快,然后换为pair试了试,结果过了(某师哥告诉我这道题有问题,然而..)。

PS:后来经过测试发现不是struct和pair的问题,是vis数组记录走的第几步这里起了作用。


自己写的改后的AC代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
using namespace std;
int x, y, n, m, flag;
int dir[8][2] = {1,2,2,1,1,-2,2,-1,-1,-2,-2,-1,-1,2,-2,1};
int vis[2005][2005];
struct node
{
    int x, y;
};
void bfs(int startx, int starty)
{
    queue<node> q;
    node tem;
    tem.x = startx;
    tem.y = starty;
    q.push(tem);
    while(!q.empty())
    {
        if(flag) return ;
        node now, nex;
        now = q.front();
        q.pop();
        if(vis[now.x][now.y] <= 199)
            for(int i = 0; i < 8; ++i)
            {
                nex.x = now.x + dir[i][0];
                nex.y = now.y + dir[i][1];
                if(nex.x <= 0 || nex.x > 2000 || nex.y <= 0 || nex.y > 2000) continue;
                if(vis[nex.x][nex.y]) continue;  //这里防止重复访问
                if(nex.x == m && nex.y == n)
                {
                    flag = 1;
                    return ;
                }
                vis[nex.x][nex.y] = vis[now.x][now.y] + 1;
                q.push(nex);
            }
    }
}
int main()
{
    scanf("%d%d%d%d",&x,&y,&m,&n);
    flag = 0;
    bfs(x, y);
    if(flag)  cout << "Y" << endl;
    else cout << "N" << endl;
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值