hdu 3338 Kakuro Extension

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3338

题目大意:填数字,使白色区域的值得和等于有值得黑色区域的相对应的值,用网络流来做

题目思路:增加一个源点和汇点,然后左进上出,用源点连左面,容量为相对应的值,汇点连上面,容量是相对应的值,然后左面连空白区域的,容量为8,因为数字为1-9,有下限,但因每个空白必须有值,所以至少为一,所以容量空间为0-8;然后空白区域来连上面,容量同样为8;

题目原题:

Kakuro Extension

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 566    Accepted Submission(s): 193
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 _
 
题目代码:

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define INF 999999999
#define maxn 14000

#define RE(x) (x)^1
int head[maxn];
int st,ed;//开始节点,结束节点
struct Edge
{
    int v,next;
    int val;
    Edge() {}
    Edge( int V , int NEXT , int W = 0 ):v(V),next(NEXT),val(W) {}
} edge[500000];
int lvl[maxn], gap[maxn];
int cnt_edge;
int map[maxn][maxn];
struct gg
{
    int x,y;
    int val;
} row[maxn],col[maxn];
int emp,row_num,col_num;
int n,m,T;
void Insert_Edge( int u , int v , int flow = 0 )
{

    edge[cnt_edge] = Edge(v,head[u],flow);
    head[u] = cnt_edge++;
    edge[cnt_edge] = Edge(u,head[v]);
    head[v] = cnt_edge++;

}
void Init()
{
    cnt_edge = 0;
    memset(head,-1,sizeof(head));
    memset(lvl, 0, sizeof (lvl));
    memset(gap, 0, sizeof (gap));
}
int dfs(int u, int flow)
{
    if (u==ed)
    {
        return flow;
    }
    int tf = 0, sf, mlvl = ed-1;
    for (int i= head[u]; i != -1; i = edge[i].next)
    {
        if (edge[i].val > 0)
        {
            if (lvl[u] ==lvl[edge[i].v]+1)
            {
                sf = dfs(edge[i].v, min(flow-tf, edge[i].val));
                edge[i].val -= sf;
                edge[RE(i)].val += sf;
                tf += sf;
                if (lvl[st] >=ed)
                {
                    return tf;
                }
                if (tf == flow)
                {
                    break;
                }
            }
            mlvl = min(mlvl, lvl[edge[i].v]);
        }
    }
    if (tf == 0)
    {
        --gap[lvl[u]];
        if (!gap[lvl[u]])
        {
            lvl[st] =ed;
        }
        else
        {
            lvl[u] = mlvl+1;
            ++gap[lvl[u]];
        }
    }
    return tf;
}
int sap()
{
    int ans = 0;
    gap[0]=ed;
    while (lvl[st] <ed)
    {
        ans += dfs(st, INF);
    }
    return ans;
}
int print( int tp )
{
    int ans = 0;
    int id = tp + row_num+1;
    for( int i = head[id] ; i != -1 ; i = edge[i].next )
    {
        int v = edge[i].v;
        if( v <=row_num+1 )
        {
            ans+= edge[i].val;
            break;
        }
    }
    return ans+1;
}
int main()
{
    int i,j;
    char s[15];
    //freopen("F://ACMInput/input.txt","r",stdin);
    while(scanf("%d%d",&n,&m)!=-1)
    {
        emp=row_num=col_num=0;
        for(i=0; i<n; i++)
            for(j=0; j<m; j++)
            {
                scanf("%s",s);
                if(s[0]=='.')
                {
                    map[i][j]=++emp;
                }
                else
                {
                    map[i][j]=-1;
                    if(s[4]!='X')
                    {
                        int tp=(s[4]-'0')*100+(s[5]-'0')*10+s[6]-'0';
                        row[++row_num].x=i;
                        row[row_num].y=j;
                        row[row_num].val=tp;
                    }
                    if(s[0]!= 'X' )
                    {
                        int tp = (s[0]-'0')*100+(s[1]-'0')*10+s[2]-'0';
                        col[++col_num].x = i;
                        col[col_num].y = j;
                        col[col_num].val = tp;
                    }
                }
            }
        T=emp+col_num+row_num+2;
        st=1;
        ed=T;
        Init();
        for(i=1; i<=row_num; i++)
        {
            int pos = i;
            int x = row[i].x;
            int y = row[i].y;
            int cnt_len = 0;
            for( y=y+1; y <m ; y++ )
            {
                if( map[x][y] != -1 )
                {
                    cnt_len++;
                    Insert_Edge(i+1, row_num+ map[x][y]+1,8);
                }
                else break;
            }
            Insert_Edge(st,pos+1,row[i].val-cnt_len);
        }

        for( i = 1 ; i <=col_num ; i++ )
        {
            int pos =i+1+row_num+emp;
            int x = col[i].x;
            int y = col[i].y;
            int cnt_len = 0;
            for( x=x+1 ; x < n ; x++ )
            {
                if( map[x][y] != -1 )
                {
                    cnt_len++;
                    Insert_Edge(row_num+ map[x][y]+1,pos,8);

                }
                else break;
            }
            Insert_Edge(pos,ed,col[i].val-cnt_len);
        }
        sap();
        for(i=0; i<n; i++)
        {
            for(j=0; j<m; j++)
            {

                if(map[i][j]==-1)
                    printf("_ ");
                else
                    printf("%d ",print(map[i][j]));
            }
            printf("\n");
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值