题目传送门: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. mysql 临时表 heap_MySQL内存表-临时表
  2. Java之Object类与instanceof关键字
  3. Guide: Solr performance tuning--转载
  4. DPDK内存篇(二): 深入学习 IOVA
  5. 世界上第一台计算机国际象棋大师是如何诞生的?
  6. 用concat批量生成MySQL查询语句
  7. linux为mysql创建gpower_系统运维|RHEL/CentOS 7中安装并配置 PowerDNS 和 PowerAdmin
  8. Oracle表空间、段、区和块
  9. hyper-v无法连接本地计算机,如何在Hyper-V虚拟机中访问本地和USB硬盘驱动器
  10. 高效非支配排序ENS python版
  11. 2021-08-11BUU-CTF:[WUSTCTF2020]alison_likes_jojo
  12. 【Python】【pygame】更逼真的星星、连绵细雨
  13. 喧喧发布 2.5.3 版本,主要提升系统稳定性,优化交互体验
  14. 多元线性回归分析spss结果解读_SPSS--回归-多元线性回归模型案例解析
  15. SpringBoot系列之@PropertySource读取yaml文件
  16. spring boot的启动与配置
  17. 「极客中心」www.geekzl.com网站上线了
  18. 锐捷、ruijie、校园网 认证
  19. 图片压缩算法,保证图片不失真
  20. autoware的icp_maching解读

热门文章

  1. springboot+ssm+vue前后端分离项目-宠物之家
  2. C++和java的异同点总结
  3. C - Matrix Reducing
  4. 利用Windows内置工具测试硬盘速度
  5. 计算机网络教室财产登记表,固定资产清查登记表-资产管理.DOC
  6. Windows 无线上外网并通过共享让其它电脑用有线连接后也能上外网
  7. 扩增子图表解读4曼哈顿图:差异OTU或Taxonomy
  8. oracle remap_tablespace,oracle使用remap_tablespace修改用户数据所在的表空间
  9. 自学平面设计,不能不知道的基础知识点是什么?
  10. STM32学习笔记---触摸屏