当前位置:网站首页>[error analysis] grid access
[error analysis] grid access
2022-07-21 14:38:00 【wingaso】
On the first code .
Error code
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
const int N = 12;
int n;
int G[N][N],f[N][N],D[N][N];
int main(){
cin >> n;
while(true){
int x,y,v;scanf("%d%d%d",&x,&y,&v);
if(!(x or y or v)) break;
G[x][y] = v;
}
for(int i = 0;i <= n;i++) f[i][0] = f[0][i] = -1;
f[1][0] = 0;
for(int i = 1;i <= n;i++)
for(int j = 1;j <= n;j++){
if(f[i][j-1] > f[i-1][j]) D[i][j] = 1;
f[i][j] = max(f[i][j-1],f[i-1][j]) + G[i][j];
}
int x = f[n][n];
int i = n,j = n;
while(i != 0 and j != 0){
G[i][j] = 0;
if(D[i][j]) j --;
else i--;
}
memset(f,0,sizeof f);
for(int i = 1;i <= n;i++)
for(int j = 1;j <= n;j++){
f[i][j] = max(f[i-1][j],f[i][j-1]) + G[i][j];
}
printf("%d",f[n][n] + x);
return 0;
}
Correct code
#include<cstdio>
#include<algorithm>
using namespace std;
const int N = 12;
int n;
int G[N][N];
int f[2*N][N][N];
int main(){
scanf("%d",&n);
int x,y,v;
while(~scanf("%d%d%d",&x,&y,&v) and (x or y or v)) G[x][y] = v;
for(int k = 2;k <= 2 * n;k++){
for(int i1 = 1;i1 <= n;i1++){
for(int i2 = 1;i2 <= n;i2++){
// (i1,k-i1) (i2,k-i2)
int j1 = k - i1,j2 = k - i2;
if(j1 >= 1 and j1 <= n and j2 >= 1 and j2 <= n){
int &x = f[k][i1][i2];
int t = G[i1][j1];
if(i1 != i2) t += G[i2][j2];
x = max(x,f[k-1][i1][i2] + t);
x = max(x,f[k-1][i1-1][i2] + t);
x = max(x,f[k-1][i1][i2-1] + t);
x = max(x,f[k-1][i1-1][i2-1] + t);
}
}
}
}
printf("%d",f[2*n][n][n]);
return 0;
}
Error code is one of our easiest ideas , That is, one person goes first , After a walk , Then update the number of the square chart . Then walk again , Add up the two maximum values obtained respectively , As the end result .
Error code exists wa The situation of .
What do you ask ?
Problem analysis
We made a sequence for walking , The first person who walks gets the most solution —— We set it as x 1 x_1 x1, The optimal solution obtained by the second person is x 2 x_2 x2. It's not hard to see. , For the global optimal solution x x x, We use greedy thinking directly , Assumed x = x 1 + x 2 x=x_1+x_2 x=x1+x2.
Here comes a problem , x 1 + x 2 x_1+x_2 x1+x2 Whether it must be equal to x x x?
The answer is No .
As shown in the figure below :
All numbers can be obtained through the two paths in Figure 1 .
And if the greedy method is used , That is, choose a path that can get the current local optimal solution for the first time , Then no matter how to take it the second time , You can't select all the remaining two numbers .
therefore , without doubt , For this question , The local optimal solution cannot deduce the global optimal solution .
therefore , We have to consider “ parallel ” Dynamic planning method .
Originality is not easy. , Thank you for your support .
边栏推荐
- 华为云:一切皆服务,共建全场景智慧金融
- Simple opengauss SQL tuning using plan hint
- Supermarket order management system based on SSM framework +mysql [source code + document +ppt]
- solana上使用Rust进行合约开发
- Win64 驱动内核编程-32.枚举与删除注册表回调
- How to solve the problem that win11 printer document is suspended?
- Install MySQL and MySQL workbench on MAC 10.13.6 and other versions
- 微软推出社交应用 Viva Engage,界面神似 Facebook
- Path module of node
- 全栈开发实战 | SSM框架整合完整教程
猜你喜欢
Sword finger offer 58 - I. flip word order, strip() function
2022年7月俄罗斯数据库排行榜:ClickHouse雄踞榜首,GigaBASE摘得榜眼
MiniProg3进行Hex烧录
云原生如何支撑企业 IT 治理 | 阿里云用户组
Recruit | "one brain cloud research circle" recruit new members
solana上使用Rust进行合约开发
阿里云原生应用平台架构师田伟:应用架构的规划、治理与演进
全栈开发实战 | SSM框架整合完整教程
剑指 Offer 57. 和为s的两个数字
Sword finger offer 13 Range of motion of robot
随机推荐
乌卡时代下,企业供应链如何敏捷应对企业采购支出管理?
在node.js项目中安装配置mysql模块并进行增删改查
Kwai's overseas product snackvideo cooperates exclusively with pubg mobile to help mobile travel break the circle overseas
乘数科技云管控平台适配阿里云PolarDB,共促云原生数据库生态繁荣
mac 10.13.6 及其他版本安装MySQL和MySQL Workbench
(PC+WAP)织梦模板空气净化类网站
What if win11 C disk turns red? Cleaning method of win11 C disk turning red
yarn capacity调度器小结
代码实现层序遍历二叉树(C语言)
UMB10F-ASEMI贴片整流桥UMB10F
读书会丨如何才能不做情绪的人质?
Reading notes (I) -- Kite Runner
Umb10f-asemi patch rectifier bridge umb10f
awk 统计差值记录
QT OpenGL ambient lighting
数据库自增ID用完了会怎样?
华为云:一切皆服务,共建全场景智慧金融
【LeetCode】二分查找题解汇总
ASEMI整流桥MB10M参数,MB10M大小,MB10M特性
Win11 cannot find gpedit What about MSc? Win11 cannot open gpedit MSc solution tutorial