社区讨论

P8601求助???

灌水区参与者 1已保存回复 0

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
0 条
当前快照
1 份
快照标识符
@lzzads8h
此快照首次捕获于
2024/08/18 16:09
2 年前
此快照最后确认于
2024/08/18 19:06
2 年前
查看原帖
测试点信息: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;
}

回复

0 条回复,欢迎继续交流。

正在加载回复...