Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1122 Accepted Submission(s): 689


#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <iomanip>
#include <math.h>
#include <map>
using namespace std;
#define FIN freopen("input.txt","r",stdin);
#define FOUT freopen("output.txt","w",stdout);
#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef long long LL;
typedef pair<int,int> PII;
const int MAXN = 50 + 5;
int mp[MAXN][MAXN];
int T;
int n, m;
int cnt;
int to[4][2] = {1, 0, -1, 0, 0, -1, 0, 1};
void llss(int x, int y) {
if(mp[x][y] == 0) return ;
for(int i = 0; i < 4; i++) {
int xx = x + to[i][0];
int yy = y + to[i][1];
if(mp[xx][yy] >= mp[x][y]) {
cnt -= mp[x][y];
}
if(mp[xx][yy] < mp[x][y] && mp[xx][yy] != 0) {
cnt -= mp[xx][yy];
}
}
}
int main()
{
//FIN
scanf("%d", &T);
while(T--) {
memset(mp, 0, sizeof(mp));
scanf("%d%d", &n, &m);
cnt = 0;
int num = 0;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
scanf("%d", &mp[i][j]);
cnt += mp[i][j];
if(mp[i][j] != 0) num++;
}
}
cnt *= 6;
//cout << cnt << endl;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
llss(i, j);
if(mp[i][j] >= 2) {
cnt -= (mp[i][j] - 1) * 2;
}
//cout << cnt << endl;
}
}
cnt -= num;
printf("%d\n", cnt);
}
return 0;
}
原文:http://www.cnblogs.com/Hyouka/p/5875293.html