集训队专题(7)1008 Kakuro Extension

来源:互联网 发布:php是做前端还是后端 编辑:程序博客网 时间:2024/06/05 06:25

Kakuro Extension

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1617    Accepted Submission(s): 549
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 6XXXXXXX XXXXXXX 028\XXX 017\XXX 028\XXX XXXXXXXXXXXXXX 022\022 ....... ....... ....... 010\XXXXXX\034 ....... ....... ....... ....... .......XXX\014 ....... ....... 016\013 ....... .......XXX\022 ....... ....... ....... ....... XXXXXXXXXXXXXX XXX\016 ....... ....... XXXXXXX XXXXXXX5 8XXXXXXX 001\XXX 020\XXX 027\XXX 021\XXX 028\XXX 014\XXX 024\XXXXXX\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
 

Source
HDOJ Monthly Contest – 2010.03.06
 

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

此题乃网络流神题啊,我是自己做完全想不到和网络流有什么关系,此处小编也没怎么看懂,只有对不起观众的借鉴一下别人的题解……

http://www.cnblogs.com/ylfdrib/archive/2010/08/15/1799903.html

#include <cstdio>#include <cstring>#include <string>#include <iostream>#include <algorithm>#include <vector>#include <queue>using namespace std;const int N = 20500;const int inf = 0x3f3f3f3f;int n,m;struct Node{    int from,to,cap,flow;};vector<int> edge[N];vector<Node> e;int dis[N];  //构建层次图int cur[N];void add_Node(int from,int to,int cap){    e.push_back((Node){from,to,cap,0});    e.push_back((Node){to,from,0,0});    int tmp=e.size();    edge[from].push_back(tmp-2);    edge[to].push_back(tmp-1);}bool bfs(int s,int t){    memset(dis,-1,sizeof(dis));    queue<int> q;    q.push(s);    dis[s] = 0;    while(!q.empty())    {        int x=q.front();        q.pop();        for(int i=0;i<edge[x].size();i++)        {            Node tmp = e[edge[x][i]];            if(dis[tmp.to]<0 && tmp.cap>tmp.flow)  //第二个条件保证            {                dis[tmp.to] = dis[x]+1;                q.push(tmp.to);            }        }    }    if(dis[t]>0)        return true;    return false;}int dfs(int o,int f,int t){    if(o==t || f==0)  //优化        return f;    int a = 0,ans=0;    for(int &i=cur[o];i<edge[o].size();i++) //注意前面 ’&‘,很重要的优化    {        Node &tmp = e[edge[o][i]];        if(dis[tmp.to]==(dis[o]+1) && (a = dfs(tmp.to,min(f,tmp.cap-tmp.flow),t))>0)        {            tmp.flow+=a;            e[edge[o][i]^1].flow-=a; //存图方式            ans+=a;            f-=a;            if(f==0)  //注意优化            break;        }    }    return ans;  //优化}int dinci(int s,int t){    int ans=0;    while(bfs(s,t))    {        memset(cur,0,sizeof(cur));        int tm=dfs(s,inf,t);        ans+=tm;    }    return ans;}int mp[250][250];struct Node1{    int x,y,z;};Node1 raw[N],col[N];int solve(int raw_cnt,int id,int s){    int cnt = 0,pos = id+raw_cnt;    return e[edge[pos][1]].flow+1;}int main(){    while(~scanf("%d%d",&n,&m))    {        memset(mp,-1,sizeof(mp));        memset(col,0,sizeof(col));        memset(raw,0,sizeof(raw));        int cnt=0,raw_cnt=0,col_cnt=0;        for(int i=1;i<=n;i++)        {            for(int j=1;j<=m;j++)            {                string str;                cin>>str;                if(str[0] == '.')                mp[i][j] = ++cnt;else{                    mp[i][j] = -1;                    if(str[0] != 'X'){                        int tmp = (str[0]-'0')*100 + (str[1]-'0')*10 + str[2]-'0';                        col[++col_cnt].x = i;                        col[col_cnt].y = j;                        col[col_cnt].z = tmp;                    }                    if(str[4] != 'X'){                        int tmp = (str[4]-'0')*100 + (str[5]-'0')*10 + str[6]-'0';                        raw[++raw_cnt].x = i;                        raw[raw_cnt].y = j;                        raw[raw_cnt].z = tmp;                    }                }            }        }        int start=0,t=col_cnt+cnt+raw_cnt+2;        for(int i=1; i<=raw_cnt; i++){            int x = raw[i].x;            int y = raw[i].y;            int cnt_len = 0;            for(y=y+1; y<=m; y++){                if(mp[x][y] != -1){                    cnt_len++;                    add_Node(i,raw_cnt+mp[x][y],8);                }                else break;            }            add_Node(start,i,raw[i].z-cnt_len);        }        for(int i = 1;i <= col_cnt;i++){            int x = col[i].x;            int y = col[i].y;            int cnt_len = 0;            for(x = x+1;x <= n;x++){                if(mp[x][y] != -1){                    cnt_len++;                    add_Node(raw_cnt+mp[x][y],raw_cnt+cnt+i,8);                }                else break;            }            add_Node(raw_cnt+cnt+i,t,col[i].z-cnt_len);        }        int ans=dinci(start,t);        for(int i=1;i<=n;i++)        {            for(int j=1;j<=m;j++)            {                if(mp[i][j]==-1)                    printf("_");                else                    printf("%d",solve(raw_cnt,mp[i][j],cnt));                printf("%c",j==m?'\n':' ');            }        }        for(int i=0;i<=t;i++)        edge[i].clear();        e.clear();    }    return 0;}



0 0