Luogu3855 [TJOI2008]Binary Land (BFS)
#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)的更多相关文章
- FZU - 1688 Binary land
题目链接 Problem 1688 Binary land Accept: 72 Submit: 171Time Limit: 1000 mSec Memory Limit : 3276 ...
- 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 ...
- 畅通工程再续(MST)
畅通工程再续 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Statu ...
- LeetCode分类-前400题
1. Array 基础 27 Remove Element 26 Remove Duplicates from Sorted Array 80 Remove Duplicates from Sorte ...
- FC红白机游戏列表(维基百科)
1055个fc游戏列表 日文名 中文译名 英文版名 发行日期 发行商 ドンキーコング 大金刚 Donkey Kong 1983年7月15日 任天堂 ドンキーコングJR. 大金刚Jr. Donkey K ...
- Codechef May Challenge 2020 Division 1 记录
目录 Triple Sort Sorting Vases Buying a New String Chef and Bitwise Product Binary Land Not a Real Wor ...
- Leetcode 107 Binary Tree Level Order Traversal II 二叉树+BFS
题意是倒过来层次遍历二叉树 下面我介绍下BFS的基本框架,所有的BFS都是这样写的 struct Nodetype { int d;//层数即遍历深度 KeyType m;//相应的节点值 } que ...
- LeetCode Binary Tree Right Side View (DFS/BFS)
题意: 给一棵二叉树,要求收集每层的最后一个节点的值.按从顶到底装进vector返回. 思路: BFS比较简单,先遍历右孩子就行了. /** * Definition for a binary tre ...
- (二叉树 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 ...
随机推荐
- pandas:聚合统计、数据分箱、分组可视化
1.聚合统计 1.1描述统计 #df.describe(),对数据的总体特征进行描述 df.groupby('team').describe() df.groupby('team').describe ...
- 前端5jQuery
内容概要 jQuery简介 查找标签 jQuery操作标签 jQuery事件操作 jQuery动画效果(了解) 前端第三方框架(基础) 内容详情 jQuery简介
- 如何安装TypeScript编译器?
首先需要nodejs和npm工具.如果没有点击这里下载node(默认会附带安装npm工具):https://nodejs.org/en/ npm安装TypeScript npm install -g ...
- Python双人五子棋
这篇文章旨在介绍一个双人的五子棋程序.再次重申,本人不擅长对代码的可读性进行优化,所以可能有些杂乱(在所难免). 先瞅一眼效果图: 请注意,这个棋子--是这么圆润立体!本程序不需任何素材图片,完全用代 ...
- 基于web3D展示技术的煤矿巷道3D可视化系统
地下开采离不开巷道工程.煤矿的生产.运输.排水.通风等各个环节都少不了巷道的支持.在煤矿智能化建设被提上日程的今天,巷道工程的智能化.可视化建设也成了行业趋势.尤其是复杂的井下作业环境,人员信息安全问 ...
- 用Typescript 的方式封装Vue3的表单绑定,支持防抖等功能。
Vue3 的父子组件传值.绑定表单数据.UI库的二次封装.防抖等,想来大家都很熟悉了,本篇介绍一种使用 Typescript 的方式进行统一的封装的方法. 基础使用方法 Vue3对于表单的绑定提供了一 ...
- python爬虫之protobuf协议介绍
前言 在你学习爬虫的知识过程中是否遇到下面的类型.如果有兴趣学习一下或者了解相关知识的,且不嫌在下才疏学浅,可以参考一下.欢迎各位网友的指正. 首先叙述一下问题的会出现的式样. 你可能会在请求参数中看 ...
- awk内置函数、外部变量
外部变量 ①获取外部变量 格式: awk '{action}' 变量名=变量值 ,这样传入变量可以在action中获得值. 示例: test='awk test'--day-5 外部变量 ①获取外部变 ...
- 使用Runnable和Callable接口实现多线程的区别
使用Runnable和Callable接口实现多线程的区别 先看两种实现方式的步骤: 1.实现Runnable接口 public class ThreadDemo{ public static voi ...
- vue2升级vue3指南(一)—— 环境准备和构建篇
1.nodejs和npm 注意二者的版本,版本过低需要升级,本人升级后的版本如下: $ node -v v16.15.1 $ npm -v 8.11.0 2.package.json 和依赖升级 由于 ...