hdu 3338 Kakuro Extension(网络流dinic邻接表实现)

Kakuro Extension

Problem Description
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.
         hdu 3338 Kakuro Extension(网络流dinic邻接表实现)_第1张图片       hdu 3338 Kakuro Extension(网络流dinic邻接表实现)_第2张图片
        Picture of the first sample input            Picture of the first sample output
 

Input
The first line of input contains two integers n and m (2 ≤ n,m ≤ 100) — the number of rows and columns correspondingly. Each of the next n lines contains descriptions of m cells. Each cell description is one of the following 7-character strings: .......— "white" cell; XXXXXXX— "black" cell with no clues; AAA\BBB— "black" cell with one or two clues. AAA is either a 3-digit clue for the corresponding vertical run, or XXX if there is no associated vertical run. BBB is either a 3-digit clue for the corresponding horizontal run, or XXX if there is no associated horizontal run. The first row and the first column of the grid will never have any white cells. The given grid will have at least one "white" cell.It is guaranteed that the given puzzle has at least one solution.
 

Output
Print n lines to the output with m cells in each line. For every "black" cell print '_' (underscore), for every "white" cell print the corresponding digit from the solution. Delimit cells with a single space, so that each row consists of 2m-1 characters.If there are many solutions, you may output any of them.
 

Sample Input
   
   
   
   
6 6 XXXXXXX XXXXXXX 028\XXX 017\XXX 028\XXX XXXXXXX XXXXXXX 022\022 ....... ....... ....... 010\XXX XXX\034 ....... ....... ....... ....... ....... XXX\014 ....... ....... 016\013 ....... ....... XXX\022 ....... ....... ....... ....... XXXXXXX XXXXXXX XXX\016 ....... ....... XXXXXXX XXXXXXX 5 8 XXXXXXX 001\XXX 020\XXX 027\XXX 021\XXX 028\XXX 014\XXX 024\XXX XXX\035 ....... ....... ....... ....... ....... ....... ....... XXXXXXX 007\034 ....... ....... ....... ....... ....... ....... XXX\043 ....... ....... ....... ....... ....... ....... ....... XXX\030 ....... ....... ....... ....... ....... ....... XXXXXXX
 

Sample Output
   
   
   
   
_ _ _ _ _ _ _ _ 5 8 9 _ _ 7 6 9 8 4 _ 6 8 _ 7 6 _ 9 2 7 4 _ _ _ 7 9 _ _ _ _ _ _ _ _ _ _ _ 1 9 9 1 1 8 6 _ _ 1 7 7 9 1 9 _ 1 3 9 9 9 3 9 _ 6 7 2 4 9 2 _
 

Author
NotOnlySuccess@HDU
 

Source
HDOJ Monthly Contest – 2010.03.06

s-左-空白-上-t
总共5种点
1.type=0  黑格
2.type=1  空格
3.type=2 有两个数字的格子
4.type=3 只有上边数字的格子
5.type=4 只有左边数字的格子
左进上出
然后建边 虚构源点s,汇点t
1.源点与type24相连,容量为左边的数字-这个格子管辖的空格数*1
2.type24与他管辖的空格相连,容量为8,0-8分别代表数字1-9 这样我们就把填格子转化成了网络流中边的关系
3.空格与管辖他的type23空格相连,容量为8
4.type23与汇点相连,容量为上边的数字-这个格子管辖的空格数*1
然后计算一遍最大流
每个格子上面的数字就是这个数字   8-这个数字上面边的流量+1
#include<cstdio>
#include<cstring>
#include<queue>
#define N 105
#define M 100005
using namespace std;
const int inf=0x7ffffff;
struct node
{
    int type,a,b,id;
}map[N][N];
struct node1
{
    int u,v,w,next;
}edge[M];
int n,m,t,cnt,head[M],dist[M];
void add(int u,int v,int w)
{
    edge[cnt].u=u;
    edge[cnt].v=v;
    edge[cnt].w=w;
    edge[cnt].next=head[u];
    head[u]=cnt++;

    edge[cnt].u=v;
    edge[cnt].v=u;
    edge[cnt].w=0;
    edge[cnt].next=head[v];
    head[v]=cnt++;
}

