hdu 3338 Kakuro Extension
Description
If you solved problem like this, forget it.Because you need to use a completely different algorithm to solve the following one.
Kakuro puzzle is played on a grid of “black” and “white” cells. Apart from the top row and leftmost column which are entirely black, the grid has some amount of white cells which form “runs” and some amount of black cells. “Run” is a vertical or horizontal maximal one-lined block of adjacent white cells. Each row and column of the puzzle can contain more than one “run”. Every white cell belongs to exactly two runs ― one horizontal and one vertical run. Each horizontal “run” always has a number in the black half-cell to its immediate left, and each vertical “run” always has a number in the black half-cell immediately above it. These numbers are located in “black” cells and are called “clues”.The rules of the puzzle are simple:
1.place a single digit from 1 to 9 in each “white” cell
2.for all runs, the sum of all digits in a “run” must match the clue associated with the “run”
Given the grid, your task is to find a solution for the puzzle.
Picture of the first sample input
Picture of the first sample output
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
_ _ 7 9 _ _
_ 1 9 9 1 1 8 6
_ 1 3 9 9 9 3 9
_ 6 7 2 4 9 2 _
题目大意:给出行和和列和,要求求出满足条件的一种情况。
解题思路:很神奇的一题最大流。设置超级源点,连向所有的行(拥有空白块的行),容量为该行的和。设置超级汇点,使所有的列连向它,容量为该列的和。然后所有的空白块,都连向,他所对应的行和列,容量为8。这里有一个问题,为什么容量是8,不是9。在uva 11082 Matrix Decompressing中也有类似的问题。之所以容量为8,是为了避免零流。在每一空白块都减1的情况下,行和和列和也要记得减去相应的值。图建完了,就直接求最大流。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
using namespace std;
const int N = 10010;
const int M = 100100;
const int INF = 0x3f3f3f3f;
const int PO = 105;
#define MIN(a, b) a < b ? a : b
typedef long long ll;
struct Node{
int r, c;
}map[PO][PO];
int n, m, s, t;
int row[N], col[N], rec[PO][PO];
char ch[10];
int ec, head[N], first[N], que[N], lev[N];
int Next[M], to[M], v[M];
void init() {
ec = 0;
memset(first, -1, sizeof(first));
memset(row, 0, sizeof(row));
memset(col, 0, sizeof(col));
memset(rec, -1, sizeof(rec));
}
void addEdge(int a,int b,int c) {
to[ec] = b;
v[ec] = c;
Next[ec] = first[a];
first[a] = ec++;
to[ec] = a;
v[ec] = 0;
Next[ec] = first[b];
first[b] = ec++;
}
int BFS() {
int kid, now, f = 0, r = 1, i;
memset(lev, 0, sizeof(lev));
que[0] = s, lev[s] = 1;
while (f < r) {
now = que[f++];
for (i = first[now]; i != -1; i = Next[i]) {
kid = to[i];
if (!lev[kid] && v[i]) {
lev[kid] = lev[now] + 1;
if (kid == t) return 1;
que[r++] = kid;
}
}
}
return 0;
}
int DFS(int now, int sum) {
int kid, flow, rt = 0;
if (now == t) return sum;
for (int i = head[now]; i != -1 && rt < sum; i = Next[i]) {
head[now] = i;
kid = to[i];
if (lev[kid] == lev[now] + 1 && v[i]) {
flow = DFS(kid, MIN(sum - rt, v[i]));
if (flow) {
v[i] -= flow;
v[i^1] += flow;
rt += flow;
} else lev[kid] = -1;
}
}
return rt;
}
int dinic() {
int ans = 0;
while (BFS()) {
for (int i = 0; i <= t; i++) {
head[i] = first[i];
}
ans += DFS(s, INF);
}
return ans;
}
int getNum(char *s,int l,int r) {
if(s[l] == ‘X‘) return -1;
return 100 * (s[l] - ‘0‘) + 10 * (s[l + 1] - ‘0‘) + s[l + 2] - ‘0‘;
}
void input() {
int rn = 0, cn = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
scanf("%s", ch);
if (ch[3] == ‘X‘) continue;
if (ch[0] == ‘.‘) {
map[i][j].r = map[i][j - 1].r;
map[i][j].c = map[i - 1][j].c;
row[map[i][j].r]--; //所有元素减一,所以行的和每对应一个元素,就要减一。
col[map[i][j].c]--; //同上。
rec[i][j] = 0;
} else {
int a = getNum(ch, 0, 2), b = getNum(ch, 4, 6);
if (b != -1) row[++rn] = b, map[i][j].r = rn;
if (a != -1) col[++cn] = a, map[i][j].c = cn;
}
}
}
s = 0, t = rn + cn + 1;
for (int i = 1; i <= rn; i++) addEdge(s, i, row[i]);
for (int i = 1; i <= cn; i++) addEdge(i + rn, t, col[i]);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (rec[i][j] != -1) {
addEdge(map[i][j].r, map[i][j].c + rn, 8);
rec[i][j] = ec - 2;
}
}
}
}
void print() {
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if(rec[i][j] == -1) printf("_");
else {
int id = rec[i][j];
printf("%d", 9 - v[id]);
}
if(j != m - 1) printf(" ");
} puts("");
}
}
int main() {
while (scanf("%d %d\n", &n, &m) == 2) {
init();
input();
dinic();
print();
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许也可以转载,不过要注明出处哦。