http://acm.hdu.edu.cn/showproblem.php?pid=4500
AC代码:
1 #include<math.h> 2 #include<stdio.h> 3 4 #define MAXN 21 5 6 int n, m, ki[MAXN][MAXN]; 7 int wi[4]={0, 1, 0, -1}, 8 wj[4]={-1, 0, 1, 0}; 9 10 bool input(){ 11 scanf("%d%d", &n, &m); 12 if(n == 0 && m == 0){ 13 return false; 14 } 15 for(int i = 1; i <= n; ++i){ 16 for(int j = 1; j <= m; ++j){ 17 scanf("%d", &ki[i][j]); 18 } 19 } 20 return true; 21 } 22 23 void solve(){ 24 int bi = 0, bj = 0, bk = 0; 25 //最优值得行坐标 列坐标 做高分 26 for(int i = 1; i <= n; ++i){ 27 for(int j = 1; j <= m; ++j){ 28 int kcore = 0; 29 30 for(int k = 0; k < 4; ++k){ 31 //上下左右4个位置 32 int ti = i + wi[k], tj = j + wj[k]; 33 if(1 <= ti && ti <= n && 1 <= tj && tj <= m){ 34 if(ki[i][j] * ki[ti][tj] < 0){ 35 kcore += abs(ki[ti][tj]); 36 }else{ 37 kcore -= abs(ki[ti][tj]); 38 } 39 } 40 } 41 42 if(kcore > bk){ 43 //更新最优值 44 bk = kcore; 45 bi = i; 46 bj = j; 47 } 48 } 49 } 50 printf("%d %d %d ", bi, bj, bk); 51 } 52 53 int main(){ 54 while(input()){ 55 solve(); 56 } 57 return 0; 58 }