Codeforces 676D Theseus and labyrinth 模拟+bfs
2016-06-06 22:06
453 查看
D. Theseus and labyrinth
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Theseus has just arrived to Crete to fight Minotaur. He found a labyrinth that has a form of a rectangular field of size n × 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.
Input
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 n from
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.
Output
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.
题意:有一个n*m的地图,每个点代表一个房间,每个房间可能有四个门,例如>代表右边只有一个门在右边即只能向右走,L代表左边没有门只能除了左其他都可以走等等。现在给出起点和终点,每次你可以把全部房间旋转90度或者移动到相邻的房间,但前提是两个房间之间都有有门,现在要你求起点出发到终点的最少时间。
思路:用vis[x][y][state]来标记当前元素已在队列当中,state表示当前全部房间旋转了多少次。head为当前位置,next为下一步的可以到达的位置,g[head.u][head.v] & ((1<<(i+next.state)%4))(判断当前的房间是否有门)&& g[next.u][next.v] & ((1<<(i+next.state+2)%4))(判断相邻的房间是否有门), i代表当前准备走的方向,g[x][y]代表每个方向是否可以走的十进制数值,例如0001代表左边有门,每位分别代表上右下左方向是否有门,1代表有,0代表没,最先到达终点的即为最小操作数;
代码:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#include<bitset>
#include<utility>
#include<functional>
#include<iomanip>
#include<sstream>
#include<ctime>
using namespace std;
#define maxn int(1e3+20)
#define inf int(0x3f3f3f3f)
#define mod int(1e9+7)
#define PI acos(-1.0)
typedef long long ll;
char c;
int g[maxn][maxn],n,m;
struct Point
{
int u,v,time,state;
Point(){time = 0;};
Point(int _u, int _v, int _time, int _state):u(_u),v(_v),time(_time),state(_state){}
}s,t;
int fun(char x)
{
switch(x)
{
case '^': return 8;break;
case 'v': return 2;break;
case '<': return 1;break;
case '>': return 4;break;
case '+': return 15;break;
case '-': return 5;break;
case '|': return 10;break;
case 'L': return 14;break;
case 'R': return 11;break;
case 'U': return 7;break;
case 'D': return 13;break;
case '*': return 0;break;
}
}
int dir[4][2] ={{0,-1},{1,0},{0,1},{-1,0}};
bool vis[maxn][maxn][5];
int bfs()
{
memset(vis,0,sizeof(vis));
queue<Point>q;
Point next,head;
q.push(s);
vis[s.u][s.v][s.state] = 1;
while(!q.empty())
{
head = q.front(); q.pop();
if(head.u == t.u && head.v == t.v){
return head.time;
}
if(!vis[head.u][head.v][(head.state+1)%4]){
q.push(Point(head.u, head.v,head.time +1,(head.state+1)%4));
vis[head.u][head.v][(head.state+1)%4] = 1;
}
for(int i = 0; i<4; i++)
{
next.u = head.u + dir[i][0];
next.v = head.v + dir[i][1];
if(g[next.u][next.v] != 0)
{
next.state = head.state;
if(next.u >= 1 && next.u <= n && next.v >= 1 && next.v<= m && g[head.u][head.v] & ((1<<(i+next.state)%4))
&& g[next.u][next.v] & ((1<<(i+next.state+2)%4))){
if(!vis[next.u][next.v][next.state]){
next.time = head.time + 1;
q.push(next);
vis[next.u][next.v][next.state] = 1;
}
}
}
}
}
return -1;
}
int main()
{
#ifdef CDZSC_June
freopen("t.txt", "r", stdin);
#endif
while(~scanf("%d%d",&n,&m))
{
for(int i = 1; i<=n; i++){
for(int j = 1; j<=m; j++){
scanf(" %c",&c);
g[i][j] = fun(c);
}
}
scanf("%d%d",&s.u,&s.v);
scanf("%d%d",&t.u,&t.v);
s.state = t.state = 0;
printf("%d\n",bfs());
}
return 0;
}
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Theseus has just arrived to Crete to fight Minotaur. He found a labyrinth that has a form of a rectangular field of size n × 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.
Input
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 n from
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.
Output
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.
题意:有一个n*m的地图,每个点代表一个房间,每个房间可能有四个门,例如>代表右边只有一个门在右边即只能向右走,L代表左边没有门只能除了左其他都可以走等等。现在给出起点和终点,每次你可以把全部房间旋转90度或者移动到相邻的房间,但前提是两个房间之间都有有门,现在要你求起点出发到终点的最少时间。
思路:用vis[x][y][state]来标记当前元素已在队列当中,state表示当前全部房间旋转了多少次。head为当前位置,next为下一步的可以到达的位置,g[head.u][head.v] & ((1<<(i+next.state)%4))(判断当前的房间是否有门)&& g[next.u][next.v] & ((1<<(i+next.state+2)%4))(判断相邻的房间是否有门), i代表当前准备走的方向,g[x][y]代表每个方向是否可以走的十进制数值,例如0001代表左边有门,每位分别代表上右下左方向是否有门,1代表有,0代表没,最先到达终点的即为最小操作数;
代码:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#include<bitset>
#include<utility>
#include<functional>
#include<iomanip>
#include<sstream>
#include<ctime>
using namespace std;
#define maxn int(1e3+20)
#define inf int(0x3f3f3f3f)
#define mod int(1e9+7)
#define PI acos(-1.0)
typedef long long ll;
char c;
int g[maxn][maxn],n,m;
struct Point
{
int u,v,time,state;
Point(){time = 0;};
Point(int _u, int _v, int _time, int _state):u(_u),v(_v),time(_time),state(_state){}
}s,t;
int fun(char x)
{
switch(x)
{
case '^': return 8;break;
case 'v': return 2;break;
case '<': return 1;break;
case '>': return 4;break;
case '+': return 15;break;
case '-': return 5;break;
case '|': return 10;break;
case 'L': return 14;break;
case 'R': return 11;break;
case 'U': return 7;break;
case 'D': return 13;break;
case '*': return 0;break;
}
}
int dir[4][2] ={{0,-1},{1,0},{0,1},{-1,0}};
bool vis[maxn][maxn][5];
int bfs()
{
memset(vis,0,sizeof(vis));
queue<Point>q;
Point next,head;
q.push(s);
vis[s.u][s.v][s.state] = 1;
while(!q.empty())
{
head = q.front(); q.pop();
if(head.u == t.u && head.v == t.v){
return head.time;
}
if(!vis[head.u][head.v][(head.state+1)%4]){
q.push(Point(head.u, head.v,head.time +1,(head.state+1)%4));
vis[head.u][head.v][(head.state+1)%4] = 1;
}
for(int i = 0; i<4; i++)
{
next.u = head.u + dir[i][0];
next.v = head.v + dir[i][1];
if(g[next.u][next.v] != 0)
{
next.state = head.state;
if(next.u >= 1 && next.u <= n && next.v >= 1 && next.v<= m && g[head.u][head.v] & ((1<<(i+next.state)%4))
&& g[next.u][next.v] & ((1<<(i+next.state+2)%4))){
if(!vis[next.u][next.v][next.state]){
next.time = head.time + 1;
q.push(next);
vis[next.u][next.v][next.state] = 1;
}
}
}
}
}
return -1;
}
int main()
{
#ifdef CDZSC_June
freopen("t.txt", "r", stdin);
#endif
while(~scanf("%d%d",&n,&m))
{
for(int i = 1; i<=n; i++){
for(int j = 1; j<=m; j++){
scanf(" %c",&c);
g[i][j] = fun(c);
}
}
scanf("%d%d",&s.u,&s.v);
scanf("%d%d",&t.u,&t.v);
s.state = t.state = 0;
printf("%d\n",bfs());
}
return 0;
}
相关文章推荐
- javaType字符数组与jdbcType字符串转换(mybatis)
- ARM中的常用指令
- 偏最小二乘法
- [Leetcode]263. Ugly Number
- 贪吃蛇----第一个写出的游戏
- 北航2016集训队选拔赛解题报告
- MongoDB for Java Programmer ——3
- Android蓝牙篇:(一)基础篇
- 关于file_operations结构体
- 拉链表设计
- 文档生成工具doxygen+图像生成工具GraphViz
- 文档生成工具doxygen+图像生成工具GraphViz
- android 获取进程、服务、任务列表
- Bootstrap 选项卡和面板
- 动作栏和选项菜单
- 今天大概了解了一下百度的点击率
- java中的构造器
- 分析函数调用关系图(call graph)的几种方法
- 分析函数调用关系图(call graph)的几种方法
- 在vim中使用cscope查找调用、定义函数和变量的地方