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 ...
随机推荐
- nyoj--214--单调递增子序列(二)(二分查找+LIS)
单调递增子序列(二) 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 给定一整型数列{a1,a2...,an}(0<n<=100000),找出单调递增最长子序 ...
- WPF 基础
关于布局的规则 控件的布局应该由容器来决定,而不是通过自身使用margin之类的东西来控制位置 避免为控件定义明确的尺寸,但应该限定一个可接受的最大及最小尺寸 不要将界面元素设置成与屏幕坐标相关 容器 ...
- mysql插入数据出现java.lang.NullPointerException
在写购物车持久层的时候,要进行测试的时候居然出现了空指针异常: 最后发现是测试类少了 @RunWith(SpringRunner.class)@SpringBootTest 如下是没改之前的测试类: ...
- [USACO10NOV]奶牛的图片Cow Photographs 树状数组 递推
Code: #include<cstdio> #include<algorithm> #include<string> #include<cstring> ...
- CSS3中的transition
W3C标准中对CSS3的transition是这样描述的: CSS的transition允许CSS的属性值在一定的时间区间内平滑地过渡.这种效果可以在鼠标单击,获得焦点,被点击或对元素任何改变中触发, ...
- Msql免安装版安装
文首提要: 我下载的MySQL版本是:mysql-5.7.17-winx64.zip Archive版:系统:Windows7 64位. 一.解压文件 下载好MySQL后, ...
- eclipse历史版本下载地址
http://wiki.eclipse.org/Older_Versions_Of_Eclipse
- webpack 操作
依赖安装 : 全局安装webpack : sudo npm install webpack -g 本地安装webpack : npm install webpack —save-dev 需要注意的 ...
- 虚拟集群LVS及DR模式搭建笔记
LVS(虚拟集群Linux Virtual Server) LVS-NAT:地址转换,数据包来回都要经过NAT转换,所以Director Server(即LVS服务器)将成为系统瓶颈.使用NAT模式将 ...
- python字典对象的update()方法
使用字典对象的update()方法,如A.update(B),将B字典的键值对一次性全部添加到A字典对象,当A字典为空时,相当于深复制,非常方便.如果两个字典中存在相同的键,则进行值的更新. A={} ...