链接:https://www.nowcoder.com/acm/contest/105/F
来源:牛客网

有q个单向传送阵,每个传送阵各有一个入口和一个出口,入口和出口都在迷宫的格子里,当走到或被传送到一个有传送阵入口的格子时,小明可以选择是否开启传送阵。如果开启传送阵,小明就会被传送到出口对应的格子里,这个过程会花费3秒;如果不开启传送阵,将不会发生任何事情,小明可以继续向上下左右四个方向移动。

一个格子可能既有多个入口,又有多个出口,小明可以选择任意一个入口开启传送阵。使用传送阵是非常危险的,因为有的传送阵的出口在陷阱里,如果小明使用这样的传送阵,那他就会死亡。也有一些传送阵的入口在陷阱里,这样的传送阵是没有用的,因为小明不能活着进入。请告诉小明活着到达目的地的最短时间。

输入描述:

有多组数据。对于每组数据:
第一行有三个整数n,m,q(2≤ n,m≤300,0≤ q ≤ 1000)。
接下来是一个n行m列的矩阵,表示迷宫。
最后q行,每行四个整数

表示一个传送阵的入口在x

1

行y

1

列,出口在x

2

行y

2

列。

输出描述:

如果小明能够活着到达目的地,则输出最短时间,否则输出-1。
示例1

输入

5 5 1
..S..
.....
.###.
.....
..T..
1 2 3 3
5 5 1
..S..
.....
.###.
.....
..T..
3 3 1 2
5 5 1
S.#..
..#..
###..
.....
....T
0 1 0 2
4 4 2
S#.T
.#.#
.#.#
.#.#
0 0 0 3
2 0 2 2

输出

6
8
-1
3
bfs,注意,可能通过不止一个传送阵
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#pragma comment(linker, "/stck:1024000000,1024000000")
#define lowbit(x) (x&(-x))
#define max(x,y) (x>=y?x:y)
#define min(x,y) (x<=y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.1415926535897932384626433832
#define ios() ios::sync_with_stdio(true)
#define INF 0x3f3f3f3f
#define mem(a) ((a,0,sizeof(a)))
typedef long long ll;
struct node
{
int x,y,step;
node(int x=,int y=,int t=):x(x),y(y),step(step){};
bool operator<(const node a) const
{
return a.step<step;
}
}ans,pos;
int dir[][]={{,},{,},{,-},{-,}};
int n,m,k,xs,ys;
int dis[][];
char a[][];
int xa,xb,ya,yb;
vector<node>vis[][];
int bfs(int u,int v)
{
priority_queue<node>q;
memset(dis,INF,sizeof(dis));
ans.x=u;ans.y=v;
ans.step=;
dis[u][v]=;
q.push(ans);
while(!q.empty())
{
pos=q.top();
q.pop();
if(a[pos.x][pos.y]=='T') return pos.step;
for(int i=;i<vis[pos.x][pos.y].size();i++)
{
ans=vis[pos.x][pos.y][i];
if(dis[ans.x][ans.y]>pos.step+)
{
ans.step=pos.step+;
dis[ans.x][ans.y]=pos.step+;
q.push(ans);
}
}
for(int i=;i<;i++)
{
ans.x=pos.x+dir[i][];
ans.y=pos.y+dir[i][];
ans.step=pos.step+;
if(ans.x>= && ans.x<n && ans.y>= && ans.y<m && a[ans.x][ans.y]!='#' && dis[ans.x][ans.y]>ans.step)
{
dis[ans.x][ans.y]=pos.step+;
q.push(ans);
}
}
}
return -;
}
int main()
{
while(~scanf("%d%d%d",&n,&m,&k))
{
for(int i=;i<n;i++)
{
scanf("%s",&a[i]);
for(int j=;j<m;j++)
{
vis[i][j].clear();
if(a[i][j]=='S') xs=i,ys=j;
}
}
for(int i=;i<k;i++)
{
scanf("%d%d%d%d",&xa,&ya,&xb,&yb);
if(a[xa][ya]!='#' && a[xb][yb]!='#')
vis[xa][ya].push_back(node(xb,yb));
}
int cnt=bfs(xs,ys);
printf("%d\n",cnt);
}
return ;
}#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#pragma comment(linker, "/stck:1024000000,1024000000")
#define lowbit(x) (x&(-x))
#define max(x,y) (x>=y?x:y)
#define min(x,y) (x<=y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.1415926535897932384626433832
#define ios() ios::sync_with_stdio(true)
#define INF 0x3f3f3f3f
#define mem(a) ((a,0,sizeof(a)))
typedef long long ll;
struct node
{
int x,y,step;
node(int x=,int y=,int t=):x(x),y(y),step(step){};
bool operator<(const node a) const
{
return a.step<step;
}
}ans,pos;
int dir[][]={{,},{,},{,-},{-,}};
int n,m,k,xs,ys;
int dis[][];
char a[][];
int xa,xb,ya,yb;
vector<node>vis[][];
int bfs(int u,int v)
{
priority_queue<node>q;
memset(dis,INF,sizeof(dis));
ans.x=u;ans.y=v;
ans.step=;
dis[u][v]=;
q.push(ans);
while(!q.empty())
{
pos=q.top();
q.pop();
if(a[pos.x][pos.y]=='T') return pos.step;
for(int i=;i<vis[pos.x][pos.y].size();i++)
{
ans=vis[pos.x][pos.y][i];
if(dis[ans.x][ans.y]>pos.step+)
{
ans.step=pos.step+;
dis[ans.x][ans.y]=pos.step+;
q.push(ans);
}
}
for(int i=;i<;i++)
{
ans.x=pos.x+dir[i][];
ans.y=pos.y+dir[i][];
ans.step=pos.step+;
if(ans.x>= && ans.x<n && ans.y>= && ans.y<m && a[ans.x][ans.y]!='#' && dis[ans.x][ans.y]>ans.step)
{
dis[ans.x][ans.y]=pos.step+;
q.push(ans);
}
}
}
return -;
}
int main()
{
while(~scanf("%d%d%d",&n,&m,&k))
{
for(int i=;i<n;i++)
{
scanf("%s",&a[i]);
for(int j=;j<m;j++)
{
vis[i][j].clear();
if(a[i][j]=='S') xs=i,ys=j;
}
}
for(int i=;i<k;i++)
{
scanf("%d%d%d%d",&xa,&ya,&xb,&yb);
if(a[xa][ya]!='#' && a[xb][yb]!='#')
vis[xa][ya].push_back(node(xb,yb));
}
int cnt=bfs(xs,ys);
printf("%d\n",cnt);
}
return ;
}

