hdu3338 Kakuro Extension(较难,好题) [最大流][数和]神奇最大流行进列出

Kakuro Extension

Time Limit: 1000 MS Memory Limit: 32768 KB

64-bit integer IO format: %I64d , %I64u Java class name: Main

Special Judge

[Submit] [Status] [Discuss]

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 _

Source

HDOJ Monthly Contest – 2010.03.06

题意:给你一个图填数,有的是空白让你填,有的可能有两个数,左下角的数表示这格下面连续的空格的和,右上的数表示这格右面连续的空格的和。输出你填好的图,任意一个均可。

思路:如果不是在网络流专题上看到,可能想不到网络流。。

把有横向和的当作点A。

把空格作点B。

把有纵向和的点作点C。

源点S,汇点T。

则可以构图:

add(f,t,c)加一条从f至t容量为c的边。

每个A,add(S,   A,   A的和-拥有空格数)。(因为8为上限,因此要减)

对每个A的每个空格add(A,B,8)。(因为填数从1-9,有下限,为方便提前减1)

对每个C的每个空格add(B,  C,8)。

每个C,add(C,T,C的和-拥有空格数)。

然后跑一边最大流,把B节点的cap读出来,作差,即可。

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
const int inf = 2147483647;
const int MAXN = 20000;

struct edge{
   int to,cap,rev;
   int num;
   edge(int a = 0,int b = 0,int c = 0,int d = 0):to(a),cap(b),rev(c),num(d){}
};
struct Point{
    int yh,yl,yk;
    int h,l,k;
    int x,y;
}mapp[109][109];

vector<edge >G[MAXN];
int level[MAXN];
int iter[MAXN];
int hsum,lsum,ksum;
int hnum[MAXN],lnum[MAXN],knum[MAXN];

void init()
{
    for(int i = 0;i < MAXN;i++){
        G[i].clear();
        hnum[i] = lnum[i] = knum[i] = 0;
    }
    hsum = lsum = ksum = 0;
    for(int i = 0;i < 109;i++){
        for(int j = 0;j < 109;j++){
            mapp[i][j].yh = 0;
            mapp[i][j].yl = 0;
            mapp[i][j].yk = 0;
            mapp[i][j].h = 0;
            mapp[i][j].l = 0;
            mapp[i][j].k = 0;
        }
    }
}

void Add(int f,int t,int c,int n)
{
    G[f].push_back(edge{t,c,G[t].size()  ,n});
    G[t].push_back(edge{f,0,G[f].size()-1,0});
}

void bfs(int s)
{
    memset(level,-1,sizeof(level));
    level[s] = 0;
    queue<int> que;
    que.push(s);
    while(!que.empty()){
        int v = que.front();
        que.pop();
        for(int i = 0;i < G[v].size();i++){
            edge &e = G[v][i];

            if(e.cap > 0 && level[e.to] < 0){
                level[e.to] = level[v]+1;
                que.push(e.to);
            }
        }
    }
}

int dfs(int v,int t,int f)
{
    if(v == t) return f;
    for(int &i = iter[v];i < G[v].size();i++){
        edge &e = G[v][i];
        if(e.cap > 0 && level[e.to] > level[v]){
            int d = dfs(e.to,t,min(f,e.cap));
            if(d > 0){
                e.cap -= d;
                G[e.to][e.rev].cap += d;
                return d;
            }
        }
    }
    return 0;
}

int maxfd(int s,int t)
{
    int flow = 0;
    while(true){
        bfs(s);
        if(level[t] < 0) return flow;
        memset(iter,0,sizeof(iter));
        int f;
        while((f = dfs(s,t,inf)) > 0){
            flow += f;

        }
    }
}

int main()
{
    int hang,lie;
    char name[10];
    while(scanf("%d%d",&hang,&lie) != EOF){
        init();
        for(int i = 0;i < hang;i++){
            for(int j = 0;j < lie;j++){
                scanf("%s",&name);
                if(name[3] == 'X') continue;
                else if(name[3] == '.'){
                    mapp[i][j].x = i;
                    mapp[i][j].y = j;
                    mapp[i][j].yk = ++ksum;
                }
                else{
                    if(name[2] != 'X'){
                        mapp[i][j].x = i;
                        mapp[i][j].y = j;
                        mapp[i][j].yl = ++lsum;
                        for(int p = 0;p <= 2;p++){
                            mapp[i][j].l = mapp[i][j].l*10+name[p]-'0';
                        }
                    }
                    if(name[6] != 'X'){
                        mapp[i][j].x = i;
                        mapp[i][j].y = j;
                        mapp[i][j].yh = ++hsum;
                        for(int p = 4;p <= 6;p++){
                            mapp[i][j].h = mapp[i][j].h*10+name[p]-'0';
                        }
                    }
                }
            }
        }

        int yuan = 0;
        int hui = hsum+lsum+ksum+1;
        for(int i = 0;i < hang;i++){
            for(int j = 0;j < lie;j++){
                if(mapp[i][j].yh > 0){
                    int cnt = 0;
                    for(int p = j+1;p < lie;p++){
                        if(mapp[i][p].yk > 0){
                            cnt++;
                            Add(mapp[i][j].yh,hsum+mapp[i][p].yk,8,1);
                        }
                        else{
                            break;
                        }
                    }
                    Add(yuan,mapp[i][j].yh,mapp[i][j].h-cnt,0);
                }
                if(mapp[i][j].yl > 0){
                    int cnt = 0;
                    for(int p = i+1;p < hang;p++){
                        if(mapp[p][j].yk > 0){
                            cnt++;
                            Add(hsum+mapp[p][j].yk,hsum+ksum+mapp[i][j].yl,8,1);
                        }
                        else{
                            break;
                        }
                    }
                    Add(hsum+ksum+mapp[i][j].yl,hui,mapp[i][j].l-cnt,0);
                }
            }
        }
        int ans = maxfd(yuan,hui);

        for(int i = 0;i < hang;i++){
            for(int j = 0;j < lie;j++){
                if(j != 0) printf(" ");
                if(mapp[i][j].yk > 0){
                    for(int p = 0;p < G[hsum+mapp[i][j].yk].size();p++){
                        if(G[hsum+mapp[i][j].yk][p].num == 1){
                            printf("%d",9-G[hsum+mapp[i][j].yk][p].cap);
                            break;
                        }
                    }
                }
                else{
                    printf("_");
                }
            }
            printf("\n");
        }
    }
    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值