codeforces #354(div 2) D

题目链接:

http://codeforces.com/contest/676/problem/D

题目大意:

经典的迷宫问题,每个时刻有5种操作,走到相邻的房间或者让所有的房间顺时针旋转90°,房间的旋转

只改变门的方向,能走到相邻房间的条件是这间房和下一间房都要有门朝向对面。每次操作都会花费1秒,

求起点走到终点的最短时间。

解题思路:

可以用简单粗暴的bfs,用三维的vis数组记录每个房间的4种状态是否到达过,麻烦之处在于每个房间的门

的位置的情况比较多,需要额外写一个函数判断。我这里用整除表示了每个门能否通往下一个房间,sig所

对应的数字如果可以被2,3,5,7整除,则代表这个房间有一扇门通往对应的方向,mo2代表当前房间的门,

mo1代表目标房间的门,旋转一次则只需要对mo1和mo2进行对应的操作即可(这里需要注意mo1和mo2

移动的方向是相反的)。


AC代码:

#include <iostream>
#pragma comment(linker, "/STACK:1024000000,1024000000") 
#include <stdio.h>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <string>
#include <string.h>
#include <sstream>
#include <cctype>
#include <climits>
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <vector>
#include <iterator>
#include <algorithm>
#include <stack>
#include <functional>
//cout << "OK" << endl;
#define _clr(x,y) memset(x,y,sizeof(x))
#define _inf(x) memset(x,0x3f,sizeof(x))
#define pb push_back
#define mp make_pair
#define FORD(i,a,b) for (int i=(a); i<=(b); i++)
#define FORP(i,a,b) for (int i=(a); i>=(b); i--)
#define REP(i,n) for (int i=0; i<(n); i++)
using namespace std;
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
const double EULER = 0.577215664901532860;
const double PI = 3.1415926535897932384626;
const double E = 2.71828182845904523536028;

typedef long long LL;

int dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
LL pow_mod(LL a,LL n,LL m)
{
	if(n == 0) return 1;
	LL x = pow_mod(a,n>>1,m);
	LL ans = x*x%m;
	if(n&1) ans = ans*a%m;
	return ans;
}
int gcd(int a,int b){return b == 0 ? a : gcd(b,a%b);}
char mat[1005][1005] = {0};
const int maxn = 1000000+5;
int sig[256],vis[1005][1005][4] = {0};
struct node
{
	int x,y,sta,cost;
	bool operator < (const node& x) const{if(cost!=x.cost)return cost>x.cost;else return sta>x.sta;}
};
priority_queue<node> q;
int ans = INF;
int main()
{
    /*#ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif*/
    
	ios_base::sync_with_stdio(false); cin.tie(0);
	
	sig['+'] = 210;sig['-'] = 21;sig['|'] = 10;sig['^'] = 2;sig['>'] = 3;sig['<'] = 7;
	sig['v'] = 5;sig['L'] = 30;sig['R'] = 70;sig['U'] = 105;sig['D'] = 42;sig['*'] = 1;
	int mo1[4] = {5,7,2,3},mo2[4] = {2,3,5,7};
	int m,n;
	cin >> n >> m;
	for(int i = 1;i<=n;i++)
	for(int j = 1;j<=m;j++)
	cin >> mat[i][j];
	int sx,sy,ex,ey,com = 0;
	cin >> sx >> sy >> ex >> ey;
	
	node tmp,t;
	tmp.x = sx;tmp.y = sy;tmp.sta = 0;tmp.cost = 0;
	q.push(tmp);
	
	while(!q.empty())
	{
		t = q.top(); q.pop();
		if(t.x == ex && t.y == ey) ans = min(ans,t.cost);
		for(int i = 1;i<8;i++)
		{
			if(i >= 4)
			{
				tmp.x = t.x+dir[i-4][0]; tmp.y = t.y+dir[i-4][1];
				if(tmp.x<1 || tmp.y<1 || tmp.x>n || tmp.y>m) continue;
				if(sig[mat[t.x][t.y]] % mo2[(i-t.sta)%4] != 0) continue;
				if(sig[mat[tmp.x][tmp.y]] % mo1[(i-t.sta)%4] != 0) continue;
				tmp.sta = t.sta;
				tmp.cost = t.cost+1;
				
				if(vis[tmp.x][tmp.y][tmp.sta]<=tmp.cost && vis[tmp.x][tmp.y][tmp.sta])continue;
				vis[tmp.x][tmp.y][tmp.sta] = tmp.cost;
				q.push(tmp);
			}
			else
			{
				tmp.x = t.x; tmp.y = t.y;
				tmp.sta = (t.sta+i)%4;
				tmp.cost = t.cost+i;
				if(vis[tmp.x][tmp.y][tmp.sta]<=tmp.cost && vis[tmp.x][tmp.y][tmp.sta])continue;
				vis[tmp.x][tmp.y][tmp.sta] = tmp.cost;
				q.push(tmp);
			}
		}
	}
	if(ans==INF) cout << -1 << endl; else cout << ans << endl;

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值