HDU 3338 Kakuro Extension (maximum flow)

Source: Internet
Author: User

HDU 3338 Kakuro Extension

Description
If you solved problem like this, forget it. Because need to use a completely different algorithm to solve the following one.
Kakuro puzzle is played in a grid of "black" and "white" cells. Apart from the top row and leftmost column which is entirely black, the grid has some amount of white cells which form "R Uns "and some amount of black cells. "Run" is a vertical or horizontal maximal one-lined block of the adjacent white cells. Each row and column of the puzzle can contain more than one "run". Every white cell belongs to exactly, Runs―one horizontal and one vertical run. Each horizontal ' run ' always have a number in the black Half-cell to their immediate left, and each vertical ' run ' always have A number in the black Half-cell immediately above it. These numbers is located in "black" cells and is called "clues". The rules of the puzzle is 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 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 both 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 would never have all white cells. The given grid'll has at least one ' white ' cell. It is guaranteed that the given puzzle have at least one solution.

Output
Print n lines to the output with M cells on 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 the each row consists of 2m-1 characters. If There is many solutions, you could 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 _

The main topic: to travel and and columns, the request to find a situation to meet the conditions. The idea of solving problems: the most amazing flow of a problem. Sets the super source point, connecting to all rows (rows with blank blocks), and the capacity for that row. Set the super sink point so that all the Lielen to it, and the capacity for that column. Then all the blank blocks, all connected, he corresponds to the row and column, the capacity is 8. Here's a question, why the volume is 8, not 9. Similar problems are found in the UVA 11082 Matrix decompressing. The capacity is 8 to avoid 0 streams. In the case of each blank block minus 1, row and column and also remember to subtract the corresponding value. When the diagram is finished, the maximum flow is calculated directly.
#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <cstdlib>using namespace STD;Const intN =10010;Const intM =100100;Const intINF =0x3f3f3f3f;Const intPO = the;#define MIN (A, B) a < b? a:btypedef Long Longllstructnode{intR, C;}Map[PO] [PO];intN, M, S, t;intRow[n], Col[n], Rec[po][po];Charch[Ten];intEC, Head[n], first[n], que[n], lev[n];intNEXT[M], to[m], v[m];voidInit () {EC =0;memset(First,-1,sizeof(first));memset(Row,0,sizeof(row));memset(Col,0,sizeof(col));memset(REC,-1,sizeof(rec));}voidAddedge (intAintBintc) {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++;}intBFS () {intKid, now, F =0, r =1Imemset(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;}intDFS (intNowintSUM) {intKid, flow, RT =0;if(now = = t)returnSum for(inti = 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; }ElseLev[kid] =-1; }               }returnRT;}intDinic () {intAns =0; while(BFS ()) { for(inti =0; I <= t;        i++) {Head[i] = First[i];    } ans + = DFS (s, INF); }returnAns;}intGetnum (Char*s,intLintR) {if(S[l] = =' X ')return-1;return  -* (S[L)-' 0 ') +Ten* (S[l +1] -' 0 ') + S[l +2] -' 0 ';}voidInput () {intRN =0, CN =0; for(inti =0; I < n; i++) { for(intj =0; J < M; J + +) {scanf('%s ', ch);if(ch[3] ==' X ')Continue;if(ch[0] =='. ') {MapI [J].R =MapI [J-1].R;MapI [J].C =MapI1][J].C; row[MapI [j].r]--;//All elements minus one, so the line and each corresponding one element, it is necessary to reduce one. col[MapI [j].c]--;//Ibid. REC[I][J] =0; }Else{intA = Getnum (CH,0,2), B = Getnum (CH,4,6);if(b! =-1) Row[++rn] = B,MapI [J].R = RN;if(A! =-1) COL[++CN] = A,MapI            [J].C = CN; }}} s =0, T = rn + CN +1; for(inti =1; I <= RN; i++) Addedge (S, I, row[i]); for(inti =1; I <= cn; i++) Addedge (i + RN, T, Col[i]); for(inti =0; I < n; i++) { for(intj =0; J < M; J + +) {if(Rec[i][j]! =-1) {Addedge (MapI [J].R,MapI [J].c + RN,8); REC[I][J] = EC-2; }               }    }}voidPrint () { for(inti =0; I < n; i++) { for(intj =0; J < M; J + +) {if(Rec[i][j] = =-1)printf("_");Else{intid = rec[i][j];printf("%d",9-V[id]); }if(J! = M-1)printf(" "); }puts(""); }}intMain () { while(scanf("%d%d\n", &n, &m) = =2) {init ();        Input ();        Dinic ();    Print (); }return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission can also be reproduced, but to indicate the source oh.

Hdu 3338 Kakuro Extension (Max Stream)

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.