题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2883

Kakuro Extension

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1986    Accepted Submission(s): 692
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.
              
        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.

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 _

题目大意:

原数谜是个很有趣的游戏,每一行或每一列空白称为一个回,每一回都对应着一个整数sum,sum就是这回的和。这些空白格里只能填入1—9这九个数字,每一回中可以重复。全黑色的格为空,有数字的格,左下角的表示列的和,右上角的表示行的和,则可以得到第二个图。

分析:

一个数等于若干个数的和,可以看做一条入流分解为若干条出流,入流量等于总的出流量。每个格子(i,j)可以由行和列两个坐标确定,所以可以建立行的点和列的点,代表i行的点向代表j列的点连接一条边(容量范围【1,9】)就是代表(i,j)这个格子。具有行总和的黑色格子,可以表示为流入行点的总流量,具有列总和的黑色格子,可以表示该列流出流量的和。某些行(列)可能有多个约束总和,我们可以将每个总和都看做单独一行(列),所以实际的行数和列数并不一定等于原图的。还要记录每个格子对应那一条边,最后流过那条边的总流量就是要填的数字。由于流量有上下限限制,可以给每个数都减掉1,对应的和也对应减去几,则填出来的数字范围为0—8, 就可以用单纯的网络流搞定了。求出来后再加上1就可以了,这样就没有下界需要处理了。

建图:

