HDU Problem - 3338 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” cell2.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 _
AC
  • 根据游戏规则,每行每列的数字之和只受到相应的数字要求,这样可以用最大流建边写
  • 建边:
    1. 假设流量是从上流入,然后从左流出
    2. 如果只有下边的数字,这个数字是它下面所有的白色格子的和,所以将这个格子和它下面所有的白格子建边,权值为8(最大流的出的可能有0的流量),将这个格子和源点相连(默认流量从上流入)权值为数字 - 下方白色格子的个数(因为每个白格子建边的时候都减1)
    3. 同理只有右边的数字,就让右边的格子和这个格子建边(注意建边方向,流量从左流出),权值为8,并将这个格子和汇点建边,权值为数字 - 右边白色格子的数量
    4. 如果两个数字同时存在,就拆点建边,建边过程同上
  • 因为需要判断每个格子的信息,所以可以用一个结构体来存放每个格子的信息
    1. 类型(黑色, 白色, 有数字)
    2. 数字的大小
  • 100 * 100 的图,所以最多可以建100 * 100 * 2 * 2 条边,(建边的时候默认两条)
  • head数组应该开100 * 100 * 2
  • 最后跑一边Dinic
#include <iostream>
#include <stdio.h>
#include <map>
#include <vector>
#include <queue>
#include <algorithm>
#include <cmath>
#define N 20010
#include <cstring>
#define ll long long
#define P pair<int, int>
#define mk make_pair
using namespace std;
struct ac{
    int v, c, pre;
}edge[40001];
int head[N], dis[N], curedge[N], cnt;
int inf = 0x3f3f3f3f;
void addedge(int u, int v, int c) {
    edge[cnt].v = v;
    edge[cnt].c = c;
    edge[cnt].pre = head[u];
    head[u] = cnt++;
    swap(u, v);
    edge[cnt].v = v;
    edge[cnt].c = 0;
    edge[cnt].pre = head[u];
    head[u] = cnt++;
}
bool bfs(int s, int e) {
    queue<int> que;
    que.push(s);
    memset(dis, 0, sizeof(dis));
    dis[s] = 1;
    while (!que.empty()) {
        int t = que.front();
        que.pop();
        for (int i = head[t]; i != -1; i = edge[i].pre) {
            if (dis[edge[i].v] || edge[i].c == 0)   continue;
            dis[edge[i].v] = dis[t] + 1;
            que.push(edge[i].v);
        } 
    }
    return dis[e] != 0;
}
int dfs(int s, int e, int flow) {
    if (s == e) return flow;
    for (int &i = curedge[s]; i != -1; i = edge[i].pre) {
        if (dis[edge[i].v] == dis[s] + 1 && edge[i].c) {
            int d = dfs(edge[i].v, e, min(flow, edge[i].c));
            if (d > 0) {
                edge[i].c -= d;
                edge[i ^ 1].c += d;
                return d; 
            }
        }
    }
    return 0;
}
int solve(int s, int e) {
    int sum = 0;
    while (bfs(s, e)) {
        for (int i = 0; i <=  e; ++i) {
            curedge[i] = head[i];
        }
        int d;
        while (d = dfs(s, e, inf)) {
            sum += d;
        }
    }
    return sum;
}
// 每个格子的信息 
struct point{
    // type 标记各自的类型
    // 黑色:-1, 白色0, 只有下1, 只有右2, 两个都有3 
    // r 右边数字的和 
    // d 下方数字的和 
    int type, r, d; 
}cell[120][120];
int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
#endif
//  ios::sync_with_stdio(false);
    int n, m;
    while (cin >> n >> m) {
        memset(head, -1, sizeof(head));
        cnt = 0;
        string s;
        // 读入方格信息 
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                cin >> s;
                if (s == "XXXXXXX") {
                    cell[i][j].type = -1;
                }else if (s == ".......") {
                    cell[i][j].type = 0;
                }else {
                    int num1 = 0, num2 = 0;
                    if (s[0] != 'X') {
                        for (int k = 0; k < 3; ++k) {
                            num1 = num1 * 10 + s[k] - '0';
                        }
                    }
                    if (s[4] != 'X') {
                        for (int k = 4; k <= 6; ++k) {
                            num2 = num2 * 10 + s[k] - '0';
                        }
                    }
                    if (num1 && num2) {
                        cell[i][j].type = 3;
                        cell[i][j].r = num2;
                        cell[i][j].d = num1;
                    }else if (num1) {
                        cell[i][j].type = 1;
                        cell[i][j].d = num1;
                    }else {
                        cell[i][j].type = 2;
                        cell[i][j].r = num2;
                    }
                }
            }
        }
        // 定义源点和汇点 
        int start = 0, end = n * m * 2 + 1;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                int type = cell[i][j].type;
                if (type == -1 || type == 0) continue;
                if (type == 1) {
                    int sum = 0;
                    for (int k = i + 1; k <= n; ++k) {
                        if (cell[k][j].type != 0)   break;
                        sum++;
                        addedge(i * m - m + j, k * m - m + j, 8);
                    }
                    addedge(start, i * m - m + j, cell[i][j].d - sum);
                }else if (type == 2) {
                    int sum = 0;
                    for (int k = j + 1; k <= m; ++k) {
                        if (cell[i][k].type != 0)   break;
                        sum++;
                        addedge(i * m - m + k, i * m - m + j, 8);
                    }
                    addedge(i * m - m + j, end, cell[i][j].r - sum);
                }else if (type == 3) {
                    // 拆点 
                    int sum;
                    // 向下 
                    sum = 0;
                    for (int k = i + 1; k <= n; ++k) {
                        if (cell[k][j].type != 0)   break;
                        sum++;
                        addedge(i * m - m + j, k * m - m + j, 8);
                    }
                    addedge(start, i * m - m + j, cell[i][j].d - sum);
                    // 向右 
                    sum = 0;
                    for (int k = j + 1; k <= m; ++k) {
                        if (cell[i][k].type != 0)   break;
                        sum++;
                        addedge(i * m - m + k, i * m - m + j + n * m, 8);
                    }
                    addedge(i * m - m + j + n * m, end, cell[i][j].r - sum);
                }
            }
        }
        solve(start, end);
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                if (cell[i][j].type != 0)   cout << "_";
                else {
                    int sum = 0;
                    cout << 8 - edge[head[i * m - m + j]].c + 1;
                }
                if (j == m) cout << endl; 
                else    cout << " ";
            }
        }
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值