Hdu 3338 Kakuro Extension (maximum stream), hdukakuro

Source: Internet
Author: User

Hdu 3338 Kakuro Extension (maximum stream), hdukakuro
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 2s-1 characters. if there are already solutions, you may output any of them.

Sample Input

6 6
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
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 _

A situation where conditions are met for travel and columns and sums. Solution: The biggest stream of amazing questions. Set the super source point to connect to all rows (rows with blank blocks), and the capacity is the sum of the rows. Set a super sink point to connect all columns to it. The capacity is the sum of the columns. Then all the blank blocks are connected to each other. The corresponding rows and columns have a capacity of 8. Here is a question: why is the capacity 8, not 9. A similar problem exists in the query results of the Matrix Decompressing. The reason why the capacity is 8 is to avoid zero stream. When each blank block is reduced by 1, remember to subtract the corresponding values from the rows and columns. After creating the image, you can directly find the maximum stream.
# 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: btypedef 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], ev [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] = f Irst [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 (EV, 0, sizeof (EV); 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] --; // all elements minus one, so each row and each corresponding element must be minus one. Col [map [I] [j]. c] --; // same as above. 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 (! =-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 \ n ", & n, & m) = 2) {init (); input (); dinic (); print ();} return 0 ;}

Copyright Disclaimer: This article is an original article of the blogger. It can be reproduced without the consent of the blogger, but the source must be noted.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.