# 炸弹 树形地图

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
using namespace std;

#define st first
#define nd second
#define pi 3.141592653589793238462643383

const int dx[4] = {0, 0, 1, -1};
const int dy[4] = {1, -1, 0, 0};
const int inf = 0x3f3f3f3f;

#define Rep(i, n) for (int i = 0, _n = n; i < _n; i ++)
#define Repp(i, n) for (int i = 1, _n = n; i <= _n; i ++)
#define For(i, a, b) for (int i = a, _b = b; i <= _b; i ++)
#define Forr(i, a, b) for (int i = a, _b = b; i >= _b; i --)

typedef long long ll;
typedef pair<int, int> pii;

#define maxn 57
#define maxp maxn*maxn

int n, m, sx, sy;
int dat[maxn][maxn];

struct Pointer{
Pointer *lc, *rc;
int lf, rf;

int get() {
return min(lf, rf);
}

void Init() {
scanf("%d%d\n", &n, &m);

nll -> lf = nll -> rf = 0;

int cnt = 0;    char ch;
Repp(i, n) {
Repp(j, m) {
scanf("%c", &ch);
if (ch == '.') {
// 编号
dat[i][j] = ++ cnt;

if (cnt == 1) sx = i, sy = j;
}
}
scanf("\n");
}
}

void dfs(Pointer *x) {
if (x == nll) return;
dfs(x -> lc);
dfs(x -> rc);

if (x -> rc != nll)
x -> lf = x -> rc -> lf + x -> lc -> get();
else x -> lf = inf;

if (x -> lc != nll)
x -> rf = x -> lc -> rf + x -> rc -> get();
else x -> rf = inf;

// 其实这里可以多记录两个部分和，那么不用每次往下找一次
// 时间复杂度从 O(n ^ 3) 降到 O(n ^ 2)
int tmp = 1;
Pointer *p = x;
while (true) {
p = p -> lc;
if (p == nll) break;
tmp += p -> rc -> get();
}

p = x;
while (true) {
p = p -> rc;
if (p == nll) break;
tmp += p -> lc -> get();
}

x -> lf = min(x -> lf, tmp);
x -> rf = min(x -> rf, tmp);
}

pii que[maxp];

// 构建二叉树
void Graph() {
int quehead = 0, quetail = 0;
que[0].st = sx, que[0].nd = sy;

Rep(k, 4) {
pii to;
to.st = now.st + dx[k];
to.nd = now.nd + dy[k];

if (dat[to.st][to.nd] > 0) {

// 若已经有(左、右）孩子，则一直找(左、右）孩子的(左、右）孩子。
if (k < 2) {
while (p -> lc != nll) p = p -> lc;
}
else {
while (p -> rc != nll) p = p -> rc;
}

que[++ quetail] = to;
}
}

dat[now.st][now.nd] = 0;
}
}

int main() {
freopen("bomb.in", "r", stdin);
freopen("bomb.out", "w", stdout);

Init();

Graph();

return 0;
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120