【Codeforces】Codeforces Round #354 (Div. 2)D. Theseus and labyrinth | 搜索、BFS

200 阅读3分钟

本文已参与「新人创作礼」活动, 一起开启掘金创作之路。

题目链接

Problem - 676D - Codeforces

题目

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)(x_T, y_T) — the block in the row xTx_T and column yTy_T. Theseus know that the Minotaur is hiding in block (xM,yM)(x_M, y_M) 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.

题目大意

给定一个 n×mn\times m 的迷宫,迷宫的每一格都有符号,符号的含义如下:

  • + 表示这个格子有 4 扇门——通向上下左右的相邻格子;
  • - 表示这个格子有 2 扇门——通向左边和右边的相邻格子;
  • | 表示这个格子有 2 扇门——通向顶部和底部的相邻格子;
  • ^ 表示这个格子有 1 扇门——通向顶部的相邻格子;
  • > 表示这个格子有 1 扇门——通向右边的相邻格子;
  • < 表示这个格子有 1 扇门——通向左边的相邻格子;
  • v 表示这个格子有 1 扇门——通向底部的相邻格子;
  • L 表示这个格子有 3 扇门——通向除左边之外的所有相邻格子;
  • R 表示这个格子有 3 扇门——通向除右边之外的所有相邻格子;
  • U 表示这个格子有 3 扇门——通向除上面之外的所有相邻格子;
  • D 表示这个格子有 3 扇门——通向除底部之外的所有相邻格子;
  • * 表示这个格子是一堵墙,没有门。

想要从 A 格子离开到达相邻的格子 B,必须 A 和 B 两个格子都有通向邻边的门。可以花费一个时间去往相邻的可达的格子,或者把所有的格子顺时针旋转 90°,问最小的步数从输入的起点到达输入的重点。

思路

直接 BSF 即可。

代码

#include <bits/stdc++.h>
using namespace std;
int turn[12][4]={
	{0,0,0,0},
	{1,2,1,2},
	{2,1,2,1},
	{3,4,5,6},
	{4,5,6,3},
	{5,6,3,4},
	{6,3,4,5},
	{7,8,9,10},
	{8,9,10,7},
	{9,10,7,8},
	{10,7,8,9},
	{11,11,11,11}
};
const int N=1005;
using LL=long long;
int n,m;
int a[N][N],ex,ey,sx,sy;
char ch;
int h,t;
int ans[N][N][4];
struct asdf{
	int x,y,ts,stp;
	bool operator < (const asdf a) const
	{
		return stp>a.stp;
	}
};
priority_queue<asdf> q;
void pushback(int x,int y,int tn,int val)
{
	if (ans[x][y][tn]>val)
	{
		ans[x][y][tn]=val;
		q.push({x,y,tn,val});
	}
}
int t1,t2;
void Ucheck(int x,int y,int tn,int stp)
{
	t1=turn[a[x][y]][tn];
	t2=turn[a[x-1][y]][tn];
	if ((t1==2||t1==3||t1==7||t1==9||t1==10||t1==11)&&
		(t2==2||t2==5||t2==7||t2==8||t2==9||t2==11)) pushback(x-1,y,tn,stp);
}
void Dcheck(int x,int y,int tn,int stp)
{
	t1=turn[a[x][y]][tn];
	t2=turn[a[x+1][y]][tn];
	if ((t2==2||t2==3||t2==7||t2==9||t2==10||t2==11)&&
		(t1==2||t1==5||t1==7||t1==8||t1==9||t1==11)) pushback(x+1,y,tn,stp);	
}
void Lcheck(int x,int y,int tn,int stp)
{
	t1=turn[a[x][y]][tn];
	t2=turn[a[x][y-1]][tn];
	if ((t1==1||t1==6||t1==8||t1==9||t1==10||t1==11)&&
		(t2==1||t2==4||t2==7||t2==8||t2==10||t2==11)) pushback(x,y-1,tn,stp);
	
}
void Rcheck(int x,int y,int tn,int stp)
{
	t1=turn[a[x][y]][tn];
	t2=turn[a[x][y+1]][tn];
	if ((t2==1||t2==6||t2==8||t2==9||t2==10||t2==11)&&
		(t1==1||t1==4||t1==7||t1==8||t1==10||t1==11)) pushback(x,y+1,tn,stp);
}
int main()
{
	scanf("%d%d",&n,&m);
	for (int i=1;i<=n;++i)
		for (int j=1;j<=m;++j)
		{
			cin>>ch;	
			if (ch=='*') a[i][j]=0;
			if (ch=='-') a[i][j]=1;
			if (ch=='|') a[i][j]=2;
			if (ch=='^') a[i][j]=3;
			if (ch=='>') a[i][j]=4;
			if (ch=='v') a[i][j]=5;
			if (ch=='<') a[i][j]=6;
			if (ch=='L') a[i][j]=7;
			if (ch=='U') a[i][j]=8;
			if (ch=='R') a[i][j]=9;
			if (ch=='D') a[i][j]=10;
			if (ch=='+') a[i][j]=11;
			for (int qwq=0;qwq<4;++qwq) ans[i][j][qwq]=1<<30;
		}
	scanf("%d%d%d%d",&sx,&sy,&ex,&ey);
	pushback(sx,sy,0,0);
	int t1,t2;
	while (!q.empty())
	{
		asdf tp=q.top();
		if (tp.x==ex&&tp.y==ey)
		{
			printf("%d",tp.stp);
			return 0;
		}
		q.pop();
		for (int i=0;i<4;++i)
		{
			Ucheck(tp.x,tp.y,(tp.ts+i)%4,tp.stp+i+1);
			Dcheck(tp.x,tp.y,(tp.ts+i)%4,tp.stp+i+1);
			Lcheck(tp.x,tp.y,(tp.ts+i)%4,tp.stp+i+1);
			Rcheck(tp.x,tp.y,(tp.ts+i)%4,tp.stp+i+1);
		}
	}
	printf("-1");
	return 0;
}