hdu 3338 Kakuro Extension(最大流)

hdu 3338 Kakuro Extension

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

_ _ 7 9 _ _

_ 1 9 9 1 1 8 6

_ 1 3 9 9 9 3 9

_ 6 7 2 4 9 2 _

题目大意:给出行和和列和,要求求出满足条件的一种情况。

解题思路:很神奇的一题最大流。设置超级源点,连向所有的行(拥有空白块的行),容量为该行的和。设置超级汇点,使所有的列连向它,容量为该列的和。然后所有的空白块,都连向,他所对应的行和列,容量为8。这里有一个问题,为什么容量是8,不是9。在uva 11082 Matrix Decompressing中也有类似的问题。之所以容量为8,是为了避免零流。在每一空白块都减1的情况下,行和和列和也要记得减去相应的值。图建完了,就直接求最大流。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
using namespace std;

const int N = 10010;
const int M = 100100;
const int INF = 0x3f3f3f3f;
const int PO = 105;
#define MIN(a, b) a < b ? a : b
typedef long long ll;

struct Node{
    int r, c;
}map[PO][PO];

int n, m, s, t;
int row[N], col[N], rec[PO][PO];
char ch[10];
int ec, head[N], first[N], que[N], lev[N];
int Next[M], to[M], v[M];

void init() {
    ec = 0;
    memset(first, -1, sizeof(first));
    memset(row, 0, sizeof(row));
    memset(col, 0, sizeof(col));
    memset(rec, -1, sizeof(rec));
}

void addEdge(int a,int b,int c) {
    to[ec] = b;
    v[ec] = c;
    Next[ec] = first[a];
    first[a] = ec++;

    to[ec] = a;
    v[ec] = 0;
    Next[ec] = first[b];
    first[b] = ec++;
}

int BFS() {
    int kid, now, f = 0, r = 1, i;
    memset(lev, 0, sizeof(lev));
    que[0] = s, lev[s] = 1;
    while (f < r) {
        now = que[f++];
        for (i = first[now]; i != -1; i = Next[i]) {
            kid = to[i];
            if (!lev[kid] && v[i]) {
                lev[kid] = lev[now] + 1;
                if (kid == t) return 1;
                que[r++] = kid;
            }
        }
    }
    return 0;
}

int DFS(int now, int sum) {
    int kid, flow, rt = 0;
    if (now == t) return sum;
    for (int i = head[now]; i != -1 && rt < sum; i = Next[i]) {
        head[now] = i;
        kid = to[i];
        if (lev[kid] == lev[now] + 1 && v[i]) {
            flow = DFS(kid, MIN(sum - rt, v[i]));
            if (flow) {
                v[i] -= flow;
                v[i^1] += flow;
                rt += flow;
            } else lev[kid] = -1;
        }
    }
    return rt;
}

int dinic() {
    int ans = 0;
    while (BFS()) {
        for (int i = 0; i <= t; i++) {
            head[i] = first[i];
        }
        ans += DFS(s, INF);
    }
    return ans;
}   

int getNum(char *s,int l,int r) {
    if(s[l] == ‘X‘) return -1;
    return 100 * (s[l] - ‘0‘) + 10 * (s[l + 1] - ‘0‘) + s[l + 2] - ‘0‘;
}

void input() {
    int rn = 0, cn = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            scanf("%s", ch);
            if (ch[3] == ‘X‘) continue;
            if (ch[0] == ‘.‘) {
                map[i][j].r = map[i][j - 1].r;
                map[i][j].c = map[i - 1][j].c;
                row[map[i][j].r]--; //所有元素减一,所以行的和每对应一个元素,就要减一。
                col[map[i][j].c]--; //同上。
                rec[i][j] = 0;
            } else {
                int a = getNum(ch, 0, 2), b = getNum(ch, 4, 6);
                if (b != -1) row[++rn] = b, map[i][j].r = rn;
                if (a != -1) col[++cn] = a, map[i][j].c = cn;
            }
        }
    }
    s = 0, t = rn + cn + 1;
    for (int i = 1; i <= rn; i++) addEdge(s, i, row[i]);
    for (int i = 1; i <= cn; i++) addEdge(i + rn, t, col[i]);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (rec[i][j] != -1) {
                addEdge(map[i][j].r, map[i][j].c + rn, 8);
                rec[i][j] = ec - 2;
            }
        }
    }
}

void print() {
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < m; j++) {
            if(rec[i][j] == -1) printf("_");
            else {
                int id = rec[i][j];
                printf("%d", 9 - v[id]);
            }
            if(j != m - 1) printf(" ");
        } puts("");
    }
}

