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. Uva 11520 - Fill the Square 贪心 难度: 0

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  2. WPS处理个人信息一种方法

    下面是WPS处理个人信息的方法,具体步骤如下: 第一步:任意打开一个文件: 第二步:点击左上角WPS的小三角,找到工具——选项,如图: 第三步:进入选项后,点击用户信息: 第四步:修改个人信息,用户名 ...

  3. 归并排序(Merging Sort)

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  4. Cracking The Coding Interview 4.1

    //Implement a function to check if a tree is balanced. For the purposes of this question, a balanced ...

  5. [ZJOI2008]泡泡堂BNB

    这个题...是一道神奇的贪心题... 根据田忌赛马的原理... 先假使两队都符合田忌和齐王的配置... 我们可以发现如果我们用当前最弱的...去和对方当前最强的打... 然后一直按照这个方案...当我 ...

  6. matlab运行中出现“Caught "std::exception" Exception message is: Message Catalog MATLAB:builtins was not loaded from the file."

    在我运行过程中,经常爆出这一不确定是什么的问题,经排查后发现,原来是fopen 文件后,没有及时fclose导致的.

  7. UVALive - 6434 (贪心)

    题目链接:https://vjudge.net/problem/UVALive-6434 题意:给你n个数字,要你把这n个数字分成m组,每一组的消耗值定义为改组最大值和最小值之差,要求这m组的消耗值总 ...

  8. 火狐下,td 的 bug;

    想实现类似的效果,看代码, <div style="width:488px;float:left; margin:-52px 0px 15px 15px;"> < ...

  9. loader 的理解

    [ webpack3.0.0刚刚出来  所以文章是跟着低版本 教程 操作熟悉  结果好多对不上喔] 四:理解less-loader加载器的使用 我们先来理解下less-loader加载器,其他的sas ...

  10. SDK Manager的使用

    前言:SDK Manager就是一个Android软件开发工具包管理器,就像一个桥梁,连通本地和服务器,从服务器下载安卓开发所需工具到本地. 1.在android sdk 安装目录下,有一个SDK M ...