Kakuro Extension HDU - 3338 (Dinic)
时间:2019-08-06
本文章向大家介绍Kakuro Extension HDU - 3338 (Dinic),主要包括Kakuro Extension HDU - 3338 (Dinic)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
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
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 = 486; const int maxn = 20008; const int maxm = 200086; const int inf = 0x3f3f3f3f; const ll Inf = 999999999999999999; const int mod = 1000000007; const double eps = 1e-6; const double pi = acos(-1); 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=0; Head[v]=cnt++; } int D_vis[maxn],D_num[maxn]; int source,meeting; bool bfs() { memset(D_vis,0,sizeof(D_vis)); for(int i=0;i<=meeting;i++){//注意要覆盖所有点 D_num[i]=Head[i]; } D_vis[source]=1; queue<int>q; q.push(source); int r=0; while(!q.empty()){ int u=q.front(); q.pop(); int k=Head[u]; while(k!=-1){ if(!D_vis[e[k].v]&&e[k].w){ D_vis[e[k].v]=D_vis[u]+1; 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!=-1){ if(D_vis[e[k].v]==D_vis[u]+1&&e[k].w){ int d=dfs(e[k].v,min(f,e[k].w)); if(d>0){ e[k].w-=d; e[k^1].w+=d; return d; } } k=e[k].Next; } return 0; } int Dinic() { int ans=0; while(bfs()){ int f; while((f=dfs(source,inf))>0){ ans+=f; } } return ans; } char s[105][105][10]; int mp1[105][105]; int mp2[105][105]; int mph[105][105]; int mps[105][105]; int mpk[105][105]; int cal(char a,char b,char c){ return (a-48)*100+(b-48)*10+c-48; } int main() { // ios::sync_with_stdio(false); // freopen("in.txt", "r", stdin); int n,m; while (scanf("%d%d",&n,&m)!=EOF){ memset(Head,-1,sizeof(Head)); memset(mp1,0,sizeof(mp1)); memset(mp2,0,sizeof(mp2)); cnt = 0; int cur = 0; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ scanf("%s",s[i][j]); if(s[i][j][0]=='.'){ cur++; mp1[i][j]=cur; cur++; mp2[i][j]=cur; mpk[i][j]=cnt; add_edge(mp1[i][j],mp2[i][j],8); } } } source = 0; meeting = 20000; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if(s[i][j][0]!='.'&&s[i][j][0]!='X'){ cur++; mps[i][j]=cur; int sum = cal(s[i][j][0],s[i][j][1],s[i][j][2]); for(int k=i+1;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][0]!='.'&&s[i][j][4]!='X'){ cur++; mph[i][j]=cur; int sum = cal(s[i][j][4],s[i][j][5],s[i][j][6]); for(int k=j+1;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=1;i<=n;i++){ for(int j=1;j<m;j++){ if(mp1[i][j]==0){ printf("_ "); }else{ printf("%d ",8-e[mpk[i][j]].w+1); } } if(mp1[i][m]==0){ printf("_\n"); }else{ printf("%d\n",8-e[mpk[i][m]].w+1); } } } return 0; }
原文地址:https://www.cnblogs.com/ZGQblogs/p/11312254.html
- [WCF REST] 通过ASP.NET Output Caching实现声明式缓存
- [WCF REST] 通过ASP.NET Output Caching实现声明式缓存
- CentOS6.5下安装JDK
- 使用Maven插件构建SpringBoot项目,生成Docker镜像push到DockerHub上
- 开源API测试工具 Hitchhiker v0.8 - 自动化测试结果统计
- 腾讯AI-JavaAPI示例代码
- C++ 模板学习
- 小程序游戏上线!独家微信官方及专业核心解读!
- 零基础小白成为大数据技术专家必知的学习历程
- Wandelbots重新定义对机器人进行编程的方式
- 结构体字节对齐
- 中国IDC圈、科智咨询王若冰:从供到需——IDC产业的演进之路
- 本周网络空间态势摘要
- C++重要知识点小结---1
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 靶机闯关 DC-8
- 在Java虚拟机上班是一种怎样的体验?
- Linux下远程工作防掉线的一个办法
- Centos安装最新版本cmake
- 【实战】提权某找小姐网站服务器
- 激发态计算中的溶剂效应
- 不会吧,有人用了两年Spring, 居然不知道包扫描是怎么实现的
- 不会吧!都 0202 年了,不会还有不知道 axios 的吧
- 全栈的自我修养: 002使用@vue/cli进行vue环境搭建 (使用Vue,SpringBoot,Flask完成前后端分离)
- JAVA | Java 解决跨域问题 花式解决跨域问题
- 深入理解Java泛型(二)通配符与嵌套
- 用高斯计算磷光发射能
- Android So动态加载 优雅实现与原理分析
- 堆与栈区别
- 在Gaussian16中同时扫描两个反应坐标