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 ...
随机推荐
- border_mode
如果border_mode选择为same,那么卷积操作的输入和输出尺寸会保持一致.如果选择valid,那卷积过后,尺寸会变小 # apply a 3x3 convolution with 64 out ...
- FFT理解
*连续时间-周期性信号频谱 clc;clear;close all N = input('N= '); T = 0.05; n = 1:N; %原始数据输入 D = 2*pi/(N*T); %计算分 ...
- 接口测试-webservice接口---soapui
1.添加项目 2.填入wsdl地址 3.编辑参数,运行接口
- 压缩后的数据 要经过 base64_encode 后才能在网络上传送
function ob_gzip($content) // $content 就是要压缩的页面内容{ if(!headers_sent() && // 如果页面头部信息还没有输出 ex ...
- python2.6.6 升级 2.7.X
下载包 解压 cd 进入 ./configure && make all && make install && make clean && ...
- vue 之 Virtual Dom
什么是Virtual Dom Virtual Dom可以看做一棵模拟了DOM树的JavaScript树,其主要是通过vnode,实现一个无状态的组件,当组件状态发生更新时,然后触发Virtual Do ...
- 设计精美Power BI报告的五大秘诀
众所周知,Power BI可以帮助您创建交互式且信息丰富的报告,但使用Power BI 制作精美而实用的报告对我们这群IT人员而言,却是一个巨大的痛苦:但个人觉得不能就此止步,通过不断实践练习,小悦采 ...
- 2.7 多窗口、句柄(handle)
2.7 多窗口.句柄(handle) 前言 有些页面的链接打开后,会重新打开一个窗口,对于这种情况,想在新页面上操作,就得先切换窗口了.获取窗口的唯一标识用句柄表示,所以只需要切换句柄,我们就能在 ...
- 2.5 SeleniumBuilder辅助定位元素
前言对于用火狐浏览器的小伙伴们,你还在为定位元素而烦恼嘛?上古神器Selenium Builder来啦,哪里不会点哪里,妈妈再也不用担心我的定位元素问题啦!(但是也不是万能,基本上都能覆盖到) 2.5 ...
- git get submodule after clone
/********************************************************************************* * git get submodu ...