#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long
#define ON_DEBUG #ifdef ON_DEBUG #define D_e_Line printf("\n\n----------\n\n")
#define D_e(x) cout << #x << " = " << x << endl
#define Pause() system("pause") #else #define D_e_Line ; #endif struct ios{
template<typename ATP>ios& operator >> (ATP &x){
x = 0; int f = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
x*= f;
return *this;
}
}io;
using namespace std; #define stone 2
#define spider 3
#define endPlace 9 const int N = 37; int n, m; struct MAP{
int x_1, y_1, x_2, y_2, step;
}u, v; #include<queue> queue<MAP>q; int mp[N][N]; int ans = 0x3f3f3f3f; int walk_1[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
int walk_2[4][2]={{1,0},{0,-1},{-1,0},{0,1}}; int vis[N][N][N][N]; inline void BFS(){
vis[u.x_1][u.y_1][u.x_2][u.y_2] = true;
q.push(u);
while(!q.empty()){
u = q.front();
q.pop(); if(mp[u.x_1][u.y_1] == endPlace && mp[u.x_2][u.y_2] == endPlace){
ans = u.step;
return;
} R(i,0,3){
v.x_1 = u.x_1 + walk_1[i][0];
v.y_1 = u.y_1 + walk_1[i][1];
v.x_2 = u.x_2 + walk_2[i][0];
v.y_2 = u.y_2 + walk_2[i][1];
v.step = u.step + 1;
if(v.x_1 < 1 || v.x_2 < 1 || v.y_1 < 1 || v.y_2 < 1 || v.x_1 > n || v.x_2 > n || v.y_1 > m || v.y_2 > m) continue;
if(mp[v.x_1][v.y_1] == stone) v.x_1 = u.x_1, v.y_1 = u.y_1;
if(mp[v.x_2][v.y_2] == stone) v.x_2 = u.x_2, v.y_2 = u.y_2;
if(mp[v.x_1][v.y_1] == spider || mp[v.x_2][v.y_2] == spider) continue;
if(vis[v.x_1][v.y_1][v.x_2][v.y_2]) continue;
vis[v.x_1][v.y_1][v.x_2][v.y_2] = 1;
q.push(v);
}
} } int st[2][2];
int main(){
io >> n >> m;
R(i,1,n){
char str[57];
scanf("%s", str + 1);
R(j,1,m){
switch(str[j]){
case '#' :{
mp[i][j] = stone;
break;
}
case 'X' :{
mp[i][j] = spider;
break;
}
case 'T' :{
mp[i][j] = endPlace;
break;
}
case 'G' :{
mp[i][j] = 7;
st[0][0] = i, st[0][1] = j;
break;
}
case 'M' :{
mp[i][j] = 8;
st[1][0] = i, st[1][1] = j;
break;
}
case '.' :{
mp[i][j] = 1;
break;
}
}
}
} u = (MAP){st[0][0], st[0][1], st[1][0], st[1][1], 0}; BFS(); if(ans == 0x3f3f3f3f){
printf("no");
}
else{
printf("%d", ans);
} return 0;
}

Luogu3855 [TJOI2008]Binary Land (BFS)的更多相关文章

  1. FZU - 1688 Binary land

    题目链接  Problem 1688 Binary land Accept: 72    Submit: 171Time Limit: 1000 mSec    Memory Limit : 3276 ...

  2. programming review (c++): (2)binary tree, BFS, DFS, recursive, non-recursive

    1.二叉树定义 // Definition for a binary tree node. struct TreeNode { int val; TreeNode *left; TreeNode *r ...

  3. 畅通工程再续(MST)

    畅通工程再续 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Statu ...

  4. LeetCode分类-前400题

    1. Array 基础 27 Remove Element 26 Remove Duplicates from Sorted Array 80 Remove Duplicates from Sorte ...

  5. FC红白机游戏列表(维基百科)

    1055个fc游戏列表 日文名 中文译名 英文版名 发行日期 发行商 ドンキーコング 大金刚 Donkey Kong 1983年7月15日 任天堂 ドンキーコングJR. 大金刚Jr. Donkey K ...

  6. Codechef May Challenge 2020 Division 1 记录

    目录 Triple Sort Sorting Vases Buying a New String Chef and Bitwise Product Binary Land Not a Real Wor ...

  7. Leetcode 107 Binary Tree Level Order Traversal II 二叉树+BFS

    题意是倒过来层次遍历二叉树 下面我介绍下BFS的基本框架,所有的BFS都是这样写的 struct Nodetype { int d;//层数即遍历深度 KeyType m;//相应的节点值 } que ...

  8. LeetCode Binary Tree Right Side View (DFS/BFS)

    题意: 给一棵二叉树,要求收集每层的最后一个节点的值.按从顶到底装进vector返回. 思路: BFS比较简单,先遍历右孩子就行了. /** * Definition for a binary tre ...

  9. (二叉树 BFS) leetcode993. Cousins in Binary Tree

    In a binary tree, the root node is at depth 0, and children of each depth knode are at depth k+1. Tw ...

随机推荐

  1. C++primer第一章

    第一章 : 开始 1.1 编写一个简单的C++程序 要点:每个函数有且只能拥有一个main函数,且main的默认返回是一个int类型. 函数定义:返回类型,函数名,形参列表,函数体 1.1.1编译 运 ...

  2. Python趣味入门9:函数是你走过的套路,详解函数、调用、参数及返回值

    1.概念 琼恩·雪诺当上守夜人的司令后,为训练士兵对付僵尸兵团,把成功斩杀僵尸的一系列动作编排成了"葵花宝典剑法",这就是函数.相似,在计算机世界,一系列前后连续的计算机语句组合在 ...

  3. quasar + uni-app混合打包APP

    写几个关键点,作为备忘录. 和所有框架一样,现在本地run build quasar的cli是 quasar build 然后记住打包好以后的静态文件 目录 uni-app新建一个5+App的默认模板 ...

  4. 用C语言实现井字棋(人人/AI人机)--完结版

    目录 用C语言实现井字棋(人人/AI人机)--完结版 BUG与优化3: 1. 修改了step的计算方法,每个玩家玩完就加一次step 2. 改变了电脑下棋的逻辑,每个玩家玩完之后都跳过这次循环 源码: ...

  5. SAP Column tree

    code as bellow *&---------------------------------------------------------------------* *& I ...

  6. 我用Python做了一个咖啡馆数据分析

    在做案例前,我还想回答大家一个疑问,就是excel做数据分析可以实现Python一样的效果,那用Python的意义在哪呢? 经过这段时间学习理解,我的回答是: (https://jq.qq.com/? ...

  7. colab解压.tar.gz文件指令

    !tar -zxvf flower_photos.tar.gz 成功后如图:

  8. 上传几张.NET5之后的机器人logo

    上传几张.NET5之后的机器人logo

  9. TypeScript let与var的区别

    1.作用域不同 用var声明的变量,只有函数作用域和全局作用域,没有块级作用域.而let可以实现块级作用域,只能在代码块{}内有效,在{}之外不能访问,如下代码所示: { let a = 0; var ...

  10. 013(oulipo)

    题目:http://ybt.ssoier.cn:8088/problem_show.php?pid=1455 题目描述:在母串里找子串出现的次数 题目思路:与字符串的搜索有关那就立刻找到哈希 从s[1 ...