最大流-HDU-3338-Kakuro Extension

Kakuro Extension
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1678 Accepted Submission(s): 579
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填到图中的白色方格中,使得黑色方格中的条件被满足。
黑色方格中有两种条件:
1.向下的和:从此黑色方格起,向下连续的白色方格中的数字之和必须为给定的值;
2.向右的和:从此黑色方格起,向右连续的白色方格中的数字之和必须为给定的值。
现在求一种可行方案。


题解:
这是一道比较巧妙的题,将所有有着向右和条件的黑色方格看作入口,把所有有着向下和条件的黑色方格看作出口,则就是求一个最大流的方案了。由于有的黑色方格有两个约束条件,所以要把向右和与向上和拆为两个点。
然后记录一下每个白色方格点到上方黑色方格点的边的编号,方便输出其流。


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <utility>
#define LL long long int
using namespace std;
int n,m;
const string white=".......";
string Maze[105][105];
bool bai[105][105];
int shang[105][105],zuo[105][105];
const int MAXN = 100010;//点数的最大值
const int MAXM = 400010;//边数的最大值
const int INF = 0x3f3f3f3f;
struct Edge
{
    int to,next,cap,flow;
} edge[MAXM]; //注意是MAXM
int tol;
int head[MAXN];
int gap[MAXN],dep[MAXN],pre[MAXN],cur[MAXN];
void init()
{
    tol = 0;
    memset(head,-1,sizeof(head));
}
//加边,单向图三个参数,双向图四个参数
void addedge(int u,int v,int w,int rw=0)
{
    edge[tol].to = v;
    edge[tol].cap = w;
    edge[tol].next = head[u];
    edge[tol].flow = 0;
    head[u] = tol++;
    edge[tol].to = u;
    edge[tol].cap = rw;
    edge[tol].next = head[v];
    edge[tol].flow = 0;
    head[v]=tol++;
}
//输入参数:起点、终点、点的总数
//点的编号没有影响,只要输入点的总数
int sap(int start,int end,int N)
{
    memset(gap,0,sizeof(gap));
    memset(dep,0,sizeof(dep));
    memcpy(cur,head,sizeof(head));
    int u = start;
    pre[u] = -1;
    gap[0] = N;
    int ans = 0;
    while(dep[start] < N)
    {
        if(u == end)
        {
            int Min = INF;
            for(int i = pre[u]; i != -1; i = pre[edge[i^1].to])
                if(Min > edge[i].cap - edge[i].flow)
                    Min = edge[i].cap - edge[i].flow;
            for(int i = pre[u]; i != -1; i = pre[edge[i^1].to])
            {
                edge[i].flow += Min;
                edge[i^1].flow -= Min;
            }
            u = start;
            ans += Min;
            continue;
        }
        bool flag = false;
        int v;
        for(int i = cur[u]; i != -1; i = edge[i].next)
        {
            v = edge[i].to;
            if(edge[i].cap - edge[i].flow && dep[v]+1 == dep[u])
            {
                flag = true;
                cur[u] = pre[v] = i;
                break;
            }
        }
        if(flag)
        {
            u = v;
            continue;
        }
        int Min = N;
        for(int i = head[u]; i != -1; i = edge[i].next)
            if(edge[i].cap - edge[i].flow && dep[edge[i].to] < Min)
            {
                Min = dep[edge[i].to];
                cur[u] = i;
            }
        gap[dep[u]]--;
        if(!gap[dep[u]])return ans;
        dep[u] = Min+1;
        gap[dep[u]]++;
        if(u != start) u = edge[pre[u]^1].to;
    }
    return ans;
}
int total_s=0,total_z=0;
inline int GetNum(int x,int y)
{
    return (x-1)*m+y;
}
int shang_x[10005],shang_y[10005],zuo_x[10005],zuo_y[10005];
int EdgeNum[105][105];
inline int GetFlow(int x,int y)
{
    return edge[EdgeNum[x][y]].flow+1;
}
int main()
{
    ios::sync_with_stdio(false);
    while(cin >> n >> m)
    {
        init();
        total_s=total_z=0;
        memset(bai,0,sizeof(bai));
        for(int i=1; i<=n; i++)
            for(int j=1; j<=m; j++)
                cin >> Maze[i][j];
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=m; j++)
            {
                if(Maze[i][j]==white)
                {
                    bai[i][j]=1;
                    continue;
                }
                else if(Maze[i][j][3]=='X')
                    continue;
                if(Maze[i][j][2]!='X')
                {
                    total_s++;
                    shang_x[total_s]=i;
                    shang_y[total_s]=j;
                    shang[i][j]=Maze[i][j][2]-'0'+(Maze[i][j][1]-'0')*10+(Maze[i][j][0]-'0')*100;
                }
                if(Maze[i][j][4]!='X')
                {
                    total_z++;
                    zuo_x[total_z]=i;
                    zuo_y[total_z]=j;
                    zuo[i][j]=Maze[i][j][6]-'0'+(Maze[i][j][5]-'0')*10+(Maze[i][j][4]-'0')*100;
                }
            }
        }
        for(int i=1; i<=total_s; i++)
        {
            int x=shang_x[i],y=shang_y[i],tmp=0;
            for(int j=x+1; j<=n; j++)
            {
                if(!bai[j][y])
                    break;
                tmp++;
                addedge(GetNum(j,y),n*m+i,8);
            }
            addedge(n*m+i,n*m+total_s+total_z+1,shang[x][y]-tmp);
        }
        for(int i=1; i<=total_z; i++)
        {
            int x=zuo_x[i],y=zuo_y[i],tmp=0;
            for(int j=y+1; j<=m; j++)
            {
                if(!bai[x][j])
                    break;
                tmp++;
                addedge(n*m+total_s+i,GetNum(x,j),8);
                EdgeNum[x][j]=tol-2;
            }
            addedge(0,n*m+total_s+i,zuo[x][y]-tmp);
        }
        sap(0,n*m+total_s+total_z+1,n*m+total_s+total_z+2);
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=m; j++)
            {
                if(!bai[i][j])
                    cout << "_ ";
                else
                    cout << GetFlow(i,j) << ' ';
            }
            cout << endl;
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值