当前位置:网站首页>[Wrong title] Circuit maintenance
[Wrong title] Circuit maintenance
2022-08-03 11:11:00 【wingaso】
WA代码
#include<bits/stdc++.h>
#define xx first
#define yy second
using namespace std;
typedef pair<int,int> pii;
const int N = 510;
string G[N];
int r,c;
int D[N][N];
const int d1[] = {
-1,-1,1, 1}; // 点
const int d2[] = {
-1, 1,1,-1};
const int k1[] = {
-1,-1,0, 0}; // 边
const int k2[] = {
-1, 0,0,-1};
const char kk[] = "\\/\\/";
int bfs(){
deque<pii> q;
memset(D,-1,sizeof D);
q.push_back({
0,0});
D[0][0] = 0;
while(!q.empty()){
int x = q.front().xx,y = q.front().yy;
q.pop_front();
if(x == r and y == c) break;
for(int i = 0;i < 4;i++){
int dx = x + d1[i],dy = y + d2[i];
int kx = x + k1[i],ky = y + k2[i];
if(dx < 0 or dy < 0 or dx > r or dy > c) continue;
if(~D[dx][dy]) continue;
if(G[kx][ky] == kk[i]){
D[dx][dy] = D[x][y];
q.push_front({
dx,dy});
}
else{
D[dx][dy] = D[x][y] + 1;
q.push_back({
dx,dy});
}
}
}
return D[r][c];
}
int main(){
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
int t; cin >> t;
while(t--){
cin >> r >> c;
for(int i = 0;i < r;i++) cin >> G[i];
cout << bfs() << endl;
}
return 0;
}
AC代码
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<deque>
#define xx first
#define yy second
using namespace std;
typedef pair<int,int> pii;
const int N = 510;
string G[N];
int r,c;
int D[N][N];
const int d1[] = {
-1,-1,1, 1}; // 点
const int d2[] = {
-1, 1,1,-1};
const int k1[] = {
-1,-1,0, 0}; // 边
const int k2[] = {
-1, 0,0,-1};
const char kk[] = "\\/\\/";
bool st[N][N];
int bfs(){
if((r + c) % 2) return -1;
deque<pii> q;
memset(D,0x3f,sizeof D);
memset(st,false,sizeof st);
q.push_back({
0,0});
D[0][0] = 0;
st[0][0] = true;
while(!q.empty()){
int x = q.front().xx,y = q.front().yy;
q.pop_front();
st[x][y] = true;
if(x == r and y == c) break;
for(int i = 0;i < 4;i++){
int dx = x + d1[i],dy = y + d2[i];
int kx = x + k1[i],ky = y + k2[i];
if(dx < 0 or dy < 0 or dx > r or dy > c) continue;
if(st[dx][dy]) continue;
if(G[kx][ky] == kk[i] and D[dx][dy] > D[x][y]){
D[dx][dy] = D[x][y];
q.push_front({
dx,dy});
}
else if(G[kx][ky] != kk[i] and D[dx][dy] > D[x][y] + 1){
D[dx][dy] = D[x][y] + 1;
q.push_back({
dx,dy});
}
}
}
return D[r][c];
}
int main(){
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
int t; cin >> t;
while(t--){
cin >> r >> c;
for(int i = 0;i < r;i++) cin >> G[i];
int res = bfs();
if(~res) cout << res << endl;
else cout << "NO SOLUTION" << endl;
}
return 0;
}
错误原因
错误原因是,Mistakenly believe that the distance when a point is updated for the first time must be the shortest distance.
这是一个假命题.
Because one point is taken from the queue at a time ( x , y ) (x,y) (x,y)时,will update its four adjacent points"距离".
当 D [ d x ] [ d y ] = D [ x ] [ y ] + 1 D[dx][dy] =D[x][y] + 1 D[dx][dy]=D[x][y]+1时,Still can not rule out being able to be from another distance D [ t x ] [ t y ] = D [ x ] [ y ] D[tx][ty] = D[x][y] D[tx][ty]=D[x][y]的点 ( t x , t y ) (tx,ty) (tx,ty),以 0 0 0的代价从 ( t x , t y ) (tx,ty) (tx,ty)走到 ( d x , d y ) (dx,dy) (dx,dy).
双端队列广搜,实际可以理解为dijkstra的简化版本.
Every time we update the distance,That is, a relaxation operation.
所以,we can imitate,Set the initial distance to (理论上的)无穷大,Then make the final obtained distance as the minimum distance in the update.
因此,这里不能使用DThe array is used as the basis for judging whether the distance of the node needs to be updated,We need to add a new state array s t st st.
边栏推荐
- MySQL - 2059 - Authentication plugin ‘caching_sha2_password‘ cannot be loaded
- MATLAB programming and application 2.7 Structural data and unit data
- 关于OPENSSL的问题
- 二叉搜索树(搜索二叉树)模拟实现(有递归版本)
- 混动产品谁更吃香,看技术还是看市场?
- 在线生成接口文档
- MATLAB Programming and Applications 2.6 Strings
- [错题]电路维修
- 试题G:单词分析 ← 第十一届蓝桥杯大赛第二场省赛赛题
- Depth study of 100 cases - convolution neural network (CNN) to realize the clothing image classification
猜你喜欢

在线生成接口文档

Advanced use of MySQL database

【JS 逆向百例】某网站加速乐 Cookie 混淆逆向详解

redis基础知识总结——数据类型(字符串,列表,集合,哈希,集合)

深度学习100例——卷积神经网络(CNN)实现服装图像分类

Simple implementation of a high-performance clone of Redis using .NET (1)

2022年五面蚂蚁、三面拼多多、字节跳动最终拿offer入职拼多多

Polymorphism in detail (simple implementation to buy tickets system simulation, covering/weight definition, principle of polymorphism, virtual table)

ETL data cleaning case in MapReduce

Web Server 设置缓存响应字段的一些推荐方案
随机推荐
浅谈SVN备份
【多线程的相关内容】
Machine Learning Overview
for in 和 for of的区别
Cross-chain bridge protocol Nomad suffers hacker attack, losing more than $150 million
苏州大学:从PostgreSQL到TDengine
Matplotlib
LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之二
[LeetCode—Question 2 Sum of Two Numbers Detailed Code Explanation ] The source code is attached, which can be copied directly
【网络原理的概念】
Machines need tokens more than people
Skills required to be a good architect: How to draw a system architecture that everyone will love?What's the secret?Come and open this article to see it!...
build --repot
2022年五面蚂蚁、三面拼多多、字节跳动最终拿offer入职拼多多
在线生成接口文档
[错题]电路维修
实至名归!九章云极DataCanvas公司荣获智能制造领域多项殊荣
numpy
【LeetCode—第2题 两数之和 代码详解 】附有源码,可直接复制
用于发票处理的 DocuWare,摆脱纸张和数据输入的束缚,自动处理所有收到的发票