洛谷 1747.好奇怪的游戏

思路:BFS

其实也就是对于走的方式进行修改就行了,这和上一个马的遍历是一样的。

只不过这里又多了一个走"田"字。

上代码:

#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<cmath> 
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
#include <iomanip>
#include<sstream>
#include<numeric>
#include<map>
#include<limits.h>
#include<set>
#define int long long
#define MAX 30
#define _for(i,a,b) for(int i=a;i<(b);i++)
#define ALL(x) x.begin(),x.end()
using namespace std;
typedef pair<int, int> PII;
int n, m;
int counts;
char maps[MAX][MAX];
int dist[MAX][MAX];
int x1, y, x2, y2;
queue<PII>q;
int dx[] = { 2,2,-2,-2,-1,-1,1,1,2,2,-2,-2 };
int dy[] = { 1,-1,-1,1,2,-2,2,-2,2,-2,2,-2 };
int bfs(int st1, int st2) {
    memset(dist, -1, sizeof dist);
    q.push({ st1,st2 });
    dist[st1][st2] = 0;
    while (!q.empty()) {
        auto tmp = q.front();
        q.pop();
        for (int i = 0; i < 12; i++) {
            int a = dx[i] + tmp.first;
            int b = dy[i] + tmp.second;
            if (a <= 0 || b <= 0||a>20||b>20)
                continue;
            if (dist[a][b] >= 0)
                continue;

            dist[a][b] = dist[tmp.first][tmp.second] + 1;
            q.push({ a,b });
        }
    }
    return dist[1][1];
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    cin >> x1 >> y;
    cin >> x2 >> y2;
    cout << bfs(x1, y) << endl;
    cout << bfs(x2, y2) << endl;
    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值