#include<cstdio>
#include<cctype>
using namespace std;
const int N = 1002;
struct node {
int l, r, x, y, id;
}s;
int n, m, G[N][N], ans[N * N], tail;
bool f[N][N];
node find(int sx, int sy) {
while(G[sx][sy] != '+')
sy --;
int tx = sx + 1, ty = sy + 1, num = 0;
while(G[sx][ty] != '+')
ty ++;
while(G[tx][sy] != '+')
tx ++;
for(int i = sx; i <= tx; i ++)
for(int j = sy; j <= ty; j ++) {
f[i][j] = 1;
if(isdigit(G[i][j]))
num = (num << 1) + (num << 3) + G[i][j] - '0';
}
return node {sx, sy, tx, ty, num};
}
node getS(int x, int y) {
while(1) {
f[x][y] = 1;
if(G[x][y] == '|') {
if(G[x + 1][y] == '-')
return find(x + 1, y);
if(! f[x + 1][y])
x ++;
}
else if(G[x][y] == '+') {
if(! f[x + 1][y])
x ++;
else if(! f[x][y - 1])
y --;
else if(! f[x][y + 1])
y ++;
}
else {
if(! f[x][y + 1])
y ++;
else if(! f[x][y - 1])
y --;
}
}
}
void dfs(node u) {
for(int i = u.x - 1; i > u.l; i --) {
if(G[i][u.r - 1] == '-')
dfs(getS(i, u.r - 1));
if(G[i][u.y + 1] == '-')
dfs(getS(i, u.y + 1));
}
ans[++ tail] = u.id;
}
int main() {
scanf("%d %d", &n, &m);
for(int i = 1; i <= n; i ++) {
scanf("\n");
for(int j = 1; j <= m; j ++)
scanf("%c", &G[i][j]);
}
for(int i = 0; i <= n + 1; i ++)
for(int j = 0; j <= m + 1; j ++)
if(! i || (! j) || i > n || j > m || G[i][j] == '.')//A
f[i][j] = 1;
for(int i = 1; i <= n && (! s.l); i ++)
for(int j = 1; j <= m && (! s.l); j ++)
if(G[i][j] != '.')
s = find(i, j);
dfs(s);
for(int i = 1; i <= tail; i ++)
printf("%d\n", ans[i]);
return 0;
}
注:
A:精妙的f数组,回避了判断冗长的if。
只想说,一份优美的代码很重要!!!