Codeforces Round #354 (Div. 2)-D
Theseus has just arrived to Crete to fight Minotaur. He found a labyrinth that has a form of a rectangular field of sizen × m and consists of blocks of size 1 × 1.
Each block of the labyrinth has a button that rotates all blocks 90 degrees clockwise. Each block rotates around its center and doesn't change its position in the labyrinth. Also, each block has some number of doors (possibly none). In one minute, Theseus can either push the button in order to rotate all the blocks 90 degrees clockwise or pass to the neighbouring block. Theseus can go from block A to some neighbouring block B only if block A has a door that leads to block B and block B has a door that leads to block A.
Theseus found an entrance to labyrinth and is now located in block (xT, yT) — the block in the row xT and column yT. Theseus know that the Minotaur is hiding in block (xM, yM) and wants to know the minimum number of minutes required to get there.
Theseus is a hero, not a programmer, so he asks you to help him.
The first line of the input contains two integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in labyrinth, respectively.
Each of the following n lines contains m characters, describing the blocks of the labyrinth. The possible characters are:
- «+» means this block has 4 doors (one door to each neighbouring block);
- «-» means this block has 2 doors — to the left and to the right neighbours;
- «|» means this block has 2 doors — to the top and to the bottom neighbours;
- «^» means this block has 1 door — to the top neighbour;
- «>» means this block has 1 door — to the right neighbour;
- «<» means this block has 1 door — to the left neighbour;
- «v» means this block has 1 door — to the bottom neighbour;
- «L» means this block has 3 doors — to all neighbours except left one;
- «R» means this block has 3 doors — to all neighbours except right one;
- «U» means this block has 3 doors — to all neighbours except top one;
- «D» means this block has 3 doors — to all neighbours except bottom one;
- «*» means this block is a wall and has no doors.
Left, right, top and bottom are defined from representing labyrinth as a table, where rows are numbered from 1 to nfrom top to bottom and columns are numbered from 1 to m from left to right.
Next line contains two integers — coordinates of the block (xT, yT) (1 ≤ xT ≤ n, 1 ≤ yT ≤ m), where Theseus is initially located.
Last line contains two integers — coordinates of the block (xM, yM) (1 ≤ xM ≤ n, 1 ≤ yM ≤ m), where Minotaur hides.
It's guaranteed that both the block where Theseus starts and the block where Minotaur is hiding have at least one door. Theseus and Minotaur may be initially located at the same block.
If Theseus is not able to get to Minotaur, then print -1 in the only line of the output. Otherwise, print the minimum number of minutes required to get to the block where Minotaur is hiding.
2 2
+*
*U
1 1
2 2
-1
2 3
<><
><>
1 1
2 1
4
Assume that Theseus starts at the block (xT, yT) at the moment 0.
题意:给定一个图表,然后是开始位置和目标位置,从开始到目标的最少时间,不存在输出-1. 每一秒可以向能走的方向走一步或者顺时针旋转全部按钮90度。注意如果你要从A->B,那么必须B->A才能从A走到B。
思路:BFS,按照思路模拟吧,比较麻烦的一道题目。 开始以为会TLE用的A*发现出来的不一定最优解。改成普通的队列就AC了。
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<bitset>
#include<queue>
#include<vector>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define PI 3.14159
const int MAXN = + ;
char g[MAXN][MAXN];
int dist[][] = { , , , -, , , -, }; //0:right 1:left 2:down 3:up
int n, m, vis[MAXN][MAXN][];
map<char, vector<int> >Move;
struct Node
{
int x, y;
int step;
char str;
int cnt;
int f;
Node(int a = , int b = , int c = , char d = '*', int e = , int ff = ) :x(a), y(b), step(c), str(d), cnt(e), f(ff){};
};
struct cmp
{
bool operator ()(const Node &a, const Node &b)const
{
return a.f>b.f;
}
};
void init() //初始化每个按钮能走的方向
{
vector<int> op1; op1.push_back(); op1.push_back(); op1.push_back(); op1.push_back(); Move['+'] = op1;
vector<int> op2; op2.push_back(); op2.push_back(); Move['-'] = op2;
vector<int> op3; op3.push_back(); op3.push_back(); Move['|'] = op3;
vector<int> op4; op4.push_back(); Move['^'] = op4;
vector<int> op5; op5.push_back(); Move['>'] = op5;
vector<int> op6; op6.push_back(); Move['<'] = op6;
vector<int> op7; op7.push_back(); Move['v'] = op7;
vector<int> op8; op8.push_back(); op8.push_back(); op8.push_back(); Move['L'] = op8;
vector<int> op9; op9.push_back(); op9.push_back(); op9.push_back(); Move['R'] = op9;
vector<int> op10; op10.push_back(); op10.push_back(); op10.push_back(); Move['U'] = op10;
vector<int> op11; op11.push_back(); op11.push_back(); op11.push_back(); Move['D'] = op11;
}
bool check(int x, int y)//判断越界
{
return x >= && x<n&&y >= && y<m;
}
int Get_F(Node a, int ex, int ey) //A*函数
{
return abs(a.x - ex) + abs(a.y - ey) + a.step;
}
char rotates(char str, int cnt) //得到rotates后按钮
{
char ST;
if (str == '-'){ cnt % ? ST = '|' : ST = '-'; }
else if (str == '|'){ cnt % ? ST = '-' : ST = '|'; }
else if (str == '^')
{
if (cnt % == ){ ST = '>'; }
else if (cnt % == ){ ST = 'v'; }
else if (cnt % == ){ ST = '<'; }
else{ ST = '^'; }
}
else if (str == '>')
{
if (cnt % == ){ ST = 'v'; }
else if (cnt % == ){ ST = '<'; }
else if (cnt % == ){ ST = '^'; }
else{ ST = '>'; }
}
else if (str == 'v')
{
if (cnt % == ){ ST = '<'; }
else if (cnt % == ){ ST = '^'; }
else if (cnt % == ){ ST = '>'; }
else{ ST = 'v'; }
}
else if (str == '<')
{
if (cnt % == ){ ST = '^'; }
else if (cnt % == ){ ST = '>'; }
else if (cnt % == ){ ST = 'v'; }
else { ST = '<'; }
}
else if (str == 'L')
{
if (cnt % == ){ ST = 'U'; }
else if (cnt % == ){ ST = 'R'; }
else if (cnt % == ){ ST = 'D'; }
else{ ST = 'L'; }
}
else if (str == 'R')
{
if (cnt % == ){ ST = 'D'; }
else if (cnt % == ){ ST = 'L'; }
else if (cnt % == ){ ST = 'U'; }
else{ ST = 'R'; }
}
else if (str == 'U')
{
if (cnt % == ){ ST = 'R'; }
else if (cnt % == ){ ST = 'D'; }
else if (cnt % == ){ ST = 'L'; }
else{ ST = 'U'; }
}
else if (str == 'D')
{
if (cnt % == ){ ST = 'L'; }
else if (cnt % == ){ ST = 'U'; }
else if (cnt % == ){ ST = 'R'; }
else{ ST = 'D'; }
}
else if (str == '+'){ ST = '+'; }
return ST;
}
bool checkMove(Node b, Node a,int op)//check B can to A?
{
char moveop = rotates(b.str, b.cnt);
if (op == )
{
for (int i = ; i < Move[moveop].size(); i++)
{
if (Move[moveop][i] == )
{
return true;
}
}
}
else if (op == )
{
for (int i = ; i < Move[moveop].size(); i++)
{
if (Move[moveop][i] == )
{
return true;
}
}
}
else if (op == )
{
for (int i = ; i < Move[moveop].size(); i++)
{
if (Move[moveop][i] == )
{
return true;
}
}
}
else
{
for (int i = ; i < Move[moveop].size(); i++)
{
if (Move[moveop][i] == )
{
return true;
}
}
}
return false;
}
int dfs(Node s, Node e)
{
//priority_queue<Node, vector<Node>, cmp >Q;
queue<Node>Q;
memset(vis, , sizeof(vis));
Q.push(s);
vis[s.x][s.y][s.cnt % ] = ;
while (!Q.empty())
{
Node Top = Q.front(); Q.pop();
//Node Top = Q.top(); Q.pop();
if (Top.x == e.x&&Top.y == e.y)
{
return Top.step;
}
Node next;
//rotates:
next.x = Top.x; next.y = Top.y; next.str = g[next.x][next.y]; next.cnt = Top.cnt + ; next.step = Top.step + ; next.f = Get_F(next, e.x, e.y);
if (vis[next.x][next.y][next.cnt % ] == )
{
vis[next.x][next.y][next.cnt % ] = ;
Q.push(next);
} //Move:
next.cnt = Top.cnt; next.str = g[next.x][next.y];
char op = rotates(next.str, next.cnt);
for (int i = ; i<Move[op].size(); i++)
{
next.x = Top.x + dist[Move[op][i]][];
next.y = Top.y + dist[Move[op][i]][];
if (check(next.x, next.y) && g[next.x][next.y] != '*'&&!vis[next.x][next.y][next.cnt % ])
{
next.str = g[next.x][next.y];
if (checkMove(next, Top,Move[op][i]))//检测是否B也能到A
{
vis[next.x][next.y][next.cnt % ] = ;
next.step = Top.step + ;
next.f = Get_F(next, e.x, e.y);
Q.push(next);
}
}
}
}
return -;
}
int main()
{
//#ifdef CYM
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
//#endif
init();
while (~scanf("%d%d", &n, &m)){
for (int i = ; i<n; i++)
{
scanf("%s", g[i]);
}
Node start, end;
scanf("%d%d", &start.x, &start.y);
start.x--; start.y--;
scanf("%d%d", &end.x, &end.y);
end.x--; end.y--;
start.step = , start.str = g[start.x][start.y], start.cnt = ; start.f = Get_F(start, end.x, end.y);
printf("%d\n", dfs(start, end));
}
return ;
}
Codeforces Round #354 (Div. 2)-D的更多相关文章
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #354 (Div. 2)-C
C. Vasya and String 题目链接:http://codeforces.com/contest/676/problem/C High school student Vasya got a ...
- Codeforces Round #354 (Div. 2)-B
B. Pyramid of Glasses 题目链接:http://codeforces.com/contest/676/problem/B Mary has just graduated from ...
- Codeforces Round #354 (Div. 2)-A
A. Nicholas and Permutation 题目链接:http://codeforces.com/contest/676/problem/A Nicholas has an array a ...
- Codeforces Round #354 (Div. 2) D. Theseus and labyrinth
题目链接: http://codeforces.com/contest/676/problem/D 题意: 如果两个相邻的格子都有对应朝向的门,则可以从一个格子到另一个格子,给你初始坐标xt,yt,终 ...
- Codeforces Round #354 (Div. 2) C. Vasya and String
题目链接: http://codeforces.com/contest/676/problem/C 题解: 把连续的一段压缩成一个数,对新的数组求前缀和,用两个指针从左到右线性扫一遍. 一段值改变一部 ...
- Codeforces Round #354 (Div. 2)_Vasya and String(尺取法)
题目连接:http://codeforces.com/contest/676/problem/C 题意:一串字符串,最多改变k次,求最大的相同子串 题解:很明显直接尺取法 #include<cs ...
- Codeforces Round #354 (Div. 2) E. The Last Fight Between Human and AI 数学
E. The Last Fight Between Human and AI 题目连接: http://codeforces.com/contest/676/problem/E Description ...
- Codeforces Round #354 (Div. 2) D. Theseus and labyrinth bfs
D. Theseus and labyrinth 题目连接: http://www.codeforces.com/contest/676/problem/D Description Theseus h ...
随机推荐
- IOS-KVO&KVC
KVC(key value coding) 我们一般是通过调用set方法或属性的点语法来直接更改对象的状态,即对象的属性值,比如[stu setAge:10]; stu.age = 9; lKVC, ...
- 火狐----此地址使用了一个通常用于网络浏览以外的端口。出于安全原因,Firefox 取消了该请求。
FirFox打开80以外的端口,会弹出以下提示: “此地址使用了一个通常用于网络浏览以外的端口.出于安全原因,Firefox 取消了该请求.”.经网上搜索,解决方法如下: 在Firefox地址栏输入a ...
- 柔性数组 data[0]
struct MyData { int nLen; char data[0];}; 在结构中,data是一个数组名:但该数组没有元素:该数组的真实地址紧随结构体MyData之后,而这个地址 ...
- JavaScript基础——使用JavaScript对象
JavaScript有许多内置对象,如Number(数字).Array(数组).String(字符串).Date(日期)和Math(数学).这些内置对象都有成员属性和方法.除了JavaScript对象 ...
- mysql中select五种子句和统计函数
select 五种子句顺序 where 条件 group by 分组 having 把结果进行再次筛选 order by 排序 limit 取出条目 统计函数 max(列名) 求最大 min( ...
- mysql优化学习笔记
优化sql的一般步骤 通过show status了解各种sql的执行频率 定位执行效率低的sql语句 通过explain分析效率低的sql 通过show profile分析sql 通过trace分析优 ...
- Android Programming: Pushing the Limits -- Chapter 2: Efficient Java Code for Android
Android's Dalvik Java 与 Java SE 进行比较 Java代码优化 内存管理与分配 Android的多线程操作 Android’s Dalvik Java 与 Java SE ...
- 设计模式学习之命令模式(Command,行为型模式)(12)
一.命令模式的定义 命令模式属于对象的行为型模式.命令模式是把一个操作或者行为抽象为一个对象中,通过对命令的抽象化来使得发出命令的责任和执行命令的责任分隔开.命令模式的实现可以提供命令的撤销和恢复功能 ...
- Delphi面向对象---接口
从Delphi3开始支持接口.接口定义了能够与一个对象进行交互操作的一组过程和函数.对一个接口进行定义包含两个方面的内容: 1)一方面是实现这个接口 2)另一方面是定义接口的客户 一个类能够实现多个接 ...
- Delphi编译dll时出错"Cannot debug project unless a host application is defined.use the run|parameters...dialog box."
问题: 在编写DLL程序的时候,按下F9或者按下那个绿色的箭头,会报错,如下 原因: 是因为你按下的F9或者那个绿色箭头是表示“Run”这个程序,但是DLL不是可执行文件,所以当然不能够运行,所以就会 ...