朝四个方向搜索即可
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
int arr[25][25], v[25][25];
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int n, m, Max, a, b;
void bfs(int x, int y){
int tx, ty, flag = 0;
if (arr[x][y] > 0)
flag = 1;
for(int i = 0; i < 4; i++){
tx = x + dx[i];
ty = y + dy[i];
if (tx > 0 && tx <= n && ty > 0 && ty <= m){
if (flag){
if (arr[tx][ty] > 0)
v[x][y] -= arr[tx][ty];
else
v[x][y] += abs(arr[tx][ty]);
}
else{
if (arr[tx][ty] > 0)
v[x][y] += arr[tx][ty];
else
v[x][y] -= abs(arr[tx][ty]);
}
}
}
if (v[x][y] > Max){
Max = v[x][y];
a = x;
b = y;
}
if (v[x][y] == Max){
if (x < a)
Max = v[x][y];
else if (x == a){
if (y < b)
Max = v[x][y];
}
}
}
int main(){
while (scanf("%d %d", &n, &m) && n && m){
memset(arr, 0, sizeof(arr));
memset(v, 0, sizeof(v));
Max = 0;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++ )
scanf("%d", &arr[i][j]);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
bfs(i, j);
printf("%d %d %d\n", a, b, Max);
}
return 0;
}
HDU4500
最新推荐文章于 2016-11-04 17:20:49 发布