hdu3338 Kakuro Extension 最大流
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 最大流的更多相关文章
- HDU3338 Kakuro Extension —— 最大流、方格填数类似数独
题目链接:https://vjudge.net/problem/HDU-3338 Kakuro Extension Time Limit: 2000/1000 MS (Java/Others) ...
- HDU3338 Kakuro Extension(最大流+思维构图)
这道题一定要写一下,卡了好久. 题意: 有黑白两种方格,最上边一行和最左边一列一定是黑色,然后其余的地方有可能是黑色,有可能是白色,和白色相邻的黑色方格里有数字(1个或2个), 现在要求在白色方格里填 ...
- HDU - 3338 Kakuro Extension (最大流求解方格填数)
题意:给一个方格,每行每列都有对白色格子中的数之和的要求.每个格子中的数范围在[1,9]中.现在给出了这些要求,求满足条件的解. 分析:本题读入和建图比较恶心... 用网络流求解.建立源点S和汇点T, ...
- HDU3338:Kakuro Extension(最大流)
Kakuro Extension Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 3338 Kakuro Extension (网络流,最大流)
HDU 3338 Kakuro Extension (网络流,最大流) Description If you solved problem like this, forget it.Because y ...
- 【最大流】【HDU3338】【Kakuro Extension】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3338 题目大意:填数字,使白色区域的值得和等于有值得黑色区域的相对应的值,用网络流来做 题目思路:增加 ...
- Kakuro Extension【最大流】
HDU-3338 这道题真的处理起来好复杂啊,题意就是个简单的方格填数问题,但是每个白点至少放1,那么最后的可能解是怎样的呢?我们是不是要把x轴上的和y轴上的统一起来,然后就是每个点都被对应的x和y匹 ...
- L - Kakuro Extension - HDU 3338 - (最大流)
题意:有一个填数字的游戏,需要你为白色的块内填一些值,不过不能随意填的,是有一些规则的(废话),在空白的上方和作方给出一些值,如果左下角有值说明下面列的和等于这个值,右上角的值等于这行后面的数的和,如 ...
- Kakuro Extension HDU - 3338 (Dinic)
Kakuro puzzle is played on a grid of "black" and "white" cells. Apart from the t ...
随机推荐
- windows 文件/文件夹操作
move命令 命令作用:移动某个文件到指定的文件夹下 将D:\file\abc.zip 移动到 E:\test\文件夹下 move d:\file\abc.zip e:\test\ 移动之后再原来的文 ...
- Win10系列:VC++媒体播放控制2
(3)停止视频播放 接下来添加对视频文件播放的停止控制,打开MainPage.xaml文件,并在Grid元素中添加一个"停止"按钮,用于停止视频的播放,代码如下所示: <Bu ...
- day05_python_1124
---恢复内容开始--- 改l1[1:4:2]=[1:32] 对于增加个数 列表按切片取就是列表 ---恢复内容结束--- 01 昨日内容回顾 list: 增: append insert(ind ...
- 微信授权(Net Mvc)
项目结构 WeiXinController.cs using System; using System.Collections.Generic; using System.Linq; using Sy ...
- bzoj1717
题解: 二分答案 然后hash 代码: #include<bits/stdc++.h> using namespace std; ,P2=,P=; int a1[P],num[P],a2[ ...
- 51nod1339飞行任务
首先按照收获从大到小排序. 然后01背包取或者不取即可. 至于为什么这样对的其实我也不知道.... 代码: #include<bits/stdc++.h> using namespace ...
- nginx:负载均衡(三)分流策略
[1]轮询策略.轮询策略是最简单的策略,无脑配置,不考虑服务器的访问的能力.每个请求按照时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除 upstream www.daysn. ...
- Python Django 之 静态文件存放设置
一.静态文件存放路径设置STATICFILES_DIRS 1.在django项目目录下面新建静态文件保存目录 2.在setting中添加相应寻找静态文件目录的配置 STATICFILES_DIRS=( ...
- 深入理解Connector
上一篇主要是从各个容器的生命周期的角度讲了一下整个tomcat的运行流程,说明了各个容器之间的调用关系.但并没有太过详细的说明每一个组件并区分他们. 下面从功能的角度上详细的分析一下connector ...
- hdu-2063-过山车(匈牙利算法)
过山车 Problem Description RPG girls今天和大家一起去游乐场玩,终于可以坐上梦寐以求的过山车了.可是,过山车的每一排只有两个座位,而且还有条不成文的规矩,就是每个女生必须找 ...