鱼C论坛

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

[已解决]问题 编译失败

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

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

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

x
在本地编译器可以正常编译,但是提交洛谷后显示编译错误。
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int book[10001][10001],arr[10001][10001],next[3][2]={{0,-1},{1,0},{0,1}};
  4. int maxn = -9999999,n,m;
  5. void dfs(int x,int y,int sum){
  6.         if(x == m && y == n){
  7.                 if(sum > maxn) maxn = sum;
  8.                 return ;
  9.         }
  10.         int tx,ty;
  11.         for(int i = 0;i < 3;i++){
  12.                 tx = x + next[i][0];
  13.                 ty = y + next[i][1];
  14.                 if(tx < 1 || tx > m || ty < 1 || ty > n){
  15.                         continue;
  16.                 }
  17.                 if(book[ty][tx] == 0){
  18.                         book[ty][tx] = 1;
  19.                         dfs(tx,ty,sum+arr[ty][tx]);
  20.                         book[ty][tx] = 0;
  21.                 }
  22.         }
  23. }
  24. int main(){
  25.         cin>>n>>m;
  26.         for(int i = 1;i <=n;i++){
  27.                 for(int j = 1;j <= m;j++){
  28.                         cin>>arr[i][j];
  29.                 }
  30.         }
  31.         dfs(1,1,arr[1][1]);
  32.         cout<<maxn;
  33. }
复制代码

https://www.luogu.com.cn/problem/P7074
最佳答案
2023-8-30 14:00:10
问题的原因是代码中定义了一个名为 next 的数组,但是 next 是C++标准库中的一个函数名,因此会导致编译错误。为了解决这个问题,我们需要将数组名更改为其他名称。

以下是修改后的代码:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int book[10001][10001],arr[10001][10001],nextPos[3][2]={{0,-1},{1,0},{0,1}};
  4. int maxn = -9999999,n,m;
  5. void dfs(int x,int y,int sum){
  6.     if(x == m && y == n){
  7.         if(sum > maxn) maxn = sum;
  8.         return ;
  9.     }
  10.     int tx,ty;
  11.     for(int i = 0;i < 3;i++){
  12.         tx = x + nextPos[ i][0];
  13.         ty = y + nextPos[ i][1];
  14.         if(tx < 1 || tx > m || ty < 1 || ty > n){
  15.             continue;
  16.         }
  17.         if(book[ty][tx] == 0){
  18.             book[ty][tx] = 1;
  19.             dfs(tx,ty,sum+arr[ty][tx]);
  20.             book[ty][tx] = 0;
  21.         }
  22.     }
  23. }
  24. int main(){
  25.     cin>>n>>m;
  26.     for(int i = 1;i <=n;i++){
  27.         for(int j = 1;j <= m;j++){
  28.             cin>>arr[ i][j];
  29.         }
  30.     }
  31.     dfs(1,1,arr[1][1]);
  32.     cout<<maxn;
  33. }
复制代码


通过将 next 数组重命名为 nextPos ,代码将不再与C++标准库中的函数名冲突,从而解决了编译错误的问题。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

以下是修改后的代码:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int book[10001][10001],arr[10001][10001],nextPos[3][2]={{0,-1},{1,0},{0,1}};
  4. int maxn = -9999999,n,m;
  5. void dfs(int x,int y,int sum){
  6.     if(x == m && y == n){
  7.         if(sum > maxn) maxn = sum;
  8.         return ;
  9.     }
  10.     int tx,ty;
  11.     for(int i = 0;i < 3;i++){
  12.         tx = x + nextPos[ i][0];
  13.         ty = y + nextPos[ i][1];
  14.         if(tx < 1 || tx > m || ty < 1 || ty > n){
  15.             continue;
  16.         }
  17.         if(book[ty][tx] == 0){
  18.             book[ty][tx] = 1;
  19.             dfs(tx,ty,sum+arr[ty][tx]);
  20.             book[ty][tx] = 0;
  21.         }
  22.     }
  23. }
  24. int main(){
  25.     cin>>n>>m;
  26.     for(int i = 1;i <=n;i++){
  27.         for(int j = 1;j <= m;j++){
  28.             cin>>arr[ i][j];
  29.         }
  30.     }
  31.     dfs(1,1,arr[1][1]);
  32.     cout<<maxn;
  33. }
复制代码


通过将 next 数组重命名为 nextPos ,代码将不再与C++标准库中的函数名冲突,从而解决了编译错误的问题。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-8-30 14:06:14 | 显示全部楼层
啥错误?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

没错误,你试试放在编译器里,一点错没有;
但是提交后就编译错误,
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

提交后显示什么错误?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

解决了,与c++的函数名重了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-22 07:35

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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