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

InputThe 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.OutputPrint 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 _ 题意:
给定横着的和和竖着的和,输出可行解.
思路:
将横着的限制看成一个点,竖着的限制看成一个点,白色方块在中间即可.
白块限制流量1~9,本来应该是上下界网络流,但是因为每一条的边的下界是一样的,所以通过减一处理即可转换为最大流.
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime> #define fuck(x) cerr<<#x<<" = "<<x<<endl;
#define debug(a, x) cerr<<#a<<"["<<x<<"] = "<<a[x]<<endl;
#define ls (t<<1)
#define rs ((t<<1)|1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int loveisblue = ;
const int maxn = ;
const int maxm = ;
const int inf = 0x3f3f3f3f;
const ll Inf = ;
const int mod = ;
const double eps = 1e-;
const double pi = acos(-); int Head[maxn],cnt;
struct edge{
int Next,v,w;
}e[maxm];
void add_edge(int u,int v,int w){
// cout<<u<<" "<<v<<" "<<w<<endl;
e[cnt].Next=Head[u];
e[cnt].v=v;
e[cnt].w=w;
Head[u]=cnt++; e[cnt].Next=Head[v];
e[cnt].v=u;
e[cnt].w=;
Head[v]=cnt++;
} int D_vis[maxn],D_num[maxn];
int source,meeting;
bool bfs()
{ memset(D_vis,,sizeof(D_vis));
for(int i=;i<=meeting;i++){//注意要覆盖所有点
D_num[i]=Head[i];
}
D_vis[source]=;
queue<int>q;
q.push(source);
int r=;
while(!q.empty()){
int u=q.front();
q.pop();
int k=Head[u];
while(k!=-){
if(!D_vis[e[k].v]&&e[k].w){
D_vis[e[k].v]=D_vis[u]+;
q.push(e[k].v);
}
k=e[k].Next;
}
}
// fuck(meeting)
return D_vis[meeting];
}
int dfs(int u,int f)
{
if(u==meeting){return f;}
int &k=D_num[u];
while(k!=-){
if(D_vis[e[k].v]==D_vis[u]+&&e[k].w){
int d=dfs(e[k].v,min(f,e[k].w));
if(d>){
e[k].w-=d;
e[k^].w+=d;
return d;
}
}
k=e[k].Next;
}
return ;
}
int Dinic()
{
int ans=;
while(bfs()){
int f;
while((f=dfs(source,inf))>){
ans+=f;
}
}
return ans;
} char s[][][];
int mp1[][];
int mp2[][];
int mph[][];
int mps[][];
int mpk[][];
int cal(char a,char b,char c){
return (a-)*+(b-)*+c-;
} int main() {
// ios::sync_with_stdio(false);
// freopen("in.txt", "r", stdin); int n,m;
while (scanf("%d%d",&n,&m)!=EOF){
memset(Head,-,sizeof(Head));
memset(mp1,,sizeof(mp1));
memset(mp2,,sizeof(mp2));
cnt = ;
int cur = ;
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
scanf("%s",s[i][j]);
if(s[i][j][]=='.'){
cur++;
mp1[i][j]=cur;
cur++;
mp2[i][j]=cur;
mpk[i][j]=cnt;
add_edge(mp1[i][j],mp2[i][j],);
}
}
}
source = ;
meeting = ; for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
if(s[i][j][]!='.'&&s[i][j][]!='X'){
cur++;
mps[i][j]=cur;
int sum = cal(s[i][j][],s[i][j][],s[i][j][]);
for(int k=i+;k<=n;k++){
if(!mp1[k][j]){ break;}
add_edge(cur,mp1[k][j],inf);
sum--;
}
add_edge(source,cur,sum);
}if(s[i][j][]!='.'&&s[i][j][]!='X'){
cur++;
mph[i][j]=cur;
int sum = cal(s[i][j][],s[i][j][],s[i][j][]);
for(int k=j+;k<=m;k++){
if(!mp2[i][k]){ break;}
add_edge(mp2[i][k],cur,inf);
sum--;
}
add_edge(cur,meeting,sum);
}
}
}
int ans = Dinic();
// fuck(ans)
// fuck("????")
for(int i=;i<=n;i++){
for(int j=;j<m;j++){
if(mp1[i][j]==){
printf("_ ");
}else{
printf("%d ",-e[mpk[i][j]].w+);
}
}
if(mp1[i][m]==){
printf("_\n");
}else{
printf("%d\n",-e[mpk[i][m]].w+);
}
} } return ;
}

