题目描述 | If you solved problem like this, forget it.Because you need to use a completely different algorithm to solve the following one. Picture of the first sample input |
Input | The first line of input contains two integers n and m (2 ≤ n,m ≤ 100) — the number of rows and columns correspondingly. Each of the next n lines contains descriptions of m cells. Each cell description is one of the following 7-character strings: .......— "white" cell; XXXXXXX— "black" cell with no clues; AAA\BBB— "black" cell with one or two clues. AAA is either a 3-digit clue for the corresponding vertical run, or XXX if there is no associated vertical run. BBB is either a 3-digit clue for the corresponding horizontal run, or XXX if there is no associated horizontal run. The first row and the first column of the grid will never have any white cells. The given grid will have at least one "white" cell.It is guaranteed that the given puzzle has at least one solution. |
Output | Print n lines to the output with m cells in each line. For every "black" cell print '_' (underscore), for every "white" cell print the corresponding digit from the solution. Delimit cells with a single space, so that each row consists of 2m-1 characters.If there are many solutions, you may output any of them. |
Sample Input | 6 6 XXXXXXX XXXXXXX 028\XXX 017\XXX 028\XXX XXXXXXX XXXXXXX 022\022 ....... ....... ....... 010\XXX XXX\034 ....... ....... ....... ....... ....... XXX\014 ....... ....... 016\013 ....... ....... XXX\022 ....... ....... ....... ....... XXXXXXX XXXXXXX XXX\016 ....... ....... XXXXXXX XXXXXXX 5 8 XXXXXXX 001\XXX 020\XXX 027\XXX 021\XXX 028\XXX 014\XXX 024\XXX XXX\035 ....... ....... ....... ....... ....... ....... ....... XXXXXXX 007\034 ....... ....... ....... ....... ....... ....... XXX\043 ....... ....... ....... ....... ....... ....... ....... XXX\030 ....... ....... ....... ....... ....... ....... XXXXXXX |
Sample Output | _ _ _ _ _ _ _ _ 5 8 9 _ _ 7 6 9 8 4 _ 6 8 _ 7 6 _ 9 2 7 4 _ _ _ 7 9 _ _ _ _ _ _ _ _ _ _ _ 1 9 9 1 1 8 6 _ _ 1 7 7 9 1 9 _ 1 3 9 9 9 3 9 _ 6 7 2 4 9 2 _ |
/*
网络流
源 -> 列和 -> 单元 ->行和 ->汇
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
#define Del(a,b) memset(a,b,sizeof(a))
const int N = 20500;
const int inf = 0x3f3f3f3f;
int n, m;
struct Node {
int from, to, cap, flow;
};
vector<int> v[N];
vector<Node> e;
int vis[N]; //构建层次图
int cur[N];
void add_Node(int from, int to, int cap) {
e.push_back((Node) {from, to, cap, 0});
e.push_back((Node) {to, from, 0, 0});
int tmp = e.size();
v[from].push_back(tmp - 2);
v[to].push_back(tmp - 1);
}
bool bfs(int s, int t) {
Del(vis, -1);
queue<int> q;
q.push(s);
vis[s] = 0;
while(!q.empty()) {
int x = q.front();
q.pop();
for(int i = 0; i<v[x].size(); i++) {
Node tmp = e[v[x][i]];
if(vis[tmp.to]<0 && tmp.cap>tmp.flow) { //第二个条件保证
vis[tmp.to]=vis[x]+1;
q.push(tmp.to);
}
}
}
if(vis[t]>0)
return true;
return false;
}
int dfs(int o,int f,int t) {
if(o==t || f==0) //优化
return f;
int a = 0,ans=0;
for(int &i=cur[o]; i<v[o].size(); i++) { //注意前面 ’&‘,很重要的优化
Node &tmp = e[v[o][i]];
if(vis[tmp.to]==(vis[o]+1) && (a = dfs(tmp.to,min(f,tmp.cap-tmp.flow),t))>0) {
tmp.flow+=a;
e[v[o][i]^1].flow-=a; //存图方式
ans+=a;
f-=a;
if(f==0) //注意优化
break;
}
}
return ans; //优化
}
int dinci(int s,int t) {
int ans=0;
while(bfs(s,t)) {
Del(cur,0);
int tm = dfs(s,inf,t);
ans += tm;
}
return ans;
}
int mp[250][250];
struct Node1 {
int x, y, z;
};
Node1 raw[N], col[N];
int solve(int raw_cnt, int id,int s) {
int cnt = 0, pos = id + raw_cnt;
return e[v[pos][1]].flow + 1;
}
int main() {
//freopen("Input.txt","r",stdin);
while(~scanf("%d%d",&n,&m)) {
memset(mp,-1,sizeof(mp));
memset(col,0,sizeof(col));
memset(raw,0,sizeof(raw));
int cnt=0,raw_cnt=0,col_cnt=0;
for(int i=1; i<=n; i++) {
for(int j=1; j<=m; j++) {
string str;
cin>>str;
if(str[0] == '.') {
mp[i][j] = ++cnt;
} else {
mp[i][j] = -1;
if(str[0] != 'X') {
int tmp = (str[0]-'0')*100 + (str[1]-'0')*10 + str[2]-'0';
col[++col_cnt].x = i;
col[col_cnt].y = j;
col[col_cnt].z = tmp;
}
if(str[4] != 'X') {
int tmp = (str[4]-'0')*100 + (str[5]-'0')*10 + str[6]-'0';
raw[++raw_cnt].x = i;
raw[raw_cnt].y = j;
raw[raw_cnt].z = tmp;
}
}
}
}
//printf("%d %d %d\n",raw_cnt,col_cnt,cnt);
int start=0,t=col_cnt+cnt+raw_cnt+2;
for(int i = 1; i <= raw_cnt; i++) {
int x = raw[i].x;
int y = raw[i].y;
int cnt_len = 0;
for(y = y+1; y <= m; y++) {
if(mp[x][y] != -1) {
cnt_len++;
add_Node(i,raw_cnt+mp[x][y],8);
} else break;
}
add_Node(start,i,raw[i].z-cnt_len);
}
for(int i = 1; i <= col_cnt; i++) {
int x = col[i].x;
int y = col[i].y;
int cnt_len = 0;
for(x = x+1; x <= n; x++) {
if(mp[x][y] != -1) {
cnt_len++;
add_Node(raw_cnt + mp[x][y],raw_cnt + cnt + i, 8);
} else break;
}
add_Node(raw_cnt + cnt + i, t, col[i].z-cnt_len);
}
int ans=dinci(start,t);
for(int i=1; i<=n; i++) {
for(int j=1; j<=m; j++) {
if(mp[i][j]==-1)
printf("_");
else
printf("%d",solve(raw_cnt,mp[i][j],cnt));
printf("%c",j==m?'\n':' ');
}
}
for(int i=0; i<=t; i++)
v[i].clear();
e.clear();
}
return 0;
}