2014
03-16

Kakuro Extension

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

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.

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.

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

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

1. 构造源点S，汇点T

2. 有行和的格子，此类节点设为A

3. 空白格，设为B

4. 有列和的格子，设为C

1. <S, A> 容量和行和

2. <A, B> 容量为8

3. <B, C> 容量为8

4. <C, T> 容量为列和

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define INF 0x7fffffff
#define maxn 20010
#define maxm 500000
using namespace std;
int v[maxm],next[maxm],w[maxm];
int first[maxn],d[maxn],work[maxn],q[maxn];
int e,S,T;
int map[105][105];
struct Row{
int x,y,z;
}row[maxn];
struct Col{
int x,y,z;
}col[maxn];

void init(){
e = 0;
memset(first,-1,sizeof(first));
}

v[e] = b;w[e] = c;next[e] = first[a];first[a] = e++;
v[e] = a;w[e] = 0;next[e] = first[b];first[b] = e++;
}

int bfs(){
int rear = 0;
memset(d,-1,sizeof(d));
d[S] = 0;q[rear++] = S;
for(int i = 0;i < rear;i++){
for(int j = first[q[i]];j != -1;j = next[j])
if(w[j] && d[v[j]] == -1){
d[v[j]] = d[q[i]] + 1;
q[rear++] = v[j];
if(v[j] == T)   return 1;
}
}
return 0;
}

int dfs(int cur,int a){
if(cur == T)    return a;
for(int &i = work[cur];i != -1;i = next[i]){
if(w[i] && d[v[i]] == d[cur] + 1)
if(int t = dfs(v[i],min(a,w[i]))){
w[i] -= t;w[i^1] += t;
return t;
}
}
return 0;
}

int dinic(){
int ans = 0;
while(bfs()){
memcpy(work,first,sizeof(first));
while(int t = dfs(S,INF))   ans += t;
}
return ans;
}

int cal(int id,int row_cnt){
int cnt = 0,pos = id+row_cnt;
for(int i = first[pos];i != -1;i = next[i])
if(v[i] <= row_cnt) cnt += w[i];
return cnt+1;
}

int main()
{
int n,m;
char str[10];
while(scanf("%d%d",&n,&m) == 2){
init();
int row_cnt,col_cnt,id;
row_cnt = col_cnt = id = 0;
for(int i = 1;i <= n;i++){
for(int j = 1;j <= m;j++){
scanf("%s",str);
if(str[0] == '.'){
map[i][j] = ++id;
}else{
map[i][j] = -1;
if(str[0] != 'X'){
int tmp = (str[0]-'0')*100 + (str[1]-'0')*10 + str[2]-'0';
col[++col_cnt].x = i;
col[col_cnt].y = j;
col[col_cnt].z = tmp;
}
if(str[4] != 'X'){
int tmp = (str[4]-'0')*100 + (str[5]-'0')*10 + str[6]-'0';
row[++row_cnt].x = i;
row[row_cnt].y = j;
row[row_cnt].z = tmp;
}
}
}
}
S = 0,T = row_cnt+id+col_cnt+1;
for(int i = 1;i <= row_cnt;i++){
int x = row[i].x;
int y = row[i].y;
int cnt_len = 0;
for(y = y+1;y <= m;y++){
if(map[x][y] != -1){
cnt_len++;
}
else   break;
}
}
for(int i = 1;i <= col_cnt;i++){
int x = col[i].x;
int y = col[i].y;
int cnt_len = 0;
for(x = x+1;x <= n;x++){
if(map[x][y] != -1){
cnt_len++;
}
else    break;
}
}
dinic();
for(int i = 1;i <= n;i++){
for(int j = 1;j <= m;j++){
if(map[i][j] == -1) printf("_ ");
else    printf("%d ",cal(map[i][j],row_cnt));
}
printf("\n");
}
}
return 0;
}

View Code

1. 很高兴你会喜欢这个网站。目前还没有一个开发团队，网站是我一个人在维护，都是用的开源系统，也没有太多需要开发的部分，主要是内容整理。非常感谢你的关注。