P8601???
  • 板块题目总版
  • 楼主lxz1227
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/8/18 16:04
  • 上次更新2024/8/18 19:04:43
查看原帖
P8601???
1065123
lxz1227楼主2024/8/18 16:04

WA TLE WA AC AC AC AC QAQ 本人代码:``c #include<bits/stdc++.h> using namespace std; int m,n; int a[10][10]; bool visit[10][10]; long long ans=0,sum=0; int cnt=INT_MAX; int t[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
void dfs(int x,int y,int k){ if(visit[x][y]==true) return; if(x>=n||y>=m||x<0||y<0)return; if(sum-ans==ans){ cnt=min(cnt,k);
return; } for(int i=0;i<4;i++){ int x1=x+t[i][0]; int y1=y+t[i][1]; visit[x][y]=true;
ans+=a[x][y];
k++;
dfs(x1,y1,k); k--;
ans-=a[x][y]; visit[x][y]=false; } } int main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin>>m>>n; for(int i=0;i<n;i++) for(int j=0;j<m;j++){ cin>>a[i][j]; sum+=a[i][j]; } dfs(0,0,0); if(cnt==INT_MAX) cout<<0<<endl; else cout<<cnt<<endl; return 0; }


#include<bits/stdc++.h>
using namespace std;
int m,n;
int a[10][10]; 
bool visit[10][10]; 
long long ans=0,sum=0; 
int cnt=INT_MAX;
int t[4][2]={{0,1},{1,0},{0,-1},{-1,0}};  
void dfs(int x,int y,int k){
    if(visit[x][y]==true) return; 
    if(x>=n||y>=m||x<0||y<0)return;
    if(sum-ans==ans){ 
        cnt=min(cnt,k);  
        return;
    }
    for(int i=0;i<4;i++){
        int x1=x+t[i][0]; 
        int y1=y+t[i][1];
        visit[x][y]=true;  
        ans+=a[x][y];  
        k++;   
        dfs(x1,y1,k);
        k--;  
        ans-=a[x][y];
        visit[x][y]=false;
    }
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
    cin>>m>>n; 
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++){
            cin>>a[i][j];
            sum+=a[i][j]; 
        }
    dfs(0,0,0);
    if(cnt==INT_MAX) 
        cout<<0<<endl;
    else
        cout<<cnt<<endl;
    return 0;
}
2024/8/18 16:04
加载中...