HDU 3338 Kakuro Extension 最大流 行进列出

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Tawn0000/article/details/82669658

                                       Kakuro Extension

                                          Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
                                                               Total Submission(s): 2668    Accepted Submission(s): 939
                                                                                                        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 _
 

Author

NotOnlySuccess@HDU

最大流,行进列出或者列进行出

以行和点,列和点建图,建图时注意 因为只有1-9,所以将所有的边行和点连向的格点的边权为8,列同。

起点和终点连行和点和列和点的边权为和-和管理的点的个数。

最终答案记得 + 1;

1.离散化

2.SAP

3.最后确定点上的值是只用找行和点或者列和点就好(只取行或者只取列,都取会TLE ,复杂度有点卡常)

//Max_flow
//@2018/05/04 Friday
//SAP  O(n^2 * m)  O(m*3*2)
//by Tawn
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
#include <map>

using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 50000 + 100;
const int maxm = 500000 + 10;
typedef pair<int,int> P;

int head[maxn];//链式前向星
int tot = 0,num;
int n,m;
int g[110][110][2];
int ANS[110][110];
map<P,int> mp;
map<P,int> mpv;


struct edge
{
  int to;
  int c;
  int next;
  edge(int x = 0, int y = 0, int z = 0) : to(x), c(y), next(z) {}
 }es[maxm*2];//记录边 注意是2倍

void add_edge(int u, int v, int c)
{
    es[tot] = edge(v,c,head[u]);
    mpv[P(u,v)] = tot;
    head[u] = tot++;
}


int SAP(int s, int t)
{
    int numh[maxn],h[maxn],ce[maxn],pre[maxn];
    //numh 记录gap优化的统计高度数量数组,h 距离标号数组,ce 当前弧,pre前驱数组
    int f, ans = 0, u, temp, neck, i; //初始化最大流为0
    memset(h,0,sizeof(h));
    memset(numh,0,sizeof(numh));
    memset(pre,-1,sizeof(pre));
    for(i = 0; i <= num; i++)  ce[i] = head[i];
    numh[0] = num;
    u = s;
    while(h[s] < num)
    {
        //寻找增广路
        if(u == t)
        {
            f = INF;
            for(i = s; i != t; i = es[ce[i]].to)
            {
                if(f > es[ce[i]].c)
                {
                    neck = i;
                    f = es[ce[i]].c;
                }
            }
            for(i = s; i != t; i = es[ce[i]].to)
            {
                temp = ce[i];
                es[temp].c -= f;
                es[temp^1].c += f;
            }
            ans += f;
            u = neck;
        }

        //寻找可行弧
        for(i = ce[u]; i != -1; i = es[i].next)
            if(es[i].c && h[u] == h[es[i].to] + 1)  break;

       //寻找增广路
        if(i != -1)
        {
            ce[u] = i;
            pre[es[i].to] = u;
            u = es[i].to;
        }
        else
        {
            if(!--numh[h[u]]) break; //gap optimization
            ce[u] = head[u];
            for(temp = num, i = head[u]; i != -1; i = es[i].next)
                if(es[i].c)  temp = min(temp, h[es[i].to]);

            h[u] = temp + 1;
            ++numh[h[u]];
            if(u != s) u = pre[u];//重标号并且从当前点前驱重新增广
        }

    }
    return ans;
}



int main()
{
  while(~scanf("%d%d",&n,&m))
  {
    int s = 0,t = 1;
    num = 2;
    tot = 0;
    mp.clear();
    mpv.clear();
    memset(head,-1,sizeof(head));
    memset(g,0,sizeof(g));
    for(int i = 0; i < n; i++)
     for(int j = 0;j < m; j++)
     {
       char str[8];
       scanf("%s",str);
       if(str[3] == '.') {g[i][j][0] = g[i][j][1] = -2;mp[P(i,j)] = num++;}
       else if(str[3] == 'X') g[i][j][0] = g[i][j][1] = -1;
       else {
         if(str[0] == 'X') g[i][j][0] = -1;
         else {
           g[i][j][0] = (str[0]-'0')*100+(str[1]-'0')*10+(str[2]-'0');
           mp[P(-i,-j)] = num++;}
         if(str[4] == 'X') g[i][j][1] = -1;
         else {
           g[i][j][1] = (str[4]-'0')*100+(str[5]-'0')*10+(str[6]-'0');
           mp[P(i,j)] = num++;}
       }
     }
    for(int i = 0; i < n; i++)
      for(int j  = 0; j < m; j++)
        {
          if(g[i][j][0] > -1)
          {
            int k;
            for(k = i+1; k < n; k++)
            {
              if(g[k][j][0] == -2 && g[k][j][1] == -2)
              {
                add_edge(mp[P(-i,-j)],mp[P(k,j)],8);
                add_edge(mp[P(k,j)],mp[P(-i,-j)],0);
              }
              else break;
            }
            add_edge(s,mp[P(-i,-j)],g[i][j][0]-(k-i)+1);
            add_edge(mp[P(-i,-j)],s,0);
          }
          if(g[i][j][1] > -1)
          {
            int k;
            for(k = j+1; k < m; k++)
            {
              if(g[i][k][0] == -2 && g[i][k][1] == -2)
              {
                add_edge(mp[P(i,k)],mp[P(i,j)],8);
                add_edge(mp[P(i,j)],mp[P(i,k)],0);
              }
              else break;
            }
            add_edge(mp[P(i,j)],t,g[i][j][1]-(k-j)+1);
            add_edge(t,mp[P(i,j)],0);
          }
        }

      int ans = SAP(s,t);
    memset(ANS,-1,sizeof(ANS));

    for(int i = 0; i < n; i++)
      for(int j = 0; j < m; j++)
        {
          if(g[i][j][0] > -1 )
            {
              int k;
              for(k = i+1; k < n; k++)
                 {
                   if(g[k][j][0] == -2 && g[k][j][1] == -2)
                         ANS[k][j] = 8-es[mpv[P(mp[P(-i,-j)],mp[P(k,j)])]].c+1;
                 }
            }
          // if(g[i][j][1] > -1)
          // {
          //   int k;
          //   for( k = j+1; k < m; k++)
          //   {
          //     if(g[i][k][0] == -2 && g[i][k][1] == -2)
          //       ANS[i][k] = 8 - es[mpv[P(mp[P(i,k)],mp[P(i,j)])]].c + 1;
          //   }
          // }
        }
   for(int i = 0; i < n; i++)
     {
       for(int j = 0; j < m; j++)
         {
           if(ANS[i][j] == -1) printf("_");
           else printf("%d",ANS[i][j]);
           if(j < m-1) printf(" ");
         }
      printf("\n");
     }

  }
  return 0;
}

猜你喜欢

转载自blog.csdn.net/Tawn0000/article/details/82669658
今日推荐