HDU 3338 Kakuro Extension(最大流)
Kakuro Extension
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 590 Accepted Submission(s): 204
Special Judge
Problem 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.
![](http://acm.hdu.edu.cn/data/images/D5353-1001-2.png)
Picture of the first sample input Picture of the first sample output
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.
![](http://acm.hdu.edu.cn/data/images/D5353-1001-1.png)
![](http://acm.hdu.edu.cn/data/images/D5353-1001-2.png)
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.
.......— "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 _
Author
NotOnlySuccess@HDU
Source
Recommend
lcy
很神奇的最大流的题目。
很有意思。
题目意思就是在n*m的格子中,有黑白两种格子。要在白格子中填入数字1~9
* 每一段横竖连续的白格子的和是知道的。
* 求出一种满足的,保证有解。
* 最大流。
* 按照横竖段进行编号。然后行进列出,构造图形。
*
* 为了保证填入的数字是1~9,所以一开始每个格子减掉了1,相应的流入和流出都减掉。
* 然后格子的边的赋值为8.
* 还有就是要记录下相应边的编号,便于输出结果。
/* * HDU 3338 Kakuro Extension * 题目意思就是在n*m的格子中,有黑白两种格子。要在白格子中填入数字1~9 * 每一段横竖连续的白格子的和是知道的。 * 求出一种满足的,保证有解。 * 最大流。 * 按照横竖段进行编号。然后行进列出,构造图形。 * * 为了保证填入的数字是1~9,所以一开始每个格子减掉了1,相应的流入和流出都减掉。 * 然后格子的边的赋值为8. * 还有就是要记录下相应边的编号,便于输出结果。 * */ #include <iostream> #include <string.h> #include <algorithm> #include <stdio.h> using namespace std; const int MAXN=20010; const int MAXM=200010; const int INF=0x3f3f3f3f; struct Node { int to,next,cap; }edge[MAXM]; int tol; int head[MAXN]; int gap[MAXN],dis[MAXN],pre[MAXN],cur[MAXN]; void init() { tol=0; memset(head,-1,sizeof(head)); } void addedge(int u,int v,int w,int rw=0) { edge[tol].to=v;edge[tol].cap=w;edge[tol].next=head[u];head[u]=tol++; edge[tol].to=u;edge[tol].cap=rw;edge[tol].next=head[v];head[v]=tol++; } int sap(int start,int end,int nodenum) { memset(dis,0,sizeof(dis)); memset(gap,0,sizeof(gap)); memcpy(cur,head,sizeof(head)); int u=pre[start]=start,maxflow=0,aug=-1; gap[0]=nodenum; while(dis[start]<nodenum) { loop: for(int &i=cur[u];i!=-1;i=edge[i].next) { int v=edge[i].to; if(edge[i].cap&&dis[u]==dis[v]+1) { if(aug==-1||aug>edge[i].cap) aug=edge[i].cap; pre[v]=u; u=v; if(v==end) { maxflow+=aug; for(u=pre[u];v!=start;v=u,u=pre[u]) { edge[cur[u]].cap-=aug; edge[cur[u]^1].cap+=aug; } aug=-1; } goto loop; } } int mindis=nodenum; for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].to; if(edge[i].cap&&mindis>dis[v]) { cur[u]=i; mindis=dis[v]; } } if((--gap[dis[u]])==0)break; gap[dis[u]=mindis+1]++; u=pre[u]; } return maxflow; } char str[110][110][10]; int lx[110][110];//存横条的标号 int ly[110][110];//存竖条的标号 int num[20010];//记录lx,ly数组中出现的次数,因为题目要求填入的数字是1~9,所以先全部变1,相应的流要减少 int id[110][110];//相应的边的编号,便于最后统计结果 int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int n,m; while(scanf("%d%d",&n,&m)==2) { for(int i=0;i<n;i++) for(int j=0;j<m;j++) scanf("%s",str[i][j]); init(); int tt=0;//结点标号 memset(lx,0,sizeof(lx)); memset(ly,0,sizeof(ly)); memset(num,0,sizeof(num)); for(int i=0;i<n;i++) for(int j=0;j<m;j++) { if(strcmp(str[i][j],".......")!=0)continue; if(j==0 || lx[i][j-1]==0)lx[i][j]=++tt; else lx[i][j]=lx[i][j-1]; num[lx[i][j]]++; } for(int j=0;j<m;j++) for(int i=0;i<n;i++) { if(strcmp(str[i][j],".......")!=0)continue; if(i==0 || ly[i-1][j]==0)ly[i][j]=++tt; else ly[i][j]=ly[i-1][j]; num[ly[i][j]]++; } int start=0,end=tt+1,nodenum=tt+2; for(int i=0;i<n;i++) for(int j=0;j<m;j++) if(strcmp(str[i][j],".......")==0) { addedge(lx[i][j],ly[i][j],8); id[i][j]=tol-2;//记录下来 } for(int i=0;i<n;i++) for(int j=0;j<m;j++) { if(str[i][j][3]!='\\')continue; if(str[i][j][0]!='X') { int tmp=(str[i][j][0]-'0')*100+(str[i][j][1]-'0')*10+(str[i][j][2]-'0'); if(ly[i+1][j]!=0) addedge(ly[i+1][j],end,tmp-num[ly[i+1][j]]); } if(str[i][j][4]!='X') { int tmp=(str[i][j][4]-'0')*100+(str[i][j][5]-'0')*10+(str[i][j][6]-'0'); if(lx[i][j+1]!=0) addedge(start,lx[i][j+1],tmp-num[lx[i][j+1]]); } } sap(start,end,nodenum); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(j>0)printf(" "); if(strcmp(str[i][j],".......")!=0)printf("_"); else printf("%c",'0'+9-edge[id[i][j]].cap); } printf("\n"); } } return 0; }
人一我百!人十我万!永不放弃~~~怀着自信的心,去追逐梦想