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 ...
随机推荐
- uniapp设置竖屏
//在APP.vue中的onLaunch钩子写入plus.screen.lockOrientation('portrait-primary');
- Java 接口返回值集合防止空指针
接口 返回值为一个集合 public interface UserSearchService{ List<User> listUser(); } 接口实现 public List<U ...
- MVC - MVC的工作流程
MVC 是Model-View-Controller的简写."Model" 代表的是应用的业务逻辑(通过JavaBean,EJB组件实现), "View" 是应 ...
- nvm安装与使用及乱码问题
前端开发工作中经常负责多个项目(新项目.多年的老项目及团队合作项目),经常会遇到npm install安装依赖包或者启动本地服务时依赖报错的情况,大多数是因为NodeJS和npm与依赖之间版本的问题, ...
- php为图片添加水印
<?php /** * 图片加水印(适用于png/jpg/gif格式) * * @author flynetcn * * @param $srcImg 原图片 * @param $waterIm ...
- 左右手切换工具xmouse v1.2版本发布
Xmouse 方便的切换鼠标左右键,因为功能非常简单,所以支持.net framework 2.0及以上 windows环境就可以了,目前已测试win7.win10可用. 关于为什么做这么个东西,那是 ...
- javaweb_Http学习
1. 什么是HTTP? HTTP(超文本传输协议)是一个简单的请求-响应协议,它通常运行在TCP之上. 文本:html,字符串,~... 超文本:图片,音乐,视频,定位,地图..... 默认端口:80 ...
- vue按需引入第三方ui插件优化
components.js import { fullScreenContainer, borderBox12, scrollBoard, loading, borderBox10, borderBo ...
- 如何查看/修改Redis密码
一.修改密码: 打开redis.windows.conf文件,默认是没有红框框里这句话的,因为默认密码是"",就是没有,跟MySql一样. 加上这句话意思就是密码修改为 root ...
- MySQL查询为什么没走索引?这篇文章带你全面解析
工作中,经常遇到这样的问题,我明明在MySQL表上面加了索引,为什么执行SQL查询的时候却没有用到索引? 同一条SQL有时候查询用到了索引,有时候却没用到索引,这是咋回事? 原因可能是索引失效了,失效 ...