zoukankan      html  css  js  c++  java
  • 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.

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

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

      1 #include<stdio.h>
      2 #include<string.h>
      3 #include<vector>
      4 #include<queue>
      5 #include<algorithm>
      6 using namespace std;
      7 const int maxm=20500;
      8 const int INF=0x7fffffff;
      9 
     10 struct edge{
     11     int from,to,f;
     12     edge(int a,int b,int c):from(a),to(b),f(c){}
     13 };
     14 
     15 struct dinic{
     16     int s,t,m;
     17     vector<edge>e;
     18     vector<int>g[maxm];
     19     bool vis[maxm];
     20     int cur[maxm],d[maxm];
     21 
     22     void init(int n){
     23         for(int i=1;i<=n;i++)g[i].clear();
     24         e.clear();
     25     }
     26 
     27     void add(int a,int b,int c){
     28         e.push_back(edge(a,b,c));
     29         e.push_back(edge(b,a,0));
     30         m=e.size();
     31         g[a].push_back(m-2);
     32         g[b].push_back(m-1);
     33     }
     34 
     35     bool bfs(){
     36         memset(vis,0,sizeof(vis));
     37         queue<int>q;
     38         q.push(s);
     39         vis[s]=1;
     40         d[s]=0;
     41         while(!q.empty()){
     42             int u=q.front();
     43             q.pop();
     44             for(int i=0;i<g[u].size();i++){
     45                 edge tmp=e[g[u][i]];
     46                 if(!vis[tmp.to]&&tmp.f>0){
     47                     d[tmp.to]=d[u]+1;
     48                     vis[tmp.to]=1;
     49                     q.push(tmp.to);
     50                 }
     51             }
     52         }
     53         return vis[t];
     54     }
     55 
     56     int dfs(int x,int a){
     57         if(x==t||a==0)return a;
     58         int flow=0,f;
     59         for(int& i=cur[x];i<g[x].size();i++){
     60             edge& tmp=e[g[x][i]];
     61             if(d[tmp.to]==d[x]+1&&tmp.f>0){
     62                 f=dfs(tmp.to,min(a,tmp.f));
     63                 tmp.f-=f;
     64                 e[g[x][i]^1].f+=f;
     65                 flow+=f;
     66                 a-=f;
     67                 if(a==0)break;
     68             }
     69         }
     70         if(flow==0)d[x]=-1;
     71         return flow;
     72     }
     73 
     74     void mf(int s,int t){
     75         this->s=s;
     76         this->t=t;
     77         int flow=0;
     78         while(bfs()){
     79             memset(cur,0,sizeof(cur));
     80             flow+=dfs(s,INF);
     81         }
     82     }
     83 };
     84 
     85 char s[105][105][10];
     86 bool vis[105][105];
     87 char ans[105][105];
     88 
     89 int main(){
     90     int n,m;
     91     while(scanf("%d%d",&n,&m)!=EOF){
     92         int i,j,k;
     93         memset(vis,0,sizeof(vis));
     94         dinic d;
     95         d.init(n*m*2+10);
     96         for(i=1;i<=n;i++){
     97             for(j=1;j<=m;j++){
     98                 scanf("%s",s[i][j]+1);
     99                 if(s[i][j][1]=='.'){
    100                     vis[i][j]=1;
    101                     d.add((i-1)*m+j,(i-1)*m+j+n*m,8);
    102                 }
    103                 else ans[i][j]='_';
    104             }
    105         }
    106         int t=n*m*2+1;
    107         for(i=1;i<=n;i++){
    108             for(j=1;j<=m;j++){
    109                 if(!vis[i][j]){
    110                     if(s[i][j][1]!='X'){
    111                         int tmp=(s[i][j][1]-'0')*100+(s[i][j][2]-'0')*10+(s[i][j][3]-'0'),num=(i-1)*m+j;
    112                         for(k=i+1;k<=n&&vis[k][j];k++){
    113                             tmp--;
    114                             d.add(num,(k-1)*m+j,INF);
    115                         }
    116                         d.add(0,num,tmp);
    117                     }
    118                     if(s[i][j][5]!='X'){
    119                         int tmp=(s[i][j][5]-'0')*100+(s[i][j][6]-'0')*10+(s[i][j][7]-'0'),num=(i-1)*m+j+n*m;
    120                         for(k=j+1;k<=m&&vis[i][k];k++){
    121                             tmp--;
    122                             d.add((i-1)*m+k+n*m,num,INF);
    123                         }
    124                         d.add(num,t,tmp);
    125                     }
    126                 }
    127             }
    128         }
    129         d.mf(0,t);
    130         for(i=0;i<d.e.size();i++){
    131             if(d.e[i].from<=n*m&&d.e[i].to>n*m){
    132             int x=d.e[i].from/m+1,y=d.e[i].from%m;
    133             if(y==0){y=m;x--;}
    134                 ans[x][y]=8-d.e[i].f+1+'0';
    135             }
    136         }
    137         for(i=1;i<=n;i++){
    138             for(j=1;j<=m;j++){
    139                 printf("%c",ans[i][j]);
    140                 if(j==m)printf("
    ");
    141                 else printf(" ");
    142             }
    143         }
    144     }
    145     return 0;
    146 }
    View Code
  • 相关阅读:
    day09页面的声明周期函数
    day8小程序的运行环境与基本架构
    day09小程序复习
    day08前后端交互
    day07获取图片
    day07获取用户地址信息
    MySQL 主从同步延迟的原因及解决办法
    升级Oracle 19c经验: TTS 在使用datapump导matadata时EXCLUDE=STATISTICS 不启作用
    12c,19c自动kill长时间未活动会话特性
    SuSE11单实例二进制安装MySQL5.7
  • 原文地址:https://www.cnblogs.com/cenariusxz/p/6592550.html
Copyright © 2011-2022 走看看