language: C (gcc-4.7.2)
date: 228 days 22 hours ago
link:
visibility: private
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include<stdio.h>
 
int findmin(int (*arr)[51],int rlow,int rhigh, int clow, int chigh)
{
    if(rlow==rhigh||clow==chigh)
    return 0;
    int i,min,p=0,q=0,r=0,s=0;
    for(i=clow;i<chigh;i++)
       p=p+arr[rlow][i];
       for(i=clow;i<chigh;i++)
       q=q+arr[rhigh-1][i];
       for(i=rlow;i<rhigh;i++)
       r=r+arr[i][clow];
       for(i=rlow;i<rhigh;i++)
       s=s+arr[i][chigh-1];
 
       min=p;
       if(q<min)
       min=q;
       if(r<min)
       min=r;
       if(s<min)
       min=s;
 
       return(min);
}
 
int deletearr(int (*arr)[51],int rlow,int rhigh,int clow,int chigh,int a,int b,int atotal, int btotal)
{
   if(clow==chigh||rlow==rhigh)
   {
      if(atotal>btotal)
      return (atotal);
      else if(btotal>atotal)
      return(btotal);
      else
      return(atotal+btotal);
   }
   int p=0,q=0,r=0,s=0,i,min,max,temp,count,value;
   char MAX;
   if(a==1)
   {
       for(i=clow;i<chigh;i++)
       p=p+arr[rlow][i];
       for(i=clow;i<chigh;i++)
       q=q+arr[rhigh-1][i];
       for(i=rlow;i<rhigh;i++)
       r=r+arr[i][clow];
       for(i=rlow;i<rhigh;i++)
       s=s+arr[i][chigh-1];
 
       min=p;
       if(q<min)
       min=q;
       if(r<min)
       min=r;
       if(s<min)
       min=s;
      
       if(min==p)
       rlow=rlow+1;
       else if(min==q)
       rhigh=rhigh-1;
       else if(min==r)
       clow=clow+1;
       else
       chigh=chigh-1;
 
       deletearr(arr,rlow,rhigh,clow,chigh,0,1,atotal+min,btotal);
       
   }
   else
   {
       for(i=clow;i<chigh;i++)
       p=p+arr[rlow][i];
       for(i=clow;i<chigh;i++)
       q=q+arr[rhigh-1][i];
       for(i=rlow;i<rhigh;i++)
       r=r+arr[i][clow];
       for(i=rlow;i<rhigh;i++)
       s=s+arr[i][chigh-1];
 
       max=p;
       if(q>=max)
       max=q;
       if(r>=max)
       max=r;
       if(s>=max)
       max=s;
      
       count=0;
       if(max==p)
       {
           temp=findmin(arr,rlow+1,rhigh,clow,chigh);
           min=temp;MAX='p';count++;
       }
       if(max==q)
       {
           temp=findmin(arr,rlow,rhigh-1,clow,chigh);
           if(count==0)
           {min=temp;MAX='q';count++;}
           else if(temp<min)
           {min=temp;MAX='q';}
       }
       if(max==r)
       {
          temp=findmin(arr,rlow,rhigh,clow+1,chigh);
           if(count==0)
           {min=temp;MAX='r';}
           else if(temp<min)
           {min=temp;MAX='r';} 
       }
       if(max==s)
       {
           temp=findmin(arr,rlow,rhigh,clow,chigh-1);
           if(count==0)
           {min=temp;MAX='s';}
           else if(temp<min)
           {min=temp;MAX='s';}
       }
 
       if(MAX=='p')
       {rlow=rlow+1;value=p;}
       else if(MAX=='q')
       {rhigh=rhigh-1;value=q;}
       else if(MAX=='r')
       {clow=clow+1;value=r;}
       else if(MAX=='s')
       {chigh=chigh-1;value=s;}
 
       deletearr(arr,rlow,rhigh,clow,chigh,1,0,atotal,btotal+value);
       
   }
}
 
int main()
{
   int t,i,j,k,r,c,arr[51][51],res;
   scanf("%d",&t);
   for(i=0;i<t;i++)
   {
      scanf("%d%d",&r,&c);
      for(j=0;j<r;j++)
      for(k=0;k<c;k++)
      scanf("%d",&arr[j][k]);
 
      res=deletearr(arr,0,r,0,c,1,0,0,0);
      printf("%d\n",res);
   }
   return 0;
}