蒟蒻求助
查看原帖
蒟蒻求助
334041
沉鸣cmh楼主2020/8/27 20:55

超时我能理解,但是为啥过不了样例?

#include<bits/stdc++.h>
using namespace std;
int m,n,a[55][55],sum;
bool pd[55][55];
void dfss(int x,int y,int tot){
	if(x==1&&y==1){sum=max(sum,tot);return;}
	if(x>1){pd[x-1][y]=true;dfss(x-1,y,tot+a[x-1][y]);pd[x-1][y]=false;}
	if(y>1){pd[x][y-1]=true;dfss(x,y-1,tot+a[x][y-1]);pd[x][y-1]=false;}
}
void dfs(int x,int y,int tot){
	if(x==m&&y==n){dfss(m,n,tot);return;}
	if(x<m){pd[x+1][y]=true;dfs(x+1,y,tot+a[x+1][y]);pd[x+1][y]=false;}
	if(y<n){pd[x][y+1]=true;dfs(x,y+1,tot+a[x][y+1]);pd[x][y+1]=false;}
}
int main(){
	cin>>m>>n;
	for(int i=1;i<=m;i++)
	for(int j=1;j<=n;j++)
	cin>>a[i][j];
	dfs(1,1,0);
	cout<<sum;
	return 0;
}
2020/8/27 20:55
加载中...