hdu 3338 Kakuro Extension(最大流,巧妙建图)

Kakuro Extension

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1715    Accepted Submission(s): 586
Special Judge


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.
               
        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 _
题意:看图就知道了吧..给你一行的和和一列的和,填数字。不过注意数字可以重复

思路:此题做到吐血..............建图时一个认为不可能出错的地方写错了 - -改了两个多小时............想死。

我们可以把这题理解为从左边进从右边出。 因为数字是1~9,而网络流是从0开始的,所以我们可以统一减1,最后再加回来就可以。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
#define N 14000
#define INF 0x3f3f3f3f
struct Edge
{
    int to,next,cap;
}edge[500000];
struct Node
{
    int x,y,z;
}col[N],row[N];
int mp[N][N],cnt,head[N];
int vis[N],d[N];
void init()
{
    memset(head,-1,sizeof(head));
    cnt=0;
}
void addedge(int from,int to,int cap)
{
    edge[cnt].to=to;edge[cnt].cap=cap;
    edge[cnt].next=head[from];head[from]=cnt++;
    edge[cnt].to=from;edge[cnt].cap=0;
    edge[cnt].next=head[to];head[to]=cnt++;
}
int bfs(int s,int t)
{
    memset(d,-1,sizeof(d));
    queue<int> q;
    q.push(s);
    d[s]=0;
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].to;
            if(d[v]==-1&&edge[i].cap>0)
            {
                d[v]=d[u]+1;
                q.push(v);
            }
        }
    }
    return d[t]!=-1;
}
int dfs(int s,int t,int f)
{
    if(s==t||f==0) return f;
    int flow=0;
    for(int i=head[s];i!=-1&&flow<f;i=edge[i].next)
    {
        int v=edge[i].to;
        if(d[v]==d[s]+1&&edge[i].cap>0)
        {
            int x=min(f-flow,edge[i].cap);
            x=dfs(v,t,x);
            flow+=x;
            edge[i].cap-=x;
            edge[i^1].cap+=x;
            if(f==0) break;
        }
    }
    if(!flow) d[s]=-2;
    return flow;
}
int Dinic(int s,int t)///起点s终点t
{
    int flow=0,f;
    while(bfs(s,t))
    {
        while(f=dfs(s,t,INF))
            flow+=f;
    }
    return flow;
}
int solve(int row_num,int tp)
{
    int ans = 0;
    int id = tp + row_num;
    for( int i = head[id] ; i != -1 ; i = edge[i].next )
    {
        int v = edge[i].to;
        if( v <=row_num+1 )
        {
            ans+= edge[i].cap;
            break;
        }
    }
    return ans+1;
}
int main()
{
    char str[15];
    int n,m;
    while(~scanf("%d %d",&n,&m))
    {
        init();
        int acnt=0,col_cnt=0,row_cnt=0;
        for(int i=1; i<=n; i++)
        {
            for(int j=1;j<=m;j++)
            {
                scanf("%s",str);
                if(str[0]=='.') mp[i][j]=++acnt;
                else
                {
                    mp[i][j]=-1;
                    if(str[0]!='X')
                    {
                        col[++col_cnt].x=i;
                        col[col_cnt].y=j;
                        col[col_cnt].z=(str[0]-'0')*100+(str[1]-'0')*10+str[2]-'0';
                    }
                    if(str[4]!='X')
                    {
                        row[++row_cnt].x=i;
                        row[row_cnt].y=j;
                        row[row_cnt].z=(str[4]-'0')*100+(str[5]-'0')*10+str[6]-'0';
                    }
                }
            }
        }
        int S=0,T=col_cnt+row_cnt+acnt+2;
        for(int i=1;i<=row_cnt;i++)
        {
            int x=row[i].x,y=row[i].y;
            int num=0;
            for(int j=y+1;j<=m;j++)
            {
                if(mp[x][j]!=-1)
                {
                    num++;
                    addedge(i,row_cnt+mp[x][j],8);
                }
                else break;
            }
            addedge(S,i,row[i].z-num);
        }

        for(int i=1;i<=col_cnt;i++)
        {
            int x=col[i].x,y=col[i].y;
            int num=0;
            for(int j=x+1;j<=n;j++)
            {
                if(mp[j][y]!=-1)
                {
                    num++;
                    addedge(row_cnt+mp[j][y],row_cnt+acnt+i,8);
                }
                else break;
            }
            addedge(row_cnt+acnt+i,T,col[i].z-num);
        }
        int ans=Dinic(S,T);
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(mp[i][j]==-1) printf("_");
                else printf("%d",solve(row_cnt,mp[i][j]));
                printf("%c",j==m?'\n':' ');
            }
        }
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值