这道题真的处理起来好复杂啊,题意就是个简单的方格填数问题,但是每个白点至少放1,那么最后的可能解是怎样的呢?我们是不是要把x轴上的和y轴上的统一起来,然后就是每个点都被对应的x和y匹配起来,那么,之后,用每个点的x向y建立边,跑最大流,每个点的放的值就是反向边的权值了。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
#define MP(x, y) make_pair(x, y)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 1e2 + , maxE = 1e5 + , st = ;
pair<int, int> re_xy[];
int N, M, head[], cur[], cnt, ed, id[maxN][maxN], tot, ou[maxN][maxN];
char mp[maxN][maxN][];
int down[maxN][maxN], righ[maxN][maxN];
struct Eddge
{
int nex, to, flow;
Eddge(int a=-, int b=, int c=):nex(a), to(b), flow(c) {}
}edge[maxE];
inline void addEddge(int u, int v, int w)
{
edge[cnt] = Eddge(head[u], v, w);
head[u] = cnt++;
}
inline void _add(int u, int v, int w) { addEddge(u, v, w); addEddge(v, u, ); }
inline void solve(int x, int y)
{
if(mp[x][y][] == 'X' && mp[x][y][] == 'X') { id[x][y] = -; return; }
if(mp[x][y][] == '.') { return; }
re_xy[++tot] = MP(x, y);
id[x][y] = tot;
down[x][y] = righ[x][y] = ;
if(mp[x][y][] != 'X') for(int i=; i<=; i++) down[x][y] = down[x][y] * + mp[x][y][i] - '';
if(mp[x][y][] != 'X') for(int i=; i<=; i++) righ[x][y] = righ[x][y] * + mp[x][y][i] - '';
}
int deep[];
queue<int> Q;
inline bool bfs()
{
for(int i=; i<=ed; i++) deep[i] = ;
deep[st] = ;
while(!Q.empty()) Q.pop();
Q.push(st);
while(!Q.empty())
{
int u = Q.front(); Q.pop();
for(int i=head[u], v, f; ~i; i=edge[i].nex)
{
v = edge[i].to; f = edge[i].flow;
if(f && !deep[v])
{
deep[v] = deep[u] + ;
Q.push(v);
}
}
}
return deep[ed];
}
int dfs(int u, int Dist)
{
if(u == ed) return Dist;
for(int &i=cur[u], v, f; ~i; i=edge[i].nex)
{
v = edge[i].to; f = edge[i].flow;
if(f && deep[v] == deep[u] + )
{
int di = dfs(v, min(Dist, f));
if(di)
{
edge[i].flow -= di;
edge[i^].flow += di;
return di;
}
}
}
return ;
}
inline int Dinic()
{
int ans = , tmp;
while(bfs())
{
for(int i=; i<=ed; i++) cur[i] = head[i];
while((tmp = dfs(st, INF))) ans += tmp;
}
return ans;
}
inline void init()
{
cnt = tot = ;
memset(head, -, sizeof(head));
memset(id, , sizeof(id));
}
int main()
{
while(scanf("%d%d", &N, &M) != EOF)
{
init();
for(int i=; i<=N; i++)
{
for(int j=; j<=M; j++)
{
scanf("%s", mp[i][j] + );
solve(i, j);
}
}
ed = (tot << ) + ;
for(int i=, le_id=, up_id=; i<=N; i++)
{
for(int j=; j<=M; j++)
{
if(mp[i][j][] == '.')
{
for(int dx = i - ; dx > ; dx--)
{
if(id[dx][j])
{
up_id = dx;
down[dx][j]--;
break;
}
}
for(int dy = j - ; dy > ; dy--)
{
if(id[i][dy])
{
le_id = dy;
righ[i][dy]--;
break;
}
}
_add(id[i][le_id], id[up_id][j] + tot, );
}
}
}
for(int i=; i<=N; i++)
{
for(int j=; j<=M; j++)
{
if(id[i][j] > )
{
if(mp[i][j][] != 'X') _add(id[i][j] + tot, ed, down[i][j]);
if(mp[i][j][] != 'X') _add(st, id[i][j], righ[i][j]);
}
}
}
Dinic();
for(int x = ; x <= tot; x++)
{
for(int i=head[x], y, f; ~i; i=edge[i].nex)
{
y = edge[i].to; f = edge[i^].flow;
if(y == st) continue;
ou[re_xy[x].first][re_xy[y - tot].second] = f;
}
}
for(int i=; i<=N; i++)
{
for(int j=; j<=M; j++)
{
if(mp[i][j][] != '.') printf("_");
else printf("%d", ou[i][j] + );
printf("%c", j == M ? '\n' : ' ');
}
}
}
return ;
}
/*
3 3
XXXXXXX 009/XXX XXXXXXX
XXX/009 ....... XXXXXXX
XXXXXXX XXXXXXX XXXXXXX
*/