P8601求助???
  • 板块灌水区
  • 楼主lxz1227
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/8/18 16:07
  • 上次更新2024/8/18 19:06:33
查看原帖
P8601求助???
1065123
lxz1227楼主2024/8/18 16:07

测试点信息:WA TLE WA AC AC AC AC QAQ 本人代码:

#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:07
加载中...