2018年湘潭大学程序设计竞赛 maze(bfs)的更多相关文章

  1. 2018年湘潭大学程序设计竞赛 F - maze

    把点抽出来 跑个最短路就好啦. #include<bits/stdc++.h> #define LL long long #define pii pair<int,int> # ...

  2. 2018年湘潭大学程序设计竞赛G又见斐波那契

    链接:https://www.nowcoder.com/acm/contest/105/G来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536 ...

  3. 牛客网-2018年湘潭大学程序设计竞赛-F

    题目链接:https://www.nowcoder.com/acm/contest/105/F 解题思路:这道题第一眼直接思路就是搜索,但想了半天没想到有什么好办法搜,然后就转成最短路写了, 因为多入 ...

  4. 2018年湘潭大学程序设计竞赛 H统计颜色

    链接:https://www.nowcoder.com/acm/contest/105/H来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536 ...

  5. 2018年湘潭大学程序设计竞赛 G- 又见斐波那契

    推一推矩阵直接快速幂. #include<bits/stdc++.h> #define LL long long #define pii pair<int,int> #defi ...

  6. 2018年湘潭大学程序设计竞赛 E 吃货

    题目描述 作为一个标准的吃货,mostshy又打算去联建商业街觅食了.混迹于商业街已久,mostshy已经知道了商业街的所有美食与其价格,而且他给每种美食都赋予了一个美味度,美味度越高表示他越喜爱这种 ...

  7. 2018年湘潭大学程序设计竞赛G又见斐波那契(矩阵快速幂)

    题意 题目链接 Sol 直接矩阵快速幂 推出来的矩阵应该长这样 \begin{equation*}\begin{bmatrix}1&1&1&1&1&1\\1 & ...

  8. 2018年湘潭大学程序设计竞赛 Fibonacci进制

    Fibonacci数是非常有名的一个数列,它的公式为 f(n)=f(n-1)+f(n-2),f(0)=1,f(1)=2.  我们可以把任意一个数x表示成若干不相同的Fibonacci数的和, 比如说1 ...

  9. 2018中国大学生程序设计竞赛 - 网络选拔赛 1001 - Buy and Resell 【优先队列维护最小堆+贪心】

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6438 Buy and Resell Time Limit: 2000/1000 MS (Java/O ...

随机推荐

  1. swoole-简单的异步执行

    swoole-简单的异步执行 标签(空格分隔): php 理解 一个IO操作其实分成了两个步骤:发起IO请求和实际的IO操作. 阻塞IO和非阻塞IO的区别在于第一步,发起IO请求是否会被阻塞,如果阻塞 ...

  2. gb2312和gbk互转

    String deStr = System.Web.HttpUtility.UrlEncode("欢迎", Encoding.GetEncoding("GBK" ...

  3. 解决IE下的li中img多余4px的问题--IE6有的问题

    为了对比明显,这里用img标签占位,但是背景用纯黑色 <ul> <li> <img src="" alt="" />< ...

  4. Timer 的 schedule()方法

    1.timer.schedule(new MyTask(),long time1,long time2); 第一个参数是TimerTask类,使用者要继承该类,并实现run()方法,因为TimerTa ...

  5. 向Vue实例混入plusready

    (function () { var onPlusReady = function (callback, context = this) { if (window.plus) { callback.c ...

  6. 【转载】犀利的 oracle 注入技术

    介绍一个在web上通过oracle注入直接取得主机cmdshell的方法. 以下的演示都是在web上的sql plus执行的,在web注入时 把select SYS.DBMS_EXPORT_EXTEN ...

  7. ZBrush功能特性之法线贴图

    ZMapper是ZBrush2.0推出的免费法线贴图插件.使用ZBrush革命性的多级别精度和新型的照明-快速光线跟踪器,ZMapper可以在几秒内产生适用于任何游戏引擎的法线贴图,不过3.5版本以后 ...

  8. 人工智能,你到底是天使or魔鬼?

    人工智能的概念早在60多年前就被提出,但又一度沉寂.随着谷歌人工智能程序AlphaGo(阿尔法狗)战胜围棋世界冠军李世石,再次为世人瞩目.然而,与无限风光一起相伴而来的,还有关于人工智能的种种争议! ...

  9. 入门python:《Python编程快速上手让繁琐工作自动化》中英文PDF+代码

    入门推荐学习<python编程快速上手>前6章是python的基础知识,通俗易懂地讲解基础,初学者容易犯错的地方,都会指出来.从第三章开始,每章都有一个实践项目,用来巩固前面所学的知识. ...

  10. react-native 编译 undefined is not an object (evaluating '_react2.PropTypes.func')

    情况通报: 因为是我的二维码模块报错,提示报错代码如下 重要信息是下面的红色字体部分(Android 模拟器红屏) undefined is not an object (evaluating '_r ...