2018年湘潭大学程序设计竞赛 maze(bfs)
链接:https://www.nowcoder.com/acm/contest/105/F
来源:牛客网
一个格子可能既有多个入口,又有多个出口,小明可以选择任意一个入口开启传送阵。使用传送阵是非常危险的,因为有的传送阵的出口在陷阱里,如果小明使用这样的传送阵,那他就会死亡。也有一些传送阵的入口在陷阱里,这样的传送阵是没有用的,因为小明不能活着进入。请告诉小明活着到达目的地的最短时间。
输入描述:
有多组数据。对于每组数据:
第一行有三个整数n,m,q(2≤ n,m≤300,0≤ q ≤ 1000)。
接下来是一个n行m列的矩阵,表示迷宫。
最后q行,每行四个整数
表示一个传送阵的入口在x
1
行y
1
列,出口在x
2
行y
2
列。
输出描述:
如果小明能够活着到达目的地,则输出最短时间,否则输出-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)的更多相关文章
- 2018年湘潭大学程序设计竞赛 F - maze
把点抽出来 跑个最短路就好啦. #include<bits/stdc++.h> #define LL long long #define pii pair<int,int> # ...
- 2018年湘潭大学程序设计竞赛G又见斐波那契
链接:https://www.nowcoder.com/acm/contest/105/G来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536 ...
- 牛客网-2018年湘潭大学程序设计竞赛-F
题目链接:https://www.nowcoder.com/acm/contest/105/F 解题思路:这道题第一眼直接思路就是搜索,但想了半天没想到有什么好办法搜,然后就转成最短路写了, 因为多入 ...
- 2018年湘潭大学程序设计竞赛 H统计颜色
链接:https://www.nowcoder.com/acm/contest/105/H来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536 ...
- 2018年湘潭大学程序设计竞赛 G- 又见斐波那契
推一推矩阵直接快速幂. #include<bits/stdc++.h> #define LL long long #define pii pair<int,int> #defi ...
- 2018年湘潭大学程序设计竞赛 E 吃货
题目描述 作为一个标准的吃货,mostshy又打算去联建商业街觅食了.混迹于商业街已久,mostshy已经知道了商业街的所有美食与其价格,而且他给每种美食都赋予了一个美味度,美味度越高表示他越喜爱这种 ...
- 2018年湘潭大学程序设计竞赛G又见斐波那契(矩阵快速幂)
题意 题目链接 Sol 直接矩阵快速幂 推出来的矩阵应该长这样 \begin{equation*}\begin{bmatrix}1&1&1&1&1&1\\1 & ...
- 2018年湘潭大学程序设计竞赛 Fibonacci进制
Fibonacci数是非常有名的一个数列,它的公式为 f(n)=f(n-1)+f(n-2),f(0)=1,f(1)=2. 我们可以把任意一个数x表示成若干不相同的Fibonacci数的和, 比如说1 ...
- 2018中国大学生程序设计竞赛 - 网络选拔赛 1001 - Buy and Resell 【优先队列维护最小堆+贪心】
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6438 Buy and Resell Time Limit: 2000/1000 MS (Java/O ...
随机推荐
- Java路径问题终于解决方式—可定位全部资源的相对路径寻址
1.在Java项目中,应该通过绝对路径訪问文件.下面为訪问的经常用法: 第一种方法:类名.class.getResource("/").getPath()+文件名称 另外一种方法: ...
- UVALive 4192/HDU 2959 Close Enough Computations 数学
Close Enough Computations Problem Description The nutritional food label has become ubiquitous. A sa ...
- JAVA配置Tomcat
1.下载tomcat,我jdk是1.8的,网上查了一下,说要安装tomcat8及以上的tomcat 尝试点击,弹出, 2.配置环境 3.安装通过cmd安装 4.点击开启服务 5.输入localhost ...
- iOS7实现带文本输入框的UIAlertView及获取TextField文本内容
if (customAlertView==nil) { customAlertView = [[UIAlertView alloc] initWithTitle:@"自定义服务器地址&quo ...
- mysql表空间传输(ERROR 1808) row_format设置
文章结构如下: 从MYSQL5.6版本开始,引入了传输表空间这个功能,可以把一张表从一个数据库移到另一个数据库或者机器上.迁移的时候很方便,尤其是大表. 由于本次达到测试使用版本5.6.38传到5.7 ...
- 模仿百度首页“元宵节汤圆”动图(js的定时任务:setInterval)
模仿百度首页“元宵节汤圆”动图:(js的定时任务:setInterval) 原理:需要一张切图,通过不断定位使得图片就像一帧一帧的图片在播放从而形成了动画 效果图: 切图地址: https://ss1 ...
- Servlet学习(三)——实例:用户登录并记录登陆次数
1.前提:在Mysql数据库下建立数据库web13,在web13下创建一张表user,插入几条数据如下: 2.创建HTML文件,命名为login,作为登录界面(以post方式提交) <!DOCT ...
- swift语言点评十七-Designated Initializers and Convenience Initializers
Swift defines two kinds of initializers for class types to help ensure all stored properties receive ...
- Linux下编译,安装Apache httpd服务器
环境:ubuntu 16.0.4 Apache官网下载Apache httpd压缩包:httpd-2.4.27.tar.gz,安装之前请确定安装了make工具,我安装的是GNU make 解压文件 s ...
- 2013-11-02 【webrebuild广州站】分享会纪要
为了不让自己沉浸个人的技术研究当中,也为了多去接触业界新技术新思想,今天去参加了webrebuild广州站的一个分享交流会,效果不错,有一些获益.听了四个主题,依据个人获取信息的情况来做个纪要(比较粗 ...