Kakuro ExtensionTime limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 1144 Accepted Submission (s): 404
Special Judge
Problem DescriptionIf 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
Inputthe first line of input contains, integers n and M (2≤n,m≤100)-the number of rows and columns Correspondingl Y. 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.
Outputprint n lines to the output with M cells 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 6XXXXXXX XXXXXXX 028\xxx 017\xxx 028\xxx xxxxxxxxxxxxxx 022\022 ......... ..... 010\xxxxxx\034 ........ ................... Xxx\014 ........ 016\013 ........ xxx\022 ......... ............ Xxxxxxxxxxxxxx xxx\016 ..... ..... XXXXXXX XXXXXXX5 8XXXXXXX 001\xxx 020\xxx 027\xxx 021\xxx 028\xxx 014\xxx 024\xxxxxx\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 _
Author[email protected]
Sourcehdoj Monthly contest–2010.03.06
Test instructions: Enter a n,m, indicating that the table has n rows m column, the number of black lattice in the horizontal and vertical, if a number in the "\" to the right of the number from the current grid horizontal right continuous white lattice number of the sum (the number of white lattice needs to fill in, Range: 1 ~ 9), if a number on the left, The sum of the numbers within the continuous white lattice below the current vertical. So each white lattice has exactly two times, one level, one vertical. The given form fills in the number of white squares always has the answer. Fill in the form of any one of the conditions can be satisfied (the white lattice filled in at least 1).
Analysis: At the beginning of the problem after reading the question, read the online report on the problem, with the network flow solution. The key is to understand how to build the map. Why would think of the network flow to solve the problem, 1: From the horizontal direction, all the horizontal direction of the sum of the white lattice is exactly equal to the vertical direction of the sum of the number of white lattice, total inflow = = Total outflow. 2: Each white lattice happens to be used two times, once is the horizontal is vertical, the edge of the inflow point and the edge of the outflow point are only one edge, the amount of the in is equal, that is, the white lattice filled with the number of children.
Map: Divide the following categories of points.
S: Extreme Source Point
A:: The number of horizontal directions within a black lattice is considered a point.
B: White lattice.
C: The number of vertical directions in a black lattice is considered another point.
T: Ultra-polar Meeting point
Edges in the diagram:
(S, A, capcity): Capcity for the capacity of the edge =a number of points-the number of consecutive white squares to the right of the horizontal (ensures that the number in the white lattice is at least 1)
(A, B, 8): Capacity 8: Ensures that the number in the white lattice is at least 1 (the entry edge of the white lattice point)
(B, C, 8): The same (out of the white lattice point)
(C, T, capcity): Capcity is the number of =c points of the edge-vertical below the number of consecutive white squares
#include <stdio.h> #include <string.h> #include <queue>using namespace std; #define Captype Intconst int MAXN = 100010; Total number of points const int MAXM = 400010; Total number of edges const int INF = 1<<30;struct edg{int to,next; Captype Cap,flow;} Edg[maxm];int Eid,head[maxn];int GAP[MAXN]; The number of points for each distance (or can be considered a height) int DIS[MAXN]; The shortest distance from each point to the end Enode int CUR[MAXN]; Cur[u] indicates that from the U point can flow through the cur[u] number side void init () {eid=0; memset (head,-1,sizeof (Head));} There are three parameters to the edge, 4 parameters void addedg (int u,int v,captype c,captype rc=0) without a forward edge {edg[eid].to=v; edg[eid].next=head[u]; Edg[eid].cap=c; edg[eid].flow=0; head[u]=eid++; Edg[eid].to=u; EDG[EID].NEXT=HEAD[V]; EDG[EID].CAP=RC; edg[eid].flow=0; head[v]=eid++;} Preprocessing Enode points to all points at the shortest distance void BFS (int sNode, int enode) {queue<int>q; memset (Gap,0,sizeof (GAP)); memset (dis,-1,sizeof (dis)); Gap[0]=1; dis[enode]=0; Q.push (Enode); while (!q.empty ()) {int U=q.front (); Q.pop (); for (int i=head[u]; i!=-1; i=edg[i].next) { int v=edg[i].to; if (dis[v]==-1) {dis[v]=dis[u]+1; gap[dis[v]]++; Q.push (v); }}}}int NODE_F[MAXN]; The value that actually flows through each point, that is, the value of the positive flow through the current point, not the reflux int S[MAXN]; Path stack, which is the ID number of the edge captype maxflow_sap (int snode,int enode, int n) {//NOTE: N is the total number of points, including the source point and the meeting point BFS (SNode, Enode); Pre-Enode the shortest distance to all points if (dis[snode]==-1) return 0; Source point to unreachable sink point memcpy (cur,head,sizeof (head)); memset (node_f,0,sizeof (Node_f)); int top=0; Stack top Captype ans=0; Max Stream int U=snode; while (dis[snode]<n) {//determines from SNode point there is no flow to the next adjacent point if (u==enode) {//Find a way to add flow captype min=inf; int inser; for (int i=0; i<top; i++)//To find the maximum amount of traffic Min if (min>edg[s[i]].cap-edg[s[i]].flow) {M) from this stream In=edg[s[i]].cap-edg[s[i]].flow; Inser=i; } for (int i=0; i<top; i++) {edg[s[i]].flow+=min; Edg[s[i]^1].flow-=min;Flow if (edg[s[i]].cap>0) Node_f[edg[s[i]].to]=edg[s[i]].flow of the side that can be recycled; else Node_f[edg[s[i]^1].to]=edg[s[i]^1].flow; } ans+=min; Top=inser; From the flow of this can be added to the flow of the bottleneck in the edge of the upper edge of the flow can be increased, so only from the fault of the traffic bottleneck cutting u=edg[s[top]^1].to; The starting point of the traffic bottleneck edge is continue; } bool flag = FALSE; It is possible to determine whether an int v can flow toward the neighboring point from the U point; for (int i=cur[u]; i!=-1; i=edg[i].next) {v=edg[i].to; if (edg[i].cap-edg[i].flow>0 && dis[u]==dis[v]+1) {flag=true; Cur[u]=i; Break }} if (flag) {s[top++] = Cur[u]; Add an edge u=v; Continue }//If a flow adjacent point is not found above, then the distance from the starting point U (which can also be considered a height) is the minimum distance of +1 int mind= n for the adjacent flow point; for (int i=head[u]; i!=-1; i=edg[i].next) if (edg[i].cap-edg[i].flow>0 && mind>dis[edg[i].to]) { MIND=DIS[EDG[I].TO]; Cur[u]=i; } gap[dis[u]]--; if (gap[dis[u]]==0) return ans; When Dis[u] The point of this distance is gone, it is impossible to find an augmented stream path from the source point//Because there is only one distance from the sink point to the current point, then from the source point to the sink point must pass the current point, but the current point is not able to find Can flow to the point, then the inevitable cut-off dis[u]=mind+1; If a flow adjacent point is found, the distance is the adjacent point distance of +1, or n+1 gap[dis[u]]++ if it is not found; if (U!=snode) u=edg[s[--top]^1].to; Return an Edge} return ans; struct node{char num[10]; int num1,num2; int Id1,id2; The number of the point}mapt[105][105];int main () {int n,m; while (scanf ("%d%d", &n,&m) >0) {init (); int k=1; for (int i=0; i<n; i++) for (int j=0; j<m; J + +) {scanf ("%s", mapt[i][j].num); Char ss[10]; strcpy (Ss,mapt[i][j].num); Mapt[i][j].num1=-1; Mapt[i][j].num2=-1; Mapt[i][j].id1=-1; Mapt[i][j].id2=-1; if (ss[0]== '. ') mapt[i][j].id1=k++; if (ss[0]>= ' 0 ' && ss[0]<= ' 9 ') {mapt[i][j].num1= (ss[0]-' 0 ') *100+ (ss[1]-' 0 ') *10+ss[2]-' 0 '; mapt[i][j].id1=k++; } if (ss[4]>= ' 0 ' &&ss[4]<= ' 9 ') {mapt[i][j].num2= (ss[4]-' 0 ') *100+ (ss[5]-' 0 ') * 10+ss[6]-' 0 '; mapt[i][j].id2=k++; }} int s=0, t = k; for (int i=0; i<n; i++) {int j=0; while (J<m) {while (j<m&&mapt[i][j].num2==-1) j + +; Find a grid if (j>=m) break with value in horizontal direction; int U=mapt[i][j].id2, c=mapt[i][j].num2, tk=0; j + +; while (j<m&&mapt[i][j].num[0]== '. ') {int v=mapt[i][j].id1; ADDEDG (u,v,8); j + +; tk++; } addedg (S,U,C-TK); Remember to reduce the number of empty squares, because each empty lattice value is at least 1} for (int j=0; j<m; J + +) {int i=0; while (I<n) { while (I<n&&mapt[i][j].num1==-1) i++; if (i>=n) break; int V=mapt[i][j].id1, C=MAPT[I][J].NUM1, tk=0; i++; while (i<n&&mapt[i][j].num[0]== '. ') {int u=mapt[i][j].id1; ADDEDG (u,v,8); i++; tk++; } addedg (V,T,C-TK); }} maxflow_sap (s,t,k+1); for (int i=0; i<n; i++) {int J; for (j=0; j<m-1; j + +) if (mapt[i][j].num[0]== '. ') printf ("%d", node_f[mapt[i][j].id1]+1); else printf ("_"); if (mapt[i][j].num[0]== '. ') printf ("%d\n", node_f[mapt[i][j].id1]+1); else printf ("_\n"); } }}
Hdu3338kakuro Extension (maximum flow, ISAP) is the key