Kakuro Extension HDU - 3338 (Dinic)的更多相关文章

  1. L - Kakuro Extension - HDU 3338 - (最大流)

    题意:有一个填数字的游戏,需要你为白色的块内填一些值,不过不能随意填的,是有一些规则的(废话),在空白的上方和作方给出一些值,如果左下角有值说明下面列的和等于这个值,右上角的值等于这行后面的数的和,如 ...

  2. HDU 3338 Kakuro Extension (网络流,最大流)

    HDU 3338 Kakuro Extension (网络流,最大流) Description If you solved problem like this, forget it.Because y ...

  3. HDU3338:Kakuro Extension(最大流)

    Kakuro Extension Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. hdu 3338 最大流 ****

    题意: 黑格子右上代表该行的和,左下代表该列下的和 链接:点我 这题可以用网络流做.以空白格为节点,假设流是从左流入,从上流出的,流入的容量为行和,流出来容量为列和,其余容量不变.求满足的最大流.由于 ...

  5. HDU3338 Kakuro Extension —— 最大流、方格填数类似数独

    题目链接:https://vjudge.net/problem/HDU-3338 Kakuro Extension Time Limit: 2000/1000 MS (Java/Others)     ...

  6. HDU 3338:Kakuro Extension(脑洞大开的网络流)

    http://acm.hdu.edu.cn/showproblem.php?pid=3338 题意:在一个n*m的地图里面,有黑方块和白方块,黑方块可能是“XXXXXXX”或者“YYY/YYY”,这里 ...

  7. HDU 3338 Kakuro Extension

    网络最大流 TLE了两天的题目.80次Submit才AC,发现是刘汝佳白书的Dinic代码还可以优化.....瞬间无语..... #include<cstdio> #include< ...

  8. HDU - 3338 Kakuro Extension (最大流求解方格填数)

    题意:给一个方格,每行每列都有对白色格子中的数之和的要求.每个格子中的数范围在[1,9]中.现在给出了这些要求,求满足条件的解. 分析:本题读入和建图比较恶心... 用网络流求解.建立源点S和汇点T, ...

  9. 【最大流】【HDU3338】【Kakuro Extension】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3338 题目大意:填数字,使白色区域的值得和等于有值得黑色区域的相对应的值,用网络流来做 题目思路:增加 ...

随机推荐

  1. 阿里云应用高可用 AHAS 正式商用,可一键提升云上应用可用性

    在分布式架构环境下,服务间的依赖日益复杂,可能没有人能说清单个故障对整个系统的影响,构建一个高可用的分布式系统面临着很大挑战. 7月17日,阿里云应用高可用服务AHAS 正式商用,包含架构感知.流控降 ...

  2. SPARK-SQL内置函数之字符串函数

    转载请注明转自:http://www.cnblogs.com/feiyumo/p/8763186.html 1.concat对于字符串进行拼接 concat(str1, str2, ..., strN ...

  3. 杨柳目-杨柳科-Info-新闻:让中国人焦虑的杨絮背后,隐藏着“拯救”北京的秘密!

    ylbtech-杨柳目-杨柳科-Info-新闻:让中国人焦虑的杨絮背后,隐藏着“拯救”北京的秘密! 1.返回顶部 1. 春天来了,北京和其他很多城市满城飞絮的日子也到了.库叔作为敏感体质,不得不戴上口 ...

  4. Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第四章:Direct 3D初始化

    原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第四章:Direct 3D初始化 学习目标 对Direct 3D编程在 ...

  5. 在 Linux Mint 19 上安装 zsh 和设置小键盘一步到位

    在 Linux Mint 19 上安装 zsh 和设置小键盘 安装 zsh 并设置 zsh 为默认 shell 安装 sudo apt install zsh 设置 zsh 为默认 shell,注意没 ...

  6. Java练习 SDUT-2670_3-1 Point类的构造函数

    3-1 Point类的构造函数 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 通过本题目的练习可以掌握类的构造函数的定 ...

  7. JavaScript 错误

    try 语句测试代码块的错误. catch 语句处理错误. throw 语句创建自定义错误. JavaScript 错误 当 JavaScript 引擎执行 JavaScript 代码时,会发生各种错 ...

  8. 洛谷P1060 开心的金明

    //01背包 价值等于重要度乘体积 #include<bits/stdc++.h> using namespace std; ; ; int n,m,v[maxn],w[maxn],f[m ...

  9. OracleSpatial函数实例

    Oracle Spatial操作geometry方法   Oracle Spatial中SDO_GEOMETRY类型: CREATE TYPE SDO_GEOMETRY AS OBJECT( SDO_ ...

  10. [ZJOI2007] 小Q的矩阵游戏 (模板—Dinic)

    B. 矩阵游戏 题目描述 小Q是一个非常聪明的孩子,除了国际象棋,他还很喜欢玩一个电脑益智游戏——矩阵游戏.矩阵游戏在一个N*N黑白方阵进行(如同国际象棋一般,只是颜色是随意的).每次可以对该矩阵进行 ...