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.

题意:给出一个特殊数独,它给出了连续横向空格和连续纵向空格的和,求数独的任意解。

将空格和它所在的连续横向空格组和连续纵向空格组建边,横向和纵向的组分别向超级源点和超级汇点建边,这样就可以跑最大流求出每个点的流量,即是答案了。

 #include<stdio.h>
#include<string.h>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
const int maxm=;
const int INF=0x7fffffff; struct edge{
int from,to,f;
edge(int a,int b,int c):from(a),to(b),f(c){}
}; struct dinic{
int s,t,m;
vector<edge>e;
vector<int>g[maxm];
bool vis[maxm];
int cur[maxm],d[maxm]; void init(int n){
for(int i=;i<=n;i++)g[i].clear();
e.clear();
} void add(int a,int b,int c){
e.push_back(edge(a,b,c));
e.push_back(edge(b,a,));
m=e.size();
g[a].push_back(m-);
g[b].push_back(m-);
} bool bfs(){
memset(vis,,sizeof(vis));
queue<int>q;
q.push(s);
vis[s]=;
d[s]=;
while(!q.empty()){
int u=q.front();
q.pop();
for(int i=;i<g[u].size();i++){
edge tmp=e[g[u][i]];
if(!vis[tmp.to]&&tmp.f>){
d[tmp.to]=d[u]+;
vis[tmp.to]=;
q.push(tmp.to);
}
}
}
return vis[t];
} int dfs(int x,int a){
if(x==t||a==)return a;
int flow=,f;
for(int& i=cur[x];i<g[x].size();i++){
edge& tmp=e[g[x][i]];
if(d[tmp.to]==d[x]+&&tmp.f>){
f=dfs(tmp.to,min(a,tmp.f));
tmp.f-=f;
e[g[x][i]^].f+=f;
flow+=f;
a-=f;
if(a==)break;
}
}
if(flow==)d[x]=-;
return flow;
} void mf(int s,int t){
this->s=s;
this->t=t;
int flow=;
while(bfs()){
memset(cur,,sizeof(cur));
flow+=dfs(s,INF);
}
}
}; char s[][][];
bool vis[][];
char ans[][]; int main(){
int n,m;
while(scanf("%d%d",&n,&m)!=EOF){
int i,j,k;
memset(vis,,sizeof(vis));
dinic d;
d.init(n*m*+);
for(i=;i<=n;i++){
for(j=;j<=m;j++){
scanf("%s",s[i][j]+);
if(s[i][j][]=='.'){
vis[i][j]=;
d.add((i-)*m+j,(i-)*m+j+n*m,);
}
else ans[i][j]='_';
}
}
int t=n*m*+;
for(i=;i<=n;i++){
for(j=;j<=m;j++){
if(!vis[i][j]){
if(s[i][j][]!='X'){
int tmp=(s[i][j][]-'')*+(s[i][j][]-'')*+(s[i][j][]-''),num=(i-)*m+j;
for(k=i+;k<=n&&vis[k][j];k++){
tmp--;
d.add(num,(k-)*m+j,INF);
}
d.add(,num,tmp);
}
if(s[i][j][]!='X'){
int tmp=(s[i][j][]-'')*+(s[i][j][]-'')*+(s[i][j][]-''),num=(i-)*m+j+n*m;
for(k=j+;k<=m&&vis[i][k];k++){
tmp--;
d.add((i-)*m+k+n*m,num,INF);
}
d.add(num,t,tmp);
}
}
}
}
d.mf(,t);
for(i=;i<d.e.size();i++){
if(d.e[i].from<=n*m&&d.e[i].to>n*m){
int x=d.e[i].from/m+,y=d.e[i].from%m;
if(y==){y=m;x--;}
ans[x][y]=-d.e[i].f++'';
}
}
for(i=;i<=n;i++){
for(j=;j<=m;j++){
printf("%c",ans[i][j]);
if(j==m)printf("\n");
else printf(" ");
}
}
}
return ;
}

hdu3338 Kakuro Extension 最大流的更多相关文章

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

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

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

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

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

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

  4. HDU3338:Kakuro Extension(最大流)

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

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

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

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

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

  7. Kakuro Extension【最大流】

    HDU-3338 这道题真的处理起来好复杂啊,题意就是个简单的方格填数问题,但是每个白点至少放1,那么最后的可能解是怎样的呢?我们是不是要把x轴上的和y轴上的统一起来,然后就是每个点都被对应的x和y匹 ...

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

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

  9. Kakuro Extension HDU - 3338 (Dinic)

    Kakuro puzzle is played on a grid of "black" and "white" cells. Apart from the t ...

随机推荐

  1. 平面图转对偶图&19_03_21校内训练 [Everfeel]

    对于每个平面图,都有唯一一个对偶图与之对应.若G‘是平面图G的对偶图,则满足: G'中每一条边的两个节点对应着G中有公共边的面,包括最外部无限大的面. 直观地讲,红色标出来的图就是蓝色标出的图的对偶图 ...

  2. 阿里云服务器上安装mysql的详细步骤

    阿里云安装mysql (1)下载mysql安装包,去mysql官网下载对应的包 mysql数据库官方下载网址:   https://downloads.mysql.com/archives/commu ...

  3. python if elif else判断语句

    username = 'jack' password = ' _username = input('username') _password = input('password') if userna ...

  4. 学习net core的一些疑问?

    所有的内容是否一定都要依赖注入? 获取配置文件的方式是否在类库是获取不到环境变量的? 老出现:InvalidOperationException: Unable to resolve service ...

  5. 重载的方式写Python的get请求

    #encoding=utf-8#__author__="Lanyangyang" import unittestimport requestsimport json # This ...

  6. webpack+vue+vueRouter模块化构建小demo实例超详细步骤(附截图、代码、入门篇)

    说明:本demo使用yarn代替npm指令来下载一系列依赖,有详细指令说明:使用WebStorm下Terminal来输入指令: >开始(确认已经安装node环境和yarn包管理工具) 1.新建项 ...

  7. PHP ueditor编辑器使用(TP5)

    百度搜索ueditor,下载,解压,把需要的JS文件放到对应的目录里,根据框架的不同目录也不一样 在文件中引入需要的JS文件 HTML代码 <div> <!-- 编辑器 --> ...

  8. 用Java批量重命名文件

    要事先在C盘里创建“照片”和“照片1”两个文件夹 1import java.io.*; public class Jack { public static void main(String[] arg ...

  9. Python eval,exac,compile

    # eval 是把字符串类型的数据作为代码进行执行 s = "18+2" ret = eval(s) # 执行字符串类型的代码 print(ret) code = input(&q ...

  10. js常用到的方法积累

    //获取对象长度的方法 function countObjLen(obj) { var count = 0; for (var property in obj) { if (Object.protot ...