首页 > 其他 > 详细

HDU 4708

时间:2014-11-24 22:40:23      阅读:344      评论:0      收藏:0      [点我收藏+]

对每个环都进行遍历,找出最大值

如果值相等的话就可以还要判断下走的步数

Rotation Lock Puzzle

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1369    Accepted Submission(s): 431


Problem Description
Alice was felling into a cave. She found a strange door with a number square matrix. These numbers can be rotated around the center clockwise or counterclockwise. A fairy came and told her how to solve this puzzle lock: “When the sum of main diagonal and anti-diagonal is maximum, the door is open.”.
Here, main diagonal is the diagonal runs from the top left corner to the bottom right corner, and anti-diagonal runs from the top right to the bottom left corner. The size of square matrix is always odd.

bubuko.com,布布扣


This sample is a square matrix with 5*5. The numbers with vertical shadow can be rotated around center ‘3’, the numbers with horizontal shadow is another queue. Alice found that if she rotated vertical shadow number with one step, the sum of two diagonals is maximum value of 72 (the center number is counted only once).
 

Input
Multi cases is included in the input file. The first line of each case is the size of matrix n, n is a odd number and 3<=n<=9.There are n lines followed, each line contain n integers. It is end of input when n is 0 .
 

Output
For each test case, output the maximum sum of two diagonals and minimum steps to reach this target in one line.
 

Sample Input
5 9 3 2 5 9 7 4 7 5 4 6 9 3 9 3 5 2 8 7 2 9 9 4 1 9 0
 

Sample Output
72 1
 

Source
 

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <limits.h>
#include <ctype.h>
#include <string.h>
#include <string>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <deque>
#include <vector>
#include <set>
#include <map>
using namespace std;
#define MAXN 12

int num[12][12];

int main(){
    int n,i,j,k;

    while(~scanf("%d",&n)){
        if(n == 0){
            break;
        }
        for(i=1;i<=n;i++){
            for(j=1;j<=n;j++){
                scanf("%d",&num[i][j]);
            }
        }
        int ans = 0;
        int anscon = 0;
        for(k=1;k<=n/2;k++){
            int marksum = -1;
            int mark = k;
            int sum=0;
            for(i=k;i<=n-k;i++){
                sum = 0;
                //printf("%d %d %d %d\n",num[k][i],num[i][n-k+1],num[n-k+1][n-i+1],num[n-i+1][k]);
                sum+=num[k][i];
                sum+=num[i][n-k+1];
                sum+=num[n-k+1][n-i+1];
                sum+=num[n-i+1][k];
                //printf("%d\n",sum);
                if(sum > marksum){
                    marksum = sum;
                    mark = min(i-k,(n-k+1-i));
                }
                if(sum == marksum){
                    int mark1 = min(i-k,n-k+1-i);
                    mark = min(mark1,mark);
                }
            }
            //printf("%d\n",num[n/2+1][n/2+1]);
            ans+=marksum;
            anscon+=mark;
        }
        printf("%d %d\n",ans+num[n/2+1][n/2+1],anscon);
    }

    return 0;
}


HDU 4708

原文:http://blog.csdn.net/zcr_7/article/details/41450633

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!