#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. mysql 超时配置

    日志报错: No operations allowed after connection closed.; nested exception is com.mysql.jdbc.exceptions. ...

  2. 支付宝开放平台--网页&移动应用(一)

    前提是先在支付宝上签约自己需要的支付宝功能,然后支付宝开放平台才能设置你需要的功能 一:支付宝开放平台登录 登录进入支付宝开放平台 二:根据自己的需求创建应用(我是用的网页&移动应用) 三:点 ...

  3. pycharm编辑器下载与安装

    pycharm编辑器下载与安装 首先是下载地址:https://www.jetbrains.com/pycharm/download/#section=windows   首先根据自己的电脑选择系统, ...

  4. LVS+keepalived高可用

    1.keeplived相关 1.1工作原理 Keepalived 是一个基于VRRP协议来实现的LVS服务高可用方案,可以解决静态路由出现的单点故障问题. 在一个LVS服务集群中通常有主服务器(MAS ...

  5. python+selenium 自动化测试——显式等待详解

    1.前言 之前有提到过等待函数,等待函数分为:强制等待(sleep).隐式等待(implicitly_wait),显示等待(WebDriverWait),这次以显示等待方式专门做一次总结,因为我个人是 ...

  6. 论文解读(GraphMAE)《GraphMAE: Self-Supervised Masked Graph Autoencoders》

    论文信息 论文标题:GraphMAE: Self-Supervised Masked Graph Autoencoders论文作者:Zhenyu Hou, Xiao Liu, Yukuo Cen, Y ...

  7. 关于使用koa实现线上 https服务

    var https=require("https");//https服务var fs= require("fs");var Koa = require('koa ...

  8. 【RPA之家转载RPA创新产业峰会回看】机器人流程自动化专利态势报告

    [RPA之家转载RPA创新产业峰会回看]机器人流程自动化专利态势报告 自动化的一个专利情况的监测,就是全球监测的情况.今天我可能给大家汇报的主要是三个方面,第一个方面就是讲一下全球投资智能化的专利的一 ...

  9. REST类型网址调用

    by zyi

  10. NC50999 表达式计算4

    NC50999 表达式计算4 题目 题目描述 给出一个表达式,其中运算符仅包含+,-,*,/,^(加 减 乘 整除 乘方)要求求出表达式的最终值 数据可能会出现括号情况,还有可能出现多余括号情况 数据 ...