Codeforces 676D Theseus and labyrinth(最短路)
来源:互联网 发布:最好的直通车软件 编辑:程序博客网 时间:2024/06/15 04:01
题意:
给定一个迷宫,起点和终点,每一个可以走的格子上都有不同的门的情况,每次都可以选择到达周围符合条件的格子或者是让所有格子的门顺时针旋转一下,问你从起点到重点的最短距离。
解法:
因为每次都是转90度,所以对于每个格子来说,都有四个状态,所以建立一个分层图,每层都代表个点的一种状态,然后直接BFS即可。
代码:
//// Created by CQU_CST_WuErli// Copyright (c) 2016 CQU_CST_WuErli. All rights reserved.////#pragma comment(linker, "/STACK:102400000,102400000")#include <iostream>#include <cstring>#include <cstdio>#include <cstdlib>#include <cctype>#include <cmath>#include <string>#include <vector>#include <map>#include <queue>#include <stack>#include <set>#include <algorithm>#include <sstream>#define CLR(x) memset(x,0,sizeof(x))#define OFF(x) memset(x,-1,sizeof(x))#define MEM(x,a) memset((x),(a),sizeof(x))#define BUG cout << "I am here" << endl#define lookln(x) cout << #x << "=" << x << endl#define SI(a) scanf("%d", &a)#define SII(a,b) scanf("%d%d", &a, &b)#define SIII(a,b,c) scanf("%d%d%d", &a, &b, &c)const int INF_INT=0x3f3f3f3f;const long long INF_LL=0x7f7f7f7f;const int MOD=1e9+7;const double eps=1e-10;const double pi=acos(-1);typedef long long ll;using namespace std;const int N = 1010;int n, m;int sx, sy, ex, ey;char mp[N][N];int dir[4][2] = {-1, 0, 0, 1, 1, 0, 0, -1};int vis[N][N][4];int ok[N][N][4];struct P { int x, y, sta, dis; P(int a, int b, int c, int d): x(a), y(b), sta(c), dis(d){}};void init() { CLR(vis); CLR(ok); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; ++j) { if (mp[i][j] == '+') ok[i][j][0] = ok[i][j][1] = ok[i][j][2] = ok[i][j][3] = 1; else if (mp[i][j] == '-') ok[i][j][1] = ok[i][j][3] = 1; else if (mp[i][j] == '|') ok[i][j][0] = ok[i][j][2] = 1; else if (mp[i][j] == '^') ok[i][j][0] = 1; else if (mp[i][j] == '>') ok[i][j][1] = 1; else if (mp[i][j] == 'v') ok[i][j][2] = 1; else if (mp[i][j] == '<') ok[i][j][3] = 1; else if (mp[i][j] == 'L') ok[i][j][0] = ok[i][j][1] = ok[i][j][2] = 1; else if (mp[i][j] == 'R') ok[i][j][0] = ok[i][j][2] = ok[i][j][3] = 1; else if (mp[i][j] == 'U') ok[i][j][1] = ok[i][j][2] = ok[i][j][3] = 1; else if (mp[i][j] == 'D') ok[i][j][0] = ok[i][j][1] = ok[i][j][3] = 1; } }}int bfs() { queue<P> q; q.push(P(sx, sy, 0, 0)); vis[sx][sy][0] = 1; while (q.size()) { P u = q.front(); q.pop(); int x = u.x, y = u.y, dis = u.dis, sta = u.sta; if (x == ex && y == ey) { return dis; } if (!vis[x][y][(sta + 1) % 4]) { vis[x][y][(sta + 1) % 4] = 1; q.push(P(x, y, (sta + 1) % 4, dis + 1)); } for (int i = 0; i < 4; i++) { if (ok[x][y][(i - sta + 4) % 4]) { int nx = x + dir[i][0], ny = y + dir[i][1]; if (nx < 1 || nx > n || ny < 1 || ny > m) continue; if (ok[nx][ny][(i - sta + 6) % 4] && !vis[nx][ny][sta]) { vis[nx][ny][sta] = 1; q.push(P(nx, ny, sta, dis + 1)); } } } } return -1;}int main(int argc, char const *argv[]) {#ifdef LOCAL freopen("C:\\Users\\john\\Desktop\\in.txt","r",stdin); // freopen("C:\\Users\\john\\Desktop\\out.txt","w",stdout);#endif while (SII(n, m) == 2) { for (int i = 1; i <= n; i++) scanf("%s", mp[i] + 1); init(); SII(sx, sy); SII(ex, ey); cout << bfs() << endl; } return 0;}
0 0
- Codeforces 676D Theseus and labyrinth(最短路)
- Codeforces 676D Theseus and labyrinth
- Codeforces Round #354 (Div. 2)676D. Theseus and labyrinth
- Codeforces 676D Theseus and labyrinth 模拟+bfs
- Codeforces 676D Theseus and labyrinth【模拟+Bfs】
- Codeforces 676D Theseus and labyrinth 模拟+bfs
- codeforces 354D. Theseus and labyrinth bfs
- 【25.93%】【676D】Theseus and labyrinth
- Codeforces Round #354 (Div. 2) D. Theseus and labyrinth (BFS)
- Codeforces Round #354 (Div. 2) D. Theseus and labyrinth
- Codeforces Round #354 (Div. 2) D Theseus and labyrinth
- Codeforces Round #354 (Div. 2) D. Theseus and labyrinth(BFS)
- Codeforces Round #354 (Div. 2) D.Theseus and labyrinth(BFS)
- Codeforces Round #354 (Div. 2) D. Theseus and labyrinth(bfs)
- [Codeforces Round #354 DIV2D (CF676D)] Theseus and labyrinth
- codeforces_676D. Theseus and labyrinth(bfs)
- D. Okabe and City codeforces 最短路
- 【Codeforces Round 354 (Div 2)D】【迷宫搜索BFS】Theseus and labyrinth 门门互达 可做旋转操作 最少步数起点到终点
- Eclipse搭建SSH(Struts2+Spring3+Hibernate3)框架项目教程
- 用go语言做一个简单的登录,大家不妨来看一下
- AspectJ切入点语法详解
- 反射
- 你所不知道的 HSTS
- Codeforces 676D Theseus and labyrinth(最短路)
- html向超链接中写入变量
- kafka分析
- Android实现计时与倒计时的几种方法(转)
- javascript学习之 正则表达式 (25)——exec方法
- 再论贝塞尔曲线
- Kubernetes Docker集群搭建过程
- 身份证服务器云识别SDK
- 洛谷 P1223 排队接水