当前位置: 首页 > news >正文

荷香堪筑梦,鸳鸯和月寻。(变相BFS搜索)

本题链接:登录—专业IT笔试面试备考平台_牛客网

题目:

样例:

输入
3 4 2
....
***.
..a.
输出
yes

思路:

        根据题意,这里 1 s 可以移动多次,我们将每次可以移动避开雪的的位置存储起来,判断当移动到了顶部,说明完全可以避开雪。

代码详解如下:

#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
#include <algorithm>
#include <unordered_map>
#define endl '\n'
#define x first
#define y second
#define int long long
#define YES puts("YES")
#define NO puts("NO")
#define umap unordered_map
#define All(x) x.begin(),x.end()
#pragma GCC optimize(3,"Ofast","inline")
#define IOS std::ios::sync_with_stdio(false),cin.tie(0), cout.tie(0)
using namespace std;
const int N = 2e6 + 10;
inline void solve();signed main()
{
//	freopen("a.txt", "r", stdin);IOS;int _t = 1;// cin >> _t;while (_t--){solve();}return 0;
}
int n,m,k;
char g[1010][1010];		// 下雪图
bool st[1010][1010];	// 标记是否走过
using PII = pair<int,int>;
inline void BFS(int x,int y)
{queue<PII>q;q.emplace(x,y);st[x][y] = true;while(q.size()){PII now = q.front();q.pop();// 如果到达了最顶部,说明完全避开了所以雪if(now.x == 1){cout << "yes" << endl;return ;}st[now.x][now.y] = true;// 当前位置 的 上右边可移动最长距离int rlen = min(now.y + k,m);for(int i = now.y;i <= rlen;++i){// 如果这个位置没走过,我们存储起来可以走到这个的位置if(g[now.x - 1][i] == '.' and !st[now.x - 1][i]){// 标记这里走过了st[now.x - 1][i] = true;q.emplace(PII(now.x - 1,i));}}// 当前位置 的 上右边可移动最长距离int llen = min(now.y - k,1LL);for(int i = now.y - 1;i >= llen;--i){// 如果这个位置没走过,我们存储起来可以走到这个的位置if(g[now.x - 1][i] == '.' and !st[now.x - 1][i]){// 标记这里走过了st[now.x - 1][i] = true;q.emplace(PII(now.x - 1,i));}}}cout << "no" << endl;	// 如果一直走不到 顶部,输出no
}
inline void solve()
{int x,y;cin >> n >> m >> k;for(int i = 1;i <= n;++i){for(int j = 1;j <= m;++j){cin >> g[i][j];if(g[i][j] == 'a'){x = i;y = j;}}}BFS(x,y);
}

最后提交:

http://www.lryc.cn/news/342735.html

相关文章:

  • 智慧公厕建设,打造智慧城市基础设施新亮点
  • Kubernetes 教程:在 Containerd 容器中使用 GPU
  • LNMP部署wordpress
  • 本地部署大模型ollama+docker+open WebUI/Lobe Chat
  • qt学习篇---界面按键关联(信号和槽)
  • python Django 的内置权限系统或自定义模型来存储更复杂的角色和权限关系
  • 不上班,我靠这5份赚钱副业养活了自己
  • 强一致性的皇冠:分布式事务模型的至高法则揭秘
  • mac/windows下安装docker,minikube
  • 【爬虫】fake_useragent的使用、BeautifulSoup(find()和find_all())
  • ComfyUI中图像亮度/对比度/饱和度处理
  • 基于FPGA的DDS波形发生器VHDL代码Quartus仿真
  • C++语法|可调用对象与function类型
  • Linux学习之路 -- 文件 -- 文件描述符
  • JDK动态代理和Cglib动态代理区别
  • 牛客 | 字符金字塔
  • 【计算机科学速成课】笔记三——操作系统
  • 用js代码实现贪吃蛇小游戏
  • 微信小程序+esp8266温湿度读取
  • 软考中级-软件设计师(十)网络与信息安全基础知识
  • 推荐一个好用的命令行工具ShellGPT
  • Prompt提示词教程 | 提示工程指南 | 提示词示例 入门篇
  • uniapp + uView动态表单校验
  • 【Linux】HTTPS
  • 语音识别--使用YAMNet识别环境音
  • 前端JS必用工具【js-tool-big-box】,邮箱,手机,身份证号,ip地址等正则验证方法学习
  • notepad++安装 hex-editor插件
  • Ubuntu18.04设置SSH密钥登录
  • 自动化运维管理工具----------Ansible模块详细解读
  • 零基础代码随想录【Day27】|| 39. 组合总和,40.组合总和II, 131.分割回文串