一共有四类点:

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<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define PI acos(-1.0)
#define eps 1e-8
#define ll long long
#define MEM(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define MII map<int,int>::iterator
#define MLL map<LL,LL>::iterator
#define pii pair<int,int>
#define SI set<int>::iterator
#define SL set<LL>::iterator
#define dug printf("bug-------bug-------bug\n")
const int maxn = 10005;
const int inf = 0x3f3f3f3f;
struct Edge
{int to, cap, nxt;Edge(){}Edge(int t, int c, int nx):to(t), cap(c), nxt(nx){}
};
int head[maxn], tol;
Edge edge[100*maxn];
int n, m;
int S, T;
struct Val
{int fir, sec;int emp;
};
Val mp[105][105];
int r[105][105], c[105][105], bel[105][105];
int row[maxn], col[maxn];
bool vis[maxn];
void AddEdge(int u, int v, int cap)
{edge[tol] = Edge(v, cap, head[u]);head[u] = tol++;edge[tol] = Edge(u, 0, head[v]);head[v] = tol++;
}
Val turn(char s[])
{Val ret;if(s[0] == 'X' || s[0] == '.')ret.fir = -1;else{int tmp = 0;for(int i = 0; i < 3; i++)tmp = tmp * 10 + s[i] - '0';ret.fir = tmp;}if(s[4] == 'X' || s[4] == '.')ret.sec = -1;else{int tmp = 0;for(int i = 0; i < 3; i++)tmp = tmp * 10 + s[i+4] - '0';ret.sec = tmp;}ret.emp = (s[0] == '.' && s[4] == '.');return ret;
}
void init()
{tol = 0;memset(head, -1, sizeof(head));char s[8];for(int i = 1; i <= n; i++)for(int j = 1; j <= m; j++){scanf("%s", s);mp[i][j] = turn(s);}S = 0, T = n*m + 1;int rowc = 1, colc = 1;int cnt = 1;for(int i = 1; i <= n; i++)for(int j = 1; j <= m; j++){Val cur = mp[i][j];if(cur.emp)continue;if(cur.sec != -1){int ed = j + 1;while(ed <= m && mp[i][ed].emp)r[i][ed] = rowc, ed++;row[rowc] = cnt++;AddEdge(0, row[rowc++], cur.sec - (ed - j - 1));}if(cur.fir != -1){int ed = i + 1;while(ed <= n && mp[ed][j].emp)c[ed][j] = colc, ed++;col[colc] = cnt++;AddEdge(col[colc++], T, cur.fir - (ed - i - 1));}}memset(bel, -1, sizeof(bel));for(int i = 1; i <= n; i++)for(int j = 1; j <= m; j++)if(mp[i][j].emp){AddEdge(row[r[i][j]], col[c[i][j]], 8);bel[i][j] = tol - 2;}
}
int d[maxn];bool BFS()
{queue<int> q;memset(vis, false, sizeof(vis));vis[S] = true;d[S] = 0;q.push(S);while(!q.empty()){int u = q.front();q.pop();for(int i = head[u]; i != -1; i = edge[i].nxt){int v = edge[i].to, c = edge[i].cap;if(!vis[v] && c > 0){vis[v] = true;d[v] = d[u] + 1;q.push(v);}}}return vis[T];
}
int cur[maxn];
int dfs(int u, int T, int a)
{if(u == T || a == 0)return a;int flow = 0, f;for(int &i = cur[u]; i != -1; i = edge[i].nxt){int v = edge[i].to, c = edge[i].cap, r = i^1;if(d[v] == d[u] + 1 && c > 0 && (f = dfs(v, T, min(c, a))) > 0){edge[i].cap -= f;edge[r].cap += f;flow += f;a -= f;if(a == 0)break;}}return flow;
}
int dinic(int S, int T)
{int ret = 0;while(BFS()){for(int i = 0; i <= T; i++)cur[i] = head[i];int f = inf;//while((f = dfs(S, T, f)) > 0)f = dfs(S, T, f);ret += f;}return ret;
}
int ans[maxn][maxn];
int main()
{while(cin >> n >> m){init();int t = dinic(S, T);for(int i = 1; i <= n; i++)for(int j = 1; j <= m; j++){if(bel[i][j] == -1){ans[i][j] = 0;continue;}ans[i][j] = 9 - edge[bel[i][j]].cap;}for(int i = 1; i <= n; i++)for(int j = 1; j <= m; j++){if(!ans[i][j])putchar('_');elseprintf("%d", ans[i][j]);putchar(j == m ? '\n':' ');}}return 0;
}

HDU3338Kakuro Extension(最大流+边的流量)相关推荐

  1. 【精选】表情包斗图小程序(可引流,开通流量主,权益外卖cps,带pc后台管理)

    牛云表情包斗图小程序,流量主变现,外卖cps权益变现,uniCloud云开发无需购买服务器和域名,助力每一位内容创业者. 技术优势 基于 uniapp + uniCloud 研发,无需购买服务器和域名 ...

  2. [精选]万能节日国庆头像小程序(可引流,开通流量主,带pc后台管理)

    前言 牛云万能节日头像小程序,流量主变现,外卖cps权益变现,uniCloud云开发无需购买服务器和域名,助力每一位创业者. 技术优势 基于 uniapp + uniCloud 研发,无需购买服务器和 ...

  3. 微信小程序:2022强大的修复版趣味心理测试小程序源码,趣味测试引流裂变神器,流量主激励广告实现管道收益

    大家还记得以前有一款趣味测嚒? 那款趣味测试可以说在当时是只要当时做小程序的基本是人手一款 不过后来自从腾讯更新小程序登录接口以后,那款小程序也就和接口一起挂了 那么呢现在小编就给大家发布修复过的,修 ...

  4. 微商怎么在手淘引流?把流量瞬间引入指定页面

    微商怎么在手淘引流?把流量瞬间引入指定页面 其实,最有效的引流方式,还是从自己着手,不是多么好的一个引流方式就能为我们带来更好的利益,如果我们迎来了流量,但是我们自身不足够吸引他们的话,不能让消费者满 ...

  5. HDU3338 Kakuro Extension(最大流+思维构图)

    这道题一定要写一下,卡了好久. 题意: 有黑白两种方格,最上边一行和最左边一列一定是黑色,然后其余的地方有可能是黑色,有可能是白色,和白色相邻的黑色方格里有数字(1个或2个), 现在要求在白色方格里填 ...

  6. HDU - 3338 Kakuro Extension(最大流+思维建边)

    题目链接:点击查看 题目大意:填数游戏,给出一个n*m的矩阵,矩阵中存在三种方块: 纯黑的方块:没什么用 纯白的方块:等待我们填数字的方块 黑色方块上有数字: 左下角有数字:当前黑色方块下面的白色方块 ...

  7. 视频号日引流500+精准流量6大玩法,实现微信后端转化变现丨国仁网络资讯

    微信作为一个"国民APP",每一次改动,真的都牵扯着无数人的心. 不过话说回来,视频号作为微信的更新重点之一.如今,视频号几乎已经和微信生态全面打通: 用户也可以从聊天.群聊.朋友 ...

  8. nginx根据ip限流和突发流量配置解释

    前言 前一篇记录了如何使用Nginx代理Vue项目,今天记录如果使用Nginx配置location限流,本篇是Nginx专栏第5篇, 有想学习nginx的可以订阅下该专栏,大家一起讨论,有问题可以留言 ...

  9. 烘焙门店 | 解锁公众号精准引流5W+的流量密码

    以往分享的公众号裂变增长案例大多数都是线上的,今天小编要给大家分享一个关于线上和线下实体店双重引流裂变的一个活动玩法和秘诀. 案例分享之前先来说说什么是任务宝? 通过活动奖品吸引用户,让用户邀请好友助 ...

最新文章

  1. tf.keras.losses.MeanAbsolutePercentageError 损失函数 示例
  2. idea spring 中没有标识_Spring 优雅的国际化实现
  3. keta-custom DWZ validationEng IE 下表单重复提交BUG原因及修复方法
  4. python1.学生管理系统
  5. paper 17 : 机器学习算法思想简单梳理
  6. win7 mysql 密码_笔记本win7系统下mysql忘记密码的最佳解决方法
  7. python位运算符_NumPy按位运算符解析和实例详解
  8. css 根据不同屏幕设置间距_18-CSS问题-让多个div横排显示并设置间距解决方案
  9. Java中变量及数据类型
  10. 安装软件报:The installer has encountered an unexpected error installing this package....此类错误...
  11. 红米Note刷机包 移动版 开发版4.9.23 蝰蛇音效 CRT锁屏动画 最流畅的ROM
  12. 如何使用U盘重装系统?
  13. 启动tomcat时候报错(Error deploying web application directory)
  14. 被遗忘的设计模式——空对象模式(Null Object Pattern)
  15. 基于OpenCV的卡尔曼滤波的设计
  16. 2023最新最新ChatGPT超全面从基础到实战视频教程/有兴趣自己学
  17. 基于卷积神经网络和投票机制的三维模型分类与检索 2019 论文笔记
  18. SCI投稿被退回的常见原因有哪些?
  19. Windows批处理设置自动获取IP及DNS或固定IP及DNS
  20. ifft java_OpenCV DFT_INVERSE与Matlab的ifft不同

热门文章

  1. 智能红外遥控器(三):红外学习温湿度读取
  2. java数组位置_java数组中如何查找元素的位置?
  3. gsm 收发短信 打电话
  4. 2500个常用汉字(用来练普通话的)
  5. linux双显卡配置_linux双显卡配置 linux网络配置
  6. PgSQl 结合 Mybatis 插入 json,及查询,数据库使用 jsonb
  7. 百度直播消息系统的实践和演进
  8. d3 企业图谱 仿天眼查 企查查
  9. Python爬虫 | 图书馆公众号座位自动预约【从0到1】
  10. 20952磁盘存储器的管理