Safe Path(bfs+一维数组存图)
题目链接:http://codeforces.com/gym/101755/problem/H
题目分析:先bfs一遍怪兽可以到达的点,再bfs人可以走的地方看可不可以到达终点;
很显然读到 2<=n*m<=200000 时,就不可以用二维数组存图了,不过据说因为数据比较水,可以用vector存图;
vector存图AC代码:
/* */
# include <iostream>
# include <stdio.h>
# include <string.h>
# include <string>
# include <cmath>
# include <climits>
# include <cctype>
# include <ctime>
# include <algorithm>
# include <functional>
# include <bitset>
# include <set>
# include <map>
# include <deque>
# include <queue>
# include <stack>
# include <vector>
using namespace std; const int maxn=2e5+;
vector<char>mp[maxn];
vector<int>dis[maxn];
vector<int>vis[maxn];
int n, m; struct node
{
int x, y;
int step;
}cur, after; int dir[][]={{, }, {, -}, {, }, {-, }};
int sx, sy, fx, fy; int check(int x, int y)
{
if( x>= && x<n && y>= && y<m )
return ;
return ;
} void bfs1(int d)
{
queue<node>q;
int i, j;
for( i=; i<n; i++ )
{
for( j=; j<m; j++ )
{
if( mp[i][j]=='M' )
{
cur.x = i;
cur.y = j;
cur.step = ;
q.push(cur);
dis[i][j] = ;
}
}
} while( !q.empty() )
{
cur = q.front();
q.pop();
for(int i=; i<; i++ )
{
after.x = cur.x+dir[i][];
after.y = cur.y+dir[i][];
after.step = cur.step + ; if( check(after.x, after.y) )
{
if( !dis[after.x][after.y] && after.step<=d )
{
dis[after.x][after.y] = ;
q.push(after);
}
}
}
}
} void bfs2(int x, int y)
{
cur.x = x;
cur.y = y;
cur.step = ;
queue<node>q;
q.push(cur);
vis[x][y] = ;
if( dis[x][y] )
{
printf("-1\n");
return ;
}
while( !q.empty() )
{
cur = q.front();
q.pop();
if( mp[cur.x][cur.y]=='F' )
{
printf("%d\n", cur.step);
return ;
}
for(int i=; i<; i++ )
{
after.x = cur.x + dir[i][];
after.y = cur.y + dir[i][];
after.step = cur.step + ;
if( check(after.x, after.y) )
{
if( !dis[after.x][after.y] && !vis[after.x][after.y])
{
vis[after.x][after.y] = ;
q.push(after);
}
}
}
}
printf("-1\n");
return ;
}
int main()
{
int d, i, j;
cin>>n>>m>>d;
char s; for(i=; i<n; i++)
{
mp[i].clear();
vis[i].clear();
dis[i].clear();
} for(i=; i<n; i++ )
{
for(j=; j<m; j++ )
{
cin>>s;
mp[i].push_back(s);
dis[i].push_back();
vis[i].push_back();
}
}
bfs1(d); for(i=; i<n; i++ )
{
for( j=; j<m; j++ )
{
if( mp[i][j]=='F' )
{
fx = i;
fy = j;
}
if( mp[i][j]=='S' )
{
sx = i;
sy = j;
}
}
} if( dis[fx][fy] )
{
printf("-1\n");
}
else
{
bfs2(sx, sy);
}
return ;
}
这道题也让我知道了可以用一位数组存图:
详细的见代码注释:
AC代码:
/* */
# include <iostream>
# include <stdio.h>
# include <string.h>
# include <algorithm>
# include <cctype>
# include <ctime>
# include <functional>
# include <cmath>
# include <bitset>
# include <deque>
# include <queue>
# include <stack>
# include <vector>
# include <set>
# include <map>
# include <climits>
using namespace std; typedef long long LL;
const int maxn=1e6+;
int n, m, t;
int a[maxn], d;
char str[maxn];
bool vis[maxn];///标记是否已经访问过
int dis[maxn];///记录步数
int dd[maxn];///dd[]为0,说明怪兽到不了,dd不为0说明怪兽可以到此处
int cnt, fx, fy, sx, sy;
int dir[][]={{, }, {, -}, {-, }, {, }};
struct node
{
int x;
int y;
}g[maxn], cur, after; bool check(int a, int b)
{
if( a>= && b>= && a<=n && b<=m && !dd[a*m+b] )
return true;
return false;
} void bfs()
{
queue<node>q;
cur.x = sx;
cur.y = sy;
vis[cur.x*m+cur.y] = ;
q.push(cur); while( !q.empty())
{
cur = q.front();
q.pop();
for(int i=; i<; i++ )
{
int xx = cur.x + dir[i][];
int yy = cur.y + dir[i][]; if( check(xx, yy) && !vis[xx*m+yy])
{
dis[xx*m+yy] = dis[cur.x*m+cur.y]+;
vis[xx*m+yy] = ;
after.x = xx;
after.y = yy;
q.push(after);
}
}
}
return ;
} void bfss()///广搜怪兽可以到达的地方
{
queue<node>q;
for(int i=; i<cnt; i++ )
q.push(g[i]); while( !q.empty() )
{
cur=q.front();
q.pop(); if( dd[cur.x*m+cur.y]== )
continue; for(int i=; i<; i++ )
{
int xx = cur.x + dir[i][];
int yy = cur.y + dir[i][];
if( check(xx, yy) )
{
after.x = xx;
after.y = yy;
dd[xx*m+yy] = dd[cur.x*m+cur.y]-;
q.push(after);
}
}
}
return ;
} int main()
{
while( ~ scanf("%d %d %d", &n, &m, &d) )
{
getchar();
for(int i=; i<=n; i++ )
scanf("%s", &str[i*m+]);///一维数组存图 memset(vis, false, sizeof(vis));
memset(dd, , sizeof(dd));
memset(dis, , sizeof(dis)); for(int i=; i<=n; i++ )
{
for(int j=; j<=m; j++ )
{
if( str[i*m+j]=='S' )
{
sx = i;
sy = j;
} else if( str[i*m+j]=='F' )
{
fx = i;
fy = j;
} else if( str[i*m+j]=='M' )
{
dd[i*m+j] = d+;
cur.x = i;
cur.y = j;
g[cnt++] = cur;
}
}
} bfss();
if( dd[sx*m+sy] || dd[fx*m+fy] )
{
printf("-1\n");
}
else
{
bfs();
if( !vis[fx*m+fy] )
printf("-1\n");
else
printf("%d\n", dis[fx*m+fy]);
}
}
return ;
}
Safe Path(bfs+一维数组存图)的更多相关文章
- Codeforces gym101755H Safe Path(bfs)
题意: 给以一个网格图,有起点终点和一些怪兽,可以上下左右走,不能走到距离怪兽曼哈顿距离为d以内的地方,问到终点最短路径 n*m<=2e5,d<=2e5 思路: 因为n*m的范围,不能直接 ...
- POJ 1985.Cow Marathon-树的直径-树的直径模板(BFS、DFS(vector存图)、DFS(前向星存图))
Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 7536 Accepted: 3559 Case ...
- QDUOJ 生化危机 邻接表存图+BFS
生化危机 发布时间: 2015年10月10日 18:05 时间限制: 1000ms 内存限制: 256M 描述 X博士想造福人类, 研发一种可以再生肢体的药物, 可是很不幸......研究失败 ...
- 学JAVA第十天,一维数组及二维数组的使用。
今天老师讲了JAVA数组,之前学C#的时候就学过一维数组,至于二维数组当时只是粗略普及了一下. 现在想学JAVA又学到了数组,但是这次不同,注重讲二维数组,因为老师知道我们都了解一维数组了. 所以现在 ...
- java基础5 (一维)数组和二维数组
本文知识点(目录): 一维数组(一维数组的概念.优点.格式.定义.初始化.遍历.常见异常.内存分析以及常见操作(找最大值.选择排序.冒泡排序等等)) 二维数组(二维数组的遍历.排序.查找.定义. ...
- B - Cow Marathon DFS+vector存图
After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to get more exerc ...
- Treasure Island DFS +存图
All of us love treasures, right? That's why young Vasya is heading for a Treasure Island. Treasure I ...
- js cookie 数组 存读
自己研究了一下. "Cookie里面只能放String 类型" 所以只能将arr的数据按照自己的约定转成string格式存进cookie. 这里提示一下cookie是存在本地浏览器 ...
- C# 数组、一维数组、二维数组、多维数组、锯齿数组
C# 数组.一维数组.二维数组.多维数组.锯齿数组 一.数组: 如果需要使用同一类型的对象,就可以使用数组,数组是一种数据结构,它可以包含同一类型的多个元素.它的长度是固定的,如长度未知的情况下,请 ...
随机推荐
- php后台实现页面跳转的方法-转载
地址:http://blog.csdn.net/abandonship/article/details/6459104 其中方法三的js代码在tp框架使用存在故障,一个是需要把代码写在一起(可能也不需 ...
- Selenium文件上传
转自:https://www.cnblogs.com/miaojjblog/p/9679915.html Web上本地上传图片,弹出的框Selenium是无法识别的,也就是说,selenium本身没有 ...
- org.apache.commons.beanutils.ConversionException: No value specified解决办法
转自:https://www.cnblogs.com/linjiqin/archive/2011/07/21/2112628.html 当用到了java.sql.Date时间等非内置对象时,如果对象为 ...
- antd-table——内容展示变型
bug单: https://github.com/ant-design/ant-design/issues/13825 1.设置固定宽度:在columns中设置widht或者className { t ...
- idea中添加多级父子模块
在 IntelliJ IDEA 中,没有类似于 Eclipse 工作空间(Workspace)的概念,而是提出了Project和Module这两个概念. 在 IntelliJ IDEA 中Projec ...
- Python_变量作用域
1.变量作用域: def get_apple(name,*b): global totalCount totalCount=0 for num in b: print('............... ...
- 资源管理与调度系统-资源管理系统Mesos
资源管理与调度系统-资源管理系统Mesos 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. Mesos是诞生于UC Berkeley的一个研究项目,它的设计动机是解决编程模型和计算框 ...
- 数据库系统load飙高问题解决思路
工作过程中有时候会接收到数据库服务器器load 飙高的报警,比如: load1 15.25 base: 8.52,collect time:2014-08-30 如何处理load 异常飙高的报警呢? ...
- Linux 中 /proc/kcore为啥如此之大
What Is /proc/kcore?None of the files in /proc are really there--they're all, "pretend," f ...
- 2019-08-28 redhat linux如何部署禅道服务器(一键安装包)
linux一键安装包内置了XXD.apache, php, mysql这些应用程序,不需要再单独安装部署. linux一键安装包分为32位和64位两个包,请大家根据操作系统的情况下载相应的包. 一.准 ...