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

Codeforces Round 301 (Div. 2) C题 Ice Cave(BFS)

题目链接

https://codeforces.com/problemset/problem/540/C

思路

直接暴力 b f s bfs bfs即可。

从起点开始,向四个方向进行扩展,每到达一个节点就修改一下该节点的状态(如果该节点是完整的冰块),如果能走到终点就是YES,否则就是NO。

代码

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 5e2 + 5;
int n, m;
int r[2], c[2];
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
char s[N][N];
void bfs()
{
	queue<pair<int, int>>q;
	q.push({r[0], c[0]});
	while (q.size())
	{
		int x = q.front().first;
		int y = q.front().second;
		q.pop();
		for (int i = 0; i < 4; i++)
		{
			int tx = x + dx[i];
			int ty = y + dy[i];
			if (tx == r[1] && ty == c[1] && s[tx][ty] == 'X')
			{
				cout << "YES" << endl;
				return;
			}
			if (s[tx][ty] == '.')
			{
				s[tx][ty] = 'X';
				q.push({tx, ty});
			}
		}
	}
	cout << "NO" << endl;
}
void solve()
{
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= m; j++)
		{
			cin >> s[i][j];
		}
	}
	for (int i = 0; i < 2; i++)
	{
		cin >> r[i] >> c[i];
	}
	bfs();
}
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	int test = 1;
	// cin >> test;
	for (int i = 1; i <= test; i++)
	{
		solve();
	}
	return 0;
}

http://www.kler.cn/news/325405.html

相关文章:

  • 昇思MindSpore进阶教程--高级自动微分
  • 基于springboot+小程序的儿童预防接种预约管理系统(疫苗1)(源码+sql脚本+视频导入教程+文档)
  • 依赖倒置原则(学习笔记)
  • PostgreSQL的表碎片
  • 学习Java (五)
  • Go Sonyflake学习与使用
  • 新能源汽车充电桩怎么选?
  • Linux基础(二):磁盘分区
  • js替换css主题变量并切换iconfont文件
  • uniapp中h5环境添加console.log输出
  • 2024年7月大众点评沈阳美食店铺基础信息
  • 数据结构和算法之树形结构(4)
  • springframework Ordered接口学习
  • BOE(京东方)携故宫博物院举办2024“照亮成长路”公益项目落地仪式以创新科技赋能教育可持续发展
  • 计算机网络--TCP、UDP抓包分析实验
  • 2024年配置YOLOX运行环境+windows+pycharm24.0.1+GPU
  • [C语言]--自定义类型: 结构体
  • 【C/C++】错题记录(一)
  • pdf页面尺寸裁减
  • uni-app+vue3开发微信小程序使用本地图片渲染不出来报错[渲染层网络层错误]Failed to load local image resource
  • 黑马智数Day2
  • Python pyusb 使用指南【windows+linux】
  • 基于单片机的无线宠物自动喂食系统设计
  • 大数据复习知识点3
  • Python线程终止:如何优雅地结束一场“舞蹈”
  • Mybatis缓存机制(图文并茂!)
  • YOLOv8改进 | 融合篇,YOLOv8主干网络替换为MobileNetV4+CA注意机制+Powerful-IoU损失函数(全网独家首发,实现极限涨点)
  • 力扣刷题之1014.最佳观光组合
  • RK3588主板PCB设计学习(五)
  • CRC循环校验的功能