HDU 4708 Rotation Lock Puzzle (热身赛第三题)

转载请注明出处:http://blog.csdn.net/a1dark

分析:这题卡了我很久、WA了7次、最主要是没有考虑到如果在同一层出现多种最大的情况的时候该如何判断、别的都比较容易、

#include<stdio.h>
#include<string.h>
int max[15][15];
int map[15][15];
int maxx[15];
int x[15];
int y[15];
int xx[100];
int yy[100];
int s[15];
int min(int a,int b){
    if(a<b)return a;
    return b;
}
int main(){
    int n,i,j;
    while(scanf("%d",&n)!=EOF){
        if(n==0)break;
        for(i=1;i<=n;i++)
            for(j=1;j<=n;j++){
                scanf("%d",&map[i][j]);
            }
        memset(max,0,sizeof(max));
        for(i=1;i<=n;i++){
            for(j=1;j<=n;j++){
                if(map[i][j]+map[n-i+1][n-j+1]+map[n-j+1][i]+map[j][n-i+1]>=max[i][j])
                    max[i][j]=map[i][j]+map[n-i+1][n-j+1]+map[n-j+1][i]+map[j][n-i+1];
            }
        }
        memset(maxx,0,sizeof(maxx));
        for(i=1;i<=n/2;i++){
                x[i]=i;
                y[i]=i;
            }
        memset(s,-1,sizeof(s));
        for(i=1;i<=n/2;i++)
            for(j=i;j<=n-i+1;j++){
                if(max[i][j]>maxx[i]||(max[i][j]==maxx[i]&&s[i]>min((j-i),(n-i+1-j)))){
                    x[i]=i;
                    y[i]=j;
                    s[i]=min((x[i]-i+y[i]-i),(n-i+1-y[i]+x[i]-i));
                    maxx[i]=max[i][j];
                }
            }
        for(j=1;j<=n/2;j++)
            for(i=j;i<=n-j+1;i++){
                if(max[i][j]>maxx[j]){
                    x[j]=i;
                    y[j]=j;
                    maxx[j]=max[i][j];
                }
            }
        int sum=0;
        int step=0;
       	for(i=1;i<=n/2;i++){
            sum+=maxx[i];
       	}
       	sum+=map[n/2+1][n/2+1];
       	for(i=1;i<=n/2;i++)
            step+=min((x[i]-i+y[i]-i),(n-i+1-y[i]+x[i]-i));
       	printf("%d %d\n",sum,step);
    }
    return 0;
}


你可能感兴趣的:(模拟,ACM)