当前位置:网站首页>Acwing 175电路维修
Acwing 175电路维修
2022-07-21 05:04:00 【这一wa是晚安】
题意:
本题问我们就是从电源处到达终点,我们需要转动每个格子中线路最少次数,如果不管怎么转动都到达不了就输出 "NO SOLUTION"。
首先这个题要区分普通的bfs
首先本题跑的不是格子,是节点,要从最左上节点跑到最右下节点。
还要根据每个格子的图形形状判断走的步数:
如果当前节点可以直接划到下一节点就可以我们认为步长为0它压入队头(电流可以直接流入);
如果需要旋转才能到达下一节点(接通电路),那么该步长为1,压入队尾。
因为每个格子的图形是 "/,\";所以每个节点不能到达该节点的上下左右的临节点,而是斜方向
比如目前我们在0号点这个位置,那我们到三号点所需要的步长是0,到2号点的步长是1,我们需要将这个电线顺时针转动90°,注意我们这儿说的步长是需不需要转动最短路不需要转动步长为0,需要则为1。
首先先考虑不可能的情况,因为我们只能沿着斜线走,只有两种方式"/ \", 所以我们永远到达不了横纵坐标之和为奇数的点,所以只要我们终点的横纵坐标之和为奇数就是
不可能的
![]()
因为蓝色线使我们的起点不能移动我们这样才能和电源连接,然后圈出的点,不管你怎么旋转红色的线,前提是你要连通电源这根线,那么一定到达不了这些点
而后因为我们实现的是最小次数,这些步长为0的点应该放在队头,让我们优先来判断,其次再判断步长为一的点
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define x first
#define y second
using namespace std;
typedef pair<int, int> PII;
typedef long long ll;
const int N = 1e3 + 10;
const int dx[] = {0, -1, 0, 1}, dy[] = {-1, 0, 1, 0};//遍历当前节点能到达的哪些节点的
const int ix[] = {-1, -1, 0, 0}, iy[] = {-1, 0, 0, -1};//当前节点四周格子内的图形是什么
int n, m, t;
int g[N][N];//存图
int dist[N][N];
bool st[N][N];//判重,你如果确定了这个线的放置,就不再去旋转改变它的走向
int bfs()
{
deque<PII> q;
memset(dist, 0x3f, sizeof dist);
memset(st, 0, sizeof st);
char cs[] = "\\/\\/";//四种图形
q.push_back({0,0});
while(!q.empty())
{
auto t = q.front();
q.pop_front();
if(st[t.x][t.y]) continue;
st[t.x][t.y] = true;
for(int i = 0; i < 4; i ++)
{
int ss = t.x + dx[i];
int dd = t.y + dy[i];
if(ss < 0 || ss > n || dd < 0 || dd > m) continue;//越界
int ga = t.x + ix[i], gb = t.y + iy[i];//该节点四周图形的坐标
int d = dist[t.x][t.y] + (g[ga][gb] != cs[i]);//当前遍历到的节点如果与上一节点不同那么距离加1
if(d < dist[ss][dd])
{
dist[ss][dd] = d;
if(g[ga][gb] != cs[i]) q.push_back({ss, dd});//如果图案不同,步长为1压入队尾
else q.push_front({ss, dd});//图案相同,步长为0压入队首
}
}
}
return dist[n][m];
}
void solve()
{
cin >> n >> m;
for(int i = 0; i < n; i ++)
for(int j = 0; j < m; j ++)
cin >> g[i][j];
if(n*m & 1) puts("NO SOLUTION");
else cout << bfs() << endl;
}
signed main()
{
IOS;
cin >> t;
while(t--)
solve();
return 0;
}
边栏推荐
- OLED(经典0.96英寸)--4SPI--SSD1306控制原理(含常用芯片_oled例程)
- 使用Arduino搭建基于阿里云平台的物联网智能家居
- Nacos-配置中心原理解析
- STM32F407-OV7670(无FIFO)-ONENET-实现摄像头画面上传到onenet(EDP协议)
- ES聚合统计语法
- pycharm常见错误集锦
- Exclusive locking of this profile failed. Another running VMware process may be using a profile.
- Overview of Bert principle
- Recursive and non recursive implementation of four traversals of binary trees
- bert-serving
猜你喜欢
5. Paddlepaddle 10 lines of code deep learning image classification (cifar)
mask rcnn 加载权重报错
【PCB】基于合泰HT32F52352芯片电路板绘制实验(WiFi及光传感模块)-画板笔记
Starfish OS: create a new paradigm of the meta universe with reality as the link
pytorch的安装
Exclusive locking of this profile failed. Another running VMware process may be using a profile.
Amy-Tabb机器人世界手眼标定(2、实验结果)
分布式事务其中的那些坑
初学谷歌bert模型的预训练和fine-tuning微调
Initializing libiomp5. dylib, but found libomp. dylib already initialized
随机推荐
DataLossError : corrupted record at XXXXXXX,BERT预训练报错
UNet复现及环境配置(含数据集)
ftp不能创建多级目录【循环创建问题】
OLED(经典0.96英寸)--4SPI--SSD1306控制原理(含常用芯片_oled例程)
2.从零开始学习paddlepaddle之向量计算
Amy-Tabb机器人世界手眼标定(1、环境搭配)
Cat and dog picture resources
Resthighlevelclient syntax
[PCB] modify the default settings for items such as (via) (text string) (wiring linear dimension) in Altium designer
pycharm 笔记
吴恩达深度学习L4W3目标检测
Model definition of pytorch
Comparison of three methods of converting string: (string), toString (), string valueOf()
[PCB] Based on Hetai ht32f52352 chip circuit board drawing experiment (WiFi and optical sensor module) - drawing board notes
[PCB] Based on stm32f103rct6 joystick - Bluetooth module development board - drawing board notes sorting
Datalosserror: corrected record at XXXXXXX, Bert pre training error
Apisik microservice gateway
Recursive and non recursive implementation of four traversals of binary trees
Idea2020 open run dashboard
AS7341光谱传感器测量色温color_temperature_学习笔记