POJ 2078 Martrix

                                                             Matrix
Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 3096   Accepted: 1612

Description

Given an n*n matrix A, whose entries Ai,j are integer numbers ( 0 <= i < n, 0 <= j < n ). An operation SHIFT at row i ( 0 <= i < n ) will move the integers in the row one position right, and the rightmost integer will wrap around to the leftmost column. 

You can do the SHIFT operation at arbitrary row, and as many times as you like. Your task is to minimize 
max0<=j< n{Cj|Cj=Σ0<=i< nAi,j}

Input

The input consists of several test cases. The first line of each test case contains an integer n. Each of the following n lines contains n integers, indicating the matrix A. The input is terminated by a single line with an integer −1. You may assume that 1 <= n <= 7 and |Ai,j| < 104.

Output

For each test case, print a line containing the minimum value of the maximum of column sums.

Sample Input

24 63 731 2 34 5 67 8 9-1

 

 

 1 #include <iostream>
 2 #include <algorithm>
 3 
 4 using namespace std;
 5 
 6 int n;
 7 int mini=9999999;
 8 
 9 void zhuan(int cur,int a[8][8])
10 {
11     reverse(a[cur],a[cur]+n-1);
12     reverse(a[cur],a[cur]+n);
13 }
14 
15 void dfs(int cur,int a[8][8])
16 {
17     if(cur!=n)
18     {
19         for(int i=0;i<n;i++)
20         {
21             zhuan(cur,a);
22             dfs(cur+1,a);
23         }
24     }
25     else if(cur==n)
26     {
27         int sum;
28         int maxx=-1;
29         for(int i=0;i<n;i++)
30         {
31             sum=0;
32             for(int j=0;j<n;j++)
33             {
34                 sum+=a[j][i];
35             }
36             if(sum>maxx)
37                 maxx=sum;
38         }
39         if(maxx<mini)
40             mini=maxx;
41     }
42 }
43 
44 int main()
45 {
46     int a[8][8];
47     cin>>n;
48     while(n!=-1)
49     {
50 
51         for(int i=0;i<n;i++)
52         {
53             for(int j=0;j<n;j++)
54             {
55                 cin>>a[i][j];
56             }
57         }
58         mini=9999999;
59         dfs(0,a);
60 
61         cout<<mini<<endl;
62 
63     cin>>n;
64     }
65 
66     return 0;
67 }

 

posted @ 2013-04-11 12:02  码代码的猿猿  阅读(141)  评论(0编辑  收藏  举报