|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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
问题的原因是代码中定义了一个名为 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++标准库中的函数名冲突,从而解决了编译错误的问题。
|
|