Cainiao is a university student who loves ACM contest very much. It is a festival for him once when he attends ACM Asia Regional Contest because he always can find some famous ACMers there.
Cainiao attended Asia Regional Contest Fuzhou Site on November 20, 2011. After he got seat map, he wanted to know which seat is the best one.
Cainiao have joined so many QQ Group about ACM/ICPC that he is almost familiar with the strength of each team. In his mind, the value of a seat is defined as following:
1. Strength of each team can be expressed as a positive integer.
2. The value of a seat is related to the adjacent seat (up/down/left/right, only four directions being considering).
3. For an adjacent seat, if the strength of this team is stronger than yours, the absolute value of difference of two teams should be added to your seat, otherwise, the absolute value of difference should be subtracted from your seat.
4. If the adjacent seat is empty (which means you are located at the most left/right/up/down), the value of your seat should be subtracted 1.
5. The best one in a contest is the seat that has the highest value.
6. The initial value of the seat is ZERO.
Input contain a positive integer T( T <=50 ) in the first line, which means T cases.
The first line of each case contains two positive integers N and M (3 <= N, M <= 20) which means the row and column number of the teams, then N rows following, each line contains M positive integers that represent the strengths of the teams.
For each case, first output the case number, and then output the value and row number and column number of the best seat in one line for each case.
If there are multiple solutions for one case, you should output the seat whose row number is largest and only output the seat whose column number is largest if still overlapping.
1 3 4 1 5 3 4 6 3 3 4 4 3 2 1
Case 1: 7 1 1
#include <iostream> #include <cstring> using namespace std; int seats[25][25]; int nvalue[25][25]; int N,M,t,sum,i,j,nmax,ni,nj,ncase; void cal() { int i,j; for(i = 1; i <= N; i++) for(j = 1; j <= M; j++) { nvalue[i][j] += seats[i][j+1] + seats[i][j-1] + seats[i+1][j] + seats[i-1][j] - 4*seats[i][j]; } for(i = 1; i <= N; i++) { nvalue[i][M] +=seats[i][M]; nvalue[i][1] +=seats[i][1]; } for(j = 1; j <= M; j++) { nvalue[1][j] += seats[1][j]; nvalue[N][j] += seats[N][j]; } } int main() { cin >> t; ncase = 1; while(t--) { cin >> N >> M; for(i = 0;i < 25;i++) for(j = 0;j < 25;j++) seats[i][j] = -1; memset(nvalue,0,sizeof(nvalue)); for(i = 1;i <= N;i++) for(j = 1;j <= M;j++) { cin >> seats[i][j]; } cal(); nmax = nvalue[N][M]; ni =N; nj =M; for(i = N;i >=1 ;i--)//倒着搜,为了实现行最大 for(j = M;j >=1;j--)//同上,实现列最大 { if(nmax < nvalue[i][j])//不能用等于号,不然实现不了按行与列排序的顺序 { nmax = nvalue[i][j]; ni = i;nj = j; } } cout << "Case "<< ncase++ << ": "<<nmax<<" "<<ni<<" "<<nj<<endl; } return 0; }