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 ;
}

最新文章

  1. ArchLinux安装指南
  2. Python on VS Code
  3. java.lang.String 类的所有方法
  4. Mingyang.net:为什么不将Bean定义在Action参数中?
  5. android数独游戏
  6. [转] gc tips(2)
  7. 【暑假】[实用数据结构]UVAlive 4670 Dominating Patterns
  8. Array.prototype.map()详解
  9. Exploring TCP state machine by graphs
  10. JavaSE_ 多线程 总目录(23~24)
  11. HMM 前向后向算法(转)
  12. 使用SpringSecurity3用户验证(异常信息,验证码)
  13. STM32/GD32芯片信息
  14. odoo10.0在odoo12.0环境的基础上搭建环境
  15. python文件读书笔记
  16. (贪心部分背包问题)Saving HDU HDU2111
  17. Gitlab 备份迁移恢复报错gtar: .: Cannot mkdir: No such file or directory
  18. 几种 WebP 动态图制作方法
  19. BIO、NIO实战
  20. toFixed方法的bug

热门文章

  1. 利用FFMPEG命令进行文件分割
  2. PropertiesUtil 获取文件属性值
  3. CString、string、const char*的相互转换
  4. EEPROM读写学习笔记与I2C总线(转)
  5. 第三节 java 函数
  6. struts2应用
  7. python+requests+excel 接口自动化框架
  8. 点击图片或者鼠标放上hover .图片变大. 1)可以使用css中的transition, transform 2) 预先设置一个 弹出div. 3)弹出层 alert ; 4) 浏览器的宽度document.documentElement.clientWidth || document.body.clientWidth
  9. sql order by 结合case when then
  10. Python 时间