int main() {
    while (scanf("%d %d\n", &n, &m) == 2) {
        init();
        input();
        dinic();
        print();
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许也可以转载,不过要注明出处哦。

时间: 2024-10-09 15:04:20

hdu 3338 Kakuro Extension(最大流)的相关文章

HDU - 3338 Kakuro Extension(最大流)

题目大意:看一下图基本就知道了 解题思路:难点是构图.. 设置一个超级源点和所有的行的和相连,容量为该行的和 - 该行和由几个数相加得到 设置一个超级汇点,和所有列的和相连,容量为该列的和 - 该列和由几个数相加得到 接着就是空白部分和 "行和"和 "列和"的关系了 空白连向该行的行和,权值为8 空白连向该列的列和,权值也为8 为什么为8,而不是9,因为流量也可能为0,但是0是不能填的,所以将容量设为8,最后取值的时候加1即可 #include <cstdio

HDU 3338 Kakuro Extension(网络流)

HDU 3338 Kakuro Extension 题目链接 题意:完成如图的游戏,填充数字1-9 思路:网络流的行列模型,把每行每列连续的一段拆分出来建图即可,然后题目有限制一个下限1,所以 每行每列的容量减去相应的数字,然后建图建容量8就好,这样就默认原来容量已经有1了 代码: #include <cstdio> #include <cstring> #include <queue> #include <algorithm> using namespac

HDU 3338 Kakuro Extension

网络最大流 TLE了两天的题目.80次Submit才AC,发现是刘汝佳白书的Dinic代码还可以优化.....瞬间无语..... #include<cstdio> #include<cstring> #include<string> #include<cmath> #include<vector> #include<queue> #include<algorithm> using namespace std; const

hdoj 3338 Kakuro Extension 【经典最大流+输出流量】

题目:hdoj 3338 Kakuro Extension 定义:神级最大流(各种错误无数次,整整一天) 题意:一个游戏,这个游戏给出一个矩阵,有些矩阵里面有两个数,前面一个数表示从下一行到下一个出现数字行的所有数字和(当前这一列),而第二个数表示从下一列到下一个出现数字的列(当前这一行),让你填入满足条件的数字的矩阵(1---9),可以重复(最大流条件). 分析:首先数字可以重复那么确定了可以用最大流来做,建图方法就是列进行出,或者行进列出,前者比较好写. 这个题目还有一个条件就是要求必须有流

HDU-3338 Kakuro Extension(最大流,方格横纵和问题)

题目链接:HDU-3338 Kakuro Extension 题意 给出一个$n\times m$的网格,每个格子为黑色或白色,对于一行中连续的若干个白色格子,我们要往这若干个白色格子中填入$1\sim 9$的数字,使其和等于左边黑色格子中的一个已知数字$a_1$:对于一列中连续的若干个白色格子,同理填入$1\sim 9$的数字使其和等于上边黑色格子中的一个已知数字$a_2$.如果一个黑色格子相邻的右边和下边都有白色格子,那么这个黑色格子是带有两个已知数字$a_1$和$a_2$的,分别代表右边和

hdu 3344 Kakuro Extension Extension

Kakuro Extension Extension Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 468    Accepted Submission(s): 240 Problem Description You know ,I'm a lazy guy and write problem description is a ver

HDU3338 Kakuro Extension(最大流+思维构图)

这道题一定要写一下,卡了好久. 题意: 有黑白两种方格,最上边一行和最左边一列一定是黑色,然后其余的地方有可能是黑色,有可能是白色,和白色相邻的黑色方格里有数字(1个或2个), 现在要求在白色方格里填1~9中的一个数字,使得一个黑色方格下边的数字 = sigma(该黑色方格下边白色方格数字)  这个sigma不是下边全部的白方格, 而是一直往下走一直走到一个黑方格之前所有的白方格,详情见样例1.相同的,黑方格右上角的数字 = sigma(该黑色方格右边白色方格数字). 思路: 可以很容易看出,所

hdu 3338 最大流 ****

题意: 黑格子右上代表该行的和,左下代表该列下的和 链接:点我 这题可以用网络流做.以空白格为节点,假设流是从左流入,从上流出的,流入的容量为行和,流出来容量为列和,其余容量不变.求满足的最大流.由于流量有上下限限制,可以给每个数都减掉1,则填出来的数字范围为0—8, 就可以用单纯的网络流搞定了.求出来再加上就可以了. 这一题主要是在建图 建图: 一共有四类点: 1. 构造源点ST,汇点ED 2. 有行和的格子,即\上面有值的格子,此类节点设为A 3. 空白格,设为B 4. 有列和的格子,即\下

Kakuro Extension (hdu 3338 最大流 建图难)

Kakuro Extension Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1093    Accepted Submission(s): 377 Special Judge Problem Description If you solved problem like this, forget it.Because you nee