Bullet+回溯

Bullet

Time Limit: 1000 ms Memory Limit: 65536 KiB

Problem Description

In GGO, a world dominated by gun and steel, players are fighting for the honor of being the strongest gunmen. Player Shino is a sniper, and her aimed shot kills one monster at a time. Now she is in an n×nn \times nn×n map, and there are monsters in some grids. Each monster has an experience. As a master, however, Shino has a strange self-restrain. She would kill at most one monster in a column, and also at most one in a row. Now she wants to know how to get max experience, under the premise of killing as many monsters as possible.

Input

The first line contains an integer nnn
Then n lines follow. In each line there are n integers, and AijAijAij represents the experience of the monster at grid (i,j)(i,j)(i,j). If Aij=0Aij=0Aij=0, there is no monster at grid (i,j)(i,j)(i,j).

Output

One integer, the value of max experience.

Sample Input

2
2 0
1 8

Sample Output

2

Hint

Source

“浪潮杯”山东省第九届ACM大学生程序设计竞赛(感谢山东财经大学)
#define happy

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define all(a) (a).begin(),(a).end()
#define pll pair<ll,ll>
#define vi vector<int>
#define pb push_back
const int inf=0x3f3f3f3f;
ll rd(){
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
const int N=505;
int a[N][N];
int x[N],y[N];
int n,tans,ans;
ll sum;
void dfs(int k,ll d){
    if(k==n+1){
        if(d>=sum){
            sum=d;
            ans=min(ans,tans);
        }
        return ;
    }
    bool f=false;
    for(int i=1;i<=n;i++){
        if(a[k][i]==0||x[i]||y[k])continue;
        f=true;
        int tt=tans;
        x[i]=y[k]=1;
        tans=min(tans,a[k][i]);
        dfs(k+1,d+a[k][i]);
        x[i]=y[k]=0;
        tans=tt;
    }
    if(!f)dfs(k+1,d);
}

int main(){
#ifdef happy
    freopen("in.txt","r",stdin);
#endif
    n=rd();
    rep(i,1,n)rep(j,1,n)
    a[i][j]=rd();
    sum=0;
    tans=ans=inf;
    dfs(1,0);
    printf("%d\n",ans);
}


猜你喜欢

转载自blog.csdn.net/ujn20161222/article/details/80330017