#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. Sass预处理器

    CSS预处理器 less,sass和stylus sass:较早,缩进风格 scss:完全兼容css3,使用大括号 编写css的编程语言,引入变量,函数,重复代码等功能,如果编译成css文件 Sass ...

  2. 1. 时序练习(广告渠道vs销量预测)

    用散点图来看下sales销量与哪一维度更相关. 和目标销量的关系的话,那么这就是多元线性回归问题了. 上面把所有的200个数据集都用来训练了,现在把数据集拆分一下,分成训练集合测试集,再进行训练. 可 ...

  3. CSP J/S 初赛总结

    CSP J/S 初赛总结 2021/9/19 19:29 用官方答案估计 J 涂卡的时候唯一的一支 2B 铅笔坏了,只能用笔芯一个个涂 选择 \(-6\ pts\) 判断 \(-3\ pts\) 回答 ...

  4. Docker容器安装RabbitMQ

    Docker容器安装RabbitMQ 准备资料 erlang的rpm安装包 https://github.com/rabbitmq/erlang-rpm/releases rabbitmq的rpm安装 ...

  5. Chrome自带功能实现网页截图

    更新记录 本文迁移自Panda666原博客,原发布时间:2021年6月28日. 很简单,按下Ctrl+Shift+P,打开命令行窗口,如下图所示. 输入命令. Capture full size sc ...

  6. 拥抱 OpenAPI 3:springdoc-openapi 食用指南

    概述 使用 springdoc-openapi 可以快速为 springboot 项目生成规范的 API 文档,具体使用步骤如下: 依赖配置 在 pom.xml 加入内容,即可开始使用: <de ...

  7. redis主从复制(九)

    先来简单了解下redis中提供的集群策略, 虽然redis有持久化功能能够保障redis服务器宕机也能恢复并且只有少量的数据损失,但是由于所有数据在一台服务器上,如果这台服务器出现硬盘故障,那就算是有 ...

  8. BUUCTF-后门查杀

    后门查杀 后门查杀这种题最好还是整个D盾直接扫描目录方便. 查看文件得到flag

  9. 论文阅读 Exploring Temporal Information for Dynamic Network Embedding

    10 Exploring Temporal Information for Dynamic Network Embedding 5 link:https://scholar.google.com.sg ...

  10. 用WindowsAppSDK(WASDK)优雅的开发上位机应用

    C#开发上位机应用的一些选择 如果你不想看介绍,可以直接跳到优雅开发示例那里. 1. WASDK(WinUI 3) Windows 应用 SDK 是一组新的开发人员组件和工具,它们代表着 Window ...