
血色先锋队
发布日期:2021-05-07 07:23:50
浏览次数:11
分类:原创文章
本文共 824 字,大约阅读时间需要 2 分钟。
洛谷链接:
多次入队,但是入队的时候建议在bfs中入队
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespace std;typedef pair<int,int>PII;int n,m,a,b;int dist[1010][1010];int st[1010][1010];int go[4][2] = { {1,0},{0,1},{-1,0},{0,-1}};void bfs(){ queue<PII>q; for(int i=1;i<=a;i++) { int x,y; cin>>x>>y; q.push({x,y}); st[x][y] = 1; dist[x][y] = 0; } while(q.size()) { PII t = q.front(); q.pop(); for(int i=0;i<4;i++) { int tx = t.first+go[i][0]; int ty = t.second+go[i][1]; if(tx<1||ty<1||tx>n||ty>m||st[tx][ty]) continue; st[tx][ty] = 1; dist[tx][ty] = min(dist[tx][ty],dist[t.first][t.second]+1); q.push({tx,ty}); } }}int main(){ cin>>n>>m>>a>>b; memset(dist,0x3f3f3f3f,sizeof dist); bfs(); for(int i=1;i<=b;i++) { int x,y; cin>>x>>y; cout<<dist[x][y]<<endl; } return 0;}
发表评论
最新留言
逛到本站,mark一下
[***.202.152.39]2025年04月07日 04时44分25秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
SpringIoC和DI注解开发
2019-03-04
Java类和对象
2019-03-04
Java集合Collection
2019-03-04
SpringMVC入门-概述和基本配置
2019-03-04
SpringBoot快速入门
2019-03-04
医疗管理系统-手机快速登录和SpringSecurity权限控制
2019-03-04
网页实现微信登录
2019-03-04
vue源码分析(MVVM篇)
2019-03-04
React(八)- ReactUI组件库及Redux的使用
2019-03-04
TypeScript系列(一)- TypeScript简介与编译配置
2019-03-04
TypeScript系列文章导航
2019-03-04
Windows系统Git安装教程
2019-03-04
hibernate和mybatis的区别
2019-03-04
MyBatis学习总结(三)——优化MyBatis配置文件中的配置
2019-03-04
JavaWeb学习总结(十三)——使用Session防止表单重复提交
2019-03-04
Java中Map的用法详解
2019-03-04
Java注解全面总结
2019-03-04
base64编码字符串和图片的互转
2019-03-04
汉字转为拼音
2019-03-04