int bfs()
{
    int i,u,v;
    queue<int>q;
    memset(dist,0,sizeof(dist));
    u=0;
    dist[u]=1;
    q.push(u);
    while(!q.empty())
    {
        u=q.front();
        q.pop();
        for(i=head[u];i!=-1;i=edge[i].next)
        {
            v=edge[i].v;
            if(edge[i].w&&!dist[v])
            {
                dist[v]=dist[u]+1;
                if(v==t)
                    return 1;
                q.push(v);
            }
        }
    }
    return 0;
}
int dfs(int s,int lim)
{
    int i,tmp,v,cost=0;
    if(s==t)
        return lim;
    for(i=head[s];i!=-1;i=edge[i].next)
    {
        v=edge[i].v;
        if(edge[i].w&&dist[s]==dist[v]-1)
        {
            tmp=dfs(v,min(lim-cost,edge[i].w));
            if(tmp>0)
            {
                edge[i].w-=tmp;
                edge[i^1].w+=tmp;
                cost+=tmp;
                if(cost==lim)
                    break;
            }
            else
                dist[v]=-1;
        }
    }
    return cost;
}
int dinic()
{
    int ans=0,s=0;
    while(bfs())
        ans+=dfs(s,inf);
    return ans;
}
void outputgraph()
{
    int g[M];
    memset(g,0,sizeof(g));
    int s=0;
    for(int i=head[s];i!=-1;i=edge[i].next)
    {
        int u=edge[i].v;
        for(int j=head[u];j!=-1;j=edge[j].next)
            g[edge[j].v]=8-edge[j].w+1;
    }
    for(int i=1;i<=n;i++)
    {
        printf("_");
        for(int j=2;j<=m;j++)
        {
            if(map[i][j].type==1)
                printf("% d",g[map[i][j].id]);
            else
                printf(" _");
        }
        printf("\n");
    }
}
int main()
{
    char s[100];
    int tot,num;
    while(~scanf("%d%d",&n,&m))
    {
        memset(head,-1,sizeof(head));
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                scanf("%s",s);
                if(s[0]=='.')
                {
                    map[i][j].type=1;
                    map[i][j].a=0;
                    map[i][j].b=0;
                }
                else if(s[3]==92)
                {
                    if(s[0]>='0'&&s[0]<='9')
                    {
                        if(s[4]>='0'&&s[4]<='9')
                        {
                            map[i][j].type=2;
                            map[i][j].a=(s[0]-'0')*100+(s[1]-'0')*10+(s[2]-'0');
                            map[i][j].b=(s[4]-'0')*100+(s[5]-'0')*10+(s[6]-'0');
                        }
                        else
                        {
                            map[i][j].type=3;
                            map[i][j].a=(s[0]-'0')*100+(s[1]-'0')*10+(s[2]-'0');
                        }
                    }
                    else
                    {
                        map[i][j].type=4;
                        map[i][j].a=(s[4]-'0')*100+(s[5]-'0')*10+(s[6]-'0');
                    }
                }
                else
                {
                    map[i][j].type=0;
                    map[i][j].id=0;
                    map[i][j].a=0;
                    map[i][j].b=0;
                }
            }
        }
        num=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(map[i][j].type>0)
                {
                    if(map[i][j].type!=2)
                        num++;
                    else
                        num+=2;
                    map[i][j].id=num;
                }
            }
        }
        t=num+1;
        cnt=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(map[i][j].type==3)
                {
                    tot=0;
                    for(int k=i+1;k<=n;k++)
                    {
                        if(map[k][j].type!=1) break;
                        add(map[k][j].id,map[i][j].id,8);
                        tot++;
                    }
                    add(map[i][j].id,t,map[i][j].a-tot);
                }
                else if(map[i][j].type==4)
                {
                    tot=0;
                    for(int k=j+1;k<=m;k++)
                    {
                        if(map[i][k].type!=1) break;
                        add(map[i][j].id,map[i][k].id,8);
                        tot++;
                    }
                    add(0,map[i][j].id,map[i][j].a-tot);
                }
                else if(map[i][j].type==2)
                {
                    tot=0;
                    for(int k=i+1;k<=n;k++)
                    {
                        if(map[k][j].type!=1) break;
                        add(map[k][j].id,map[i][j].id-1,8);
                        tot++;
                    }
                    add(map[i][j].id-1,t,map[i][j].a-tot);
                    tot=0;
                    for(int k=j+1;k<=m;k++)
                    {
                        if(map[i][k].type!=1) break;
                        add(map[i][j].id,map[i][k].id,8);
                        tot++;
                    }
                    add(0,map[i][j].id,map[i][j].b-tot);
                }
            }
        }
        dinic();
        outputgraph();  
    }
    return 0;
}


你可能感兴趣的:(hdu 3338 Kakuro Extension(网络流dinic邻接表实现))