鱼C论坛

 找回密码
 立即注册
查看: 1088|回复: 5

[已解决]问题 编译失败

[复制链接]
发表于 2023-8-30 13:57:45 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
在本地编译器可以正常编译,但是提交洛谷后显示编译错误。
#include<bits/stdc++.h>
using namespace std;
int book[10001][10001],arr[10001][10001],next[3][2]={{0,-1},{1,0},{0,1}};
int maxn = -9999999,n,m;
void dfs(int x,int y,int sum){
        if(x == m && y == n){
                if(sum > maxn) maxn = sum;
                return ;
        }
        int tx,ty;
        for(int i = 0;i < 3;i++){
                tx = x + next[i][0];
                ty = y + next[i][1];
                if(tx < 1 || tx > m || ty < 1 || ty > n){
                        continue;
                }
                if(book[ty][tx] == 0){
                        book[ty][tx] = 1;
                        dfs(tx,ty,sum+arr[ty][tx]);
                        book[ty][tx] = 0;
                }
        }
}
int main(){
        cin>>n>>m;
        for(int i = 1;i <=n;i++){
                for(int j = 1;j <= m;j++){
                        cin>>arr[i][j];
                }
        }
        dfs(1,1,arr[1][1]);
        cout<<maxn;
}
https://www.luogu.com.cn/problem/P7074
最佳答案
2023-8-30 14:00:10
问题的原因是代码中定义了一个名为 next 的数组,但是 next 是C++标准库中的一个函数名,因此会导致编译错误。为了解决这个问题,我们需要将数组名更改为其他名称。

以下是修改后的代码:
#include<bits/stdc++.h>
using namespace std;
int book[10001][10001],arr[10001][10001],nextPos[3][2]={{0,-1},{1,0},{0,1}};
int maxn = -9999999,n,m;
void dfs(int x,int y,int sum){
    if(x == m && y == n){
        if(sum > maxn) maxn = sum;
        return ;
    }
    int tx,ty;
    for(int i = 0;i < 3;i++){
        tx = x + nextPos[ i][0];
        ty = y + nextPos[ i][1];
        if(tx < 1 || tx > m || ty < 1 || ty > n){
            continue;
        }
        if(book[ty][tx] == 0){
            book[ty][tx] = 1;
            dfs(tx,ty,sum+arr[ty][tx]);
            book[ty][tx] = 0;
        }
    }
}
int main(){
    cin>>n>>m;
    for(int i = 1;i <=n;i++){
        for(int j = 1;j <= m;j++){
            cin>>arr[ i][j];
        }
    }
    dfs(1,1,arr[1][1]);
    cout<<maxn;
}

通过将 next 数组重命名为 nextPos ,代码将不再与C++标准库中的函数名冲突,从而解决了编译错误的问题。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-8-30 14:00:10 | 显示全部楼层    本楼为最佳答案   
问题的原因是代码中定义了一个名为 next 的数组,但是 next 是C++标准库中的一个函数名,因此会导致编译错误。为了解决这个问题,我们需要将数组名更改为其他名称。

以下是修改后的代码:
#include<bits/stdc++.h>
using namespace std;
int book[10001][10001],arr[10001][10001],nextPos[3][2]={{0,-1},{1,0},{0,1}};
int maxn = -9999999,n,m;
void dfs(int x,int y,int sum){
    if(x == m && y == n){
        if(sum > maxn) maxn = sum;
        return ;
    }
    int tx,ty;
    for(int i = 0;i < 3;i++){
        tx = x + nextPos[ i][0];
        ty = y + nextPos[ i][1];
        if(tx < 1 || tx > m || ty < 1 || ty > n){
            continue;
        }
        if(book[ty][tx] == 0){
            book[ty][tx] = 1;
            dfs(tx,ty,sum+arr[ty][tx]);
            book[ty][tx] = 0;
        }
    }
}
int main(){
    cin>>n>>m;
    for(int i = 1;i <=n;i++){
        for(int j = 1;j <= m;j++){
            cin>>arr[ i][j];
        }
    }
    dfs(1,1,arr[1][1]);
    cout<<maxn;
}

通过将 next 数组重命名为 nextPos ,代码将不再与C++标准库中的函数名冲突,从而解决了编译错误的问题。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-8-30 14:06:14 | 显示全部楼层
啥错误?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-8-30 15:21:24 | 显示全部楼层

没错误,你试试放在编译器里,一点错没有;
但是提交后就编译错误,
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-8-30 17:53:50 | 显示全部楼层
香蕉那个不拿拿 发表于 2023-8-30 15:21
没错误,你试试放在编译器里,一点错没有;
但是提交后就编译错误,

提交后显示什么错误?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-8-30 18:52:21 | 显示全部楼层
陈尚涵 发表于 2023-8-30 17:53
提交后显示什么错误?

解决了,与c++的函数名重了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-6-17 07:27

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表