Sicily 1215: 脱离地牢(BFS)
这道题按照题意直接BFS即可,主要要注意题意中的相遇是指两种情况:一种是同时到达同一格子,另一种是在移动时相遇,如Paris在(1,2),而Helen在(1,2),若下一步Paris到达(1,1),而Helen达到(1,2),这种情况也算是相遇。

#include<bits/stdc++.h>
using namespace std;
int _move[][] = {{-, }, {, }, {, -}, {, }};
int visit[][][][];
int Helen_move[];
int n,m;
char _map[][];
struct Node{
int x1, y1;//Paris
int x2, y2;//Helen
int step;
}; Node bfs(int x1, int y1, int x2, int y2, int n, int m){
Node front;
front.x1 = x1;
front.y1 = y1;
front.x2 = x2;
front.y2 = y2;
front.step = ;
queue<Node> q;
q.push(front);
visit[x1][y1][x2][y2] = ;
while(!q.empty()){
front = q.front(); q.pop();
if(front.step > ) {
return front;//若大于255,则返回
}
for(int i = ; i < ; i++){
int Paris_x = front.x1 + _move[i][];//走一步
int Paris_y = front.y1 + _move[i][];
if( <= Paris_x && Paris_x < n && <= Paris_y && Paris_y < m &&
_map[Paris_x][Paris_y] != '#' && _map[Paris_x][Paris_y] != '!'){
int k = Helen_move[i]; int Helen_x = front.x2 + _move[k][];
int Helen_y = front.y2 + _move[k][]; if( <= Helen_x && Helen_x < n && <= Helen_y && Helen_y < m){
if(_map[Helen_x][Helen_y] == '#'){//撞墙则不走
Helen_x = front.x2;
Helen_y = front.y2;
}
else if(_map[Helen_x][Helen_y] == '!') continue;//遇到熔浆 if(visit[Paris_x][Paris_y][Helen_x][Helen_y] == ) continue;
Node tmp = front;
tmp.x1 = Paris_x;
tmp.y1 = Paris_y;
tmp.x2 = Helen_x;
tmp.y2 = Helen_y;
tmp.path[tmp.step] = i;
tmp.step = front.step + ;
q.push(tmp);
visit[Paris_x][Paris_y][Helen_x][Helen_y] = ;
if(Paris_x == Helen_x && Paris_y == Helen_y){//直接相遇
return tmp;
}
if(Paris_x == front.x2 && Paris_y == front.y2 &&
Helen_x == front.x1 && Helen_y == front.y1){
return tmp;//交差相遇
}
} }
}
}
front.step = ;
return front;
}
int main(){
cin >> n >> m; int x1, y1;
int x2, y2;
memset(visit, , sizeof(visit));
memset(Helen_move, , sizeof(Helen_move));
for(int i = ; i < n; i++){
for(int j = ; j < m; j++){
cin >> _map[i][j];
if(_map[i][j] == 'P'){//Paris的位置
x1 = i;
y1 = j;
}
if(_map[i][j] == 'H'){//Helen的位置
x2 = i;
y2 = j;
}
}
} for(int i = ; i < ; i++){
char c;
cin >> c;
if(c == 'N')Helen_move[i] = ;//Paris走时Helen的方向
else if(c == 'S')Helen_move[i] = ;
else if(c == 'W')Helen_move[i] = ;
else if(c == 'E')Helen_move[i] = ;
}
Node tmp = bfs(x1, y1, x2, y2, n, m);
if(tmp.step > ) cout << "Impossible" << endl;
else{
cout << tmp.step << endl;
} } /*
5 5
#####
#P#.#
#H!.#
#.#.#
#####
WNSE
*/
Sicily 1215: 脱离地牢(BFS)的更多相关文章
- sicily 1215. 脱离地牢
Description 在一个神秘的国度里,年轻的王子Paris与美丽的公主Helen在一起过着幸福的生活.他们都随身带有一块带磁性的阴阳魔法石,身居地狱的魔王Satan早就想得到这两块石头了,只要把 ...
- 20180610模拟赛T1——脱离地牢
Description 在一个神秘的国度里,年轻的王子Paris与美丽的公主Helen在一起过着幸福的生活.他们都随身带有一块带磁性的阴阳魔法石,身居地狱的魔王Satan早就想着得到这两块石头了,只要 ...
- Sicily 1048: Inverso(BFS)
题意是给出一个3*3的黑白网格,每点击其中一格就会使某些格子的颜色发生转变,求达到目标状态网格的操作.可用BFS搜索解答,用vector储存每次的操作 #include<bits/stdc++. ...
- sicily 题目分类
为了方便刷题,直接把分类保存下来方便来找. 转自:http://dengbaoleng.iteye.com/blog/1505083 [数据结构/图论] 1310Right-HeavyTree笛卡尔树 ...
- 地牢逃脱 (BFS)
题意:给定一个 n 行 m 列的地牢,其中 '.' 表示可以通行的位置,'X' 表示不可通行的障碍,牛牛从 (x0 , y0 ) 位置出发,遍历这个地牢,和一般的游戏所不同的是,他每一步只能按照一些指 ...
- 2019 校内赛 RPG的地牢猎手(bfs+优先队列)
Problem Description Luke最近沉迷一款RPG游戏,游戏中角色可以进入地牢关卡,只要顺利走出地牢就可以获得奖励.地牢表示为n行m列的块矩阵,其中每个块只可以是障碍块.入口.出口或数 ...
- BFS简单题套路_Codevs 1215 迷宫
BFS 简单题套路 1. 遇到迷宫之类的简单题,有什么行走方向的,先写下面的 声明 ; struct Status { int r, c; Status(, ) : r(r), c(c) {} // ...
- Sicily 1444: Prime Path(BFS)
题意为给出两个四位素数A.B,每次只能对A的某一位数字进行修改,使它成为另一个四位的素数,问最少经过多少操作,能使A变到B.可以直接进行BFS搜索 #include<bits/stdc++.h& ...
- Sicily 1051: 魔板(BFS+排重)
相对1150题来说,这道题的N可能超过10,所以需要进行排重,即相同状态的魔板不要重复压倒队列里,这里我用map储存操作过的状态,也可以用康托编码来储存状态,这样时间缩短为0.03秒.关于康托展开可以 ...
随机推荐
- [LeetCode] Number of Connected Components in an Undirected Graph 无向图中的连通区域的个数
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- [LeetCode] Read N Characters Given Read4 II - Call multiple times 用Read4来读取N个字符之二 - 多次调用
The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actu ...
- [LeetCode] LRU Cache 最近最少使用页面置换缓存器
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- [LeetCode] Combinations 组合项
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- [LeetCode] Wildcard Matching 外卡匹配
Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...
- EF使用CodeFirst方式生成数据库&技巧经验
前言 EF已经发布很久了,也有越来越多的人在使用EF.如果你已经能够非常熟练的使用EF的功能,那么就不需要看了.本文意在将自己使用EF的方式记录下来备忘,也是为了给刚刚入门的同学一些指导.看完此文,你 ...
- SQLite源程序分析之回叫机制
1.SQL访问数据库非常方便,只需简单的三个函数: sqlite3_open(char* szDbFileName, sqlite3 ** db) sqlite3_exec(sqlite3 *db, ...
- Android基础总结(三)
测试 黑盒测试 测试逻辑业务 白盒测试 测试逻辑方法 根据测试粒度 方法测试:function test 单元测试:unit test 集成测试:integration test 系统测试:syste ...
- Python 【第九章】 Django基础
在windows 命令行上安装Django 在CMD命令行中输入以下命令进行安装. pip install Django 在windows python安装目录上会出现 一个django-admin. ...
- Sublime Text编辑器的12个技巧和诀窍
本文为您提供Sublime Text编辑器的12个技巧和诀窍,深入挖掘这个看似简洁的代码编辑器,背后所隐藏的实现各种高级功能的无限可能. 1) 选择 以下是一些Sublime Text选择文本的快捷键 ...