ACM-ICPC North America Qualifier Contest 2018 L. Superdoku

topic:

Alice and Bob are big fans of math. In particular, they are very excited about playing games that are related to numbers. Whenever they see a puzzle like Sudoku, they cannot stop themselves from solving it. The objective of Sudoku is to fill a 9×9 grid with digits so that each column, each row, and each of the nine (3×3) subgrids that compose the grid (also called "boxes", "blocks", or "regions") contains all of the digits from 1 to 9. The puzzle setter provides a partially completed grid, which for a well-posed puzzle has a single solution.

After many years of solving Sudoku problems, Alice and Bob are tired of Sudoku. They have been trying to develop a harder variation of Sudoku, which they are calling Superdoku. In Superdoku, the grid is bigger –n×n instead of just 9×9. However, the "block" constraints are impossible to formulate when there are no further constraints on n. Therefore, there are no block constraints in Superdoku. Instead, the goal is simply to make sure that each column and each row in the grid contains all of the integers from 1 to n. After playing for a while in the standard way (where any of the grid cells may have previously been filled in), they decide that the game is too difficult and they want to simplify it. Therefore, they decide to make the initial grid further constrained. They constrain the board by filling in the first k rows completely.

Alice and Bob both believe that Superdoku is solvable. However, since nn could be very big, it may still take a long time to figure out a solution. They don't want to spend too much time on this single game, so they are asking for your help!

Input
The input consists of a single test case. The first line lists two space-separated integers 1≤n≤100and 0≤k≤n, denoting the size of the grid (n×n) and the number of rows k that are already filled in. Each of the following kk lines contains nn space-separated integers, denoting the first kk given rows. Allintegers in these kk lines are between 1 and n.

Output
Output either "yes" or "no" on the first line, indicating if there is a solution. If there is no solution, do not output anything more. If there is a solution, output nn more lines, each containing n space-separated integers, representing a solution. If there are multiple solutions, output any one of them.

The only answer to this question does not meet the requirements of the answers are correct

Sample Input 1
. 4 2
. 1 2. 3. 4
2. 4. 3. 1
sample output. 1
Yes
. 1 2. 3. 4
2. 4. 3. 1
. 3. 1. 4 2
. 4. 3. 1 2
Sample input 2
. 4 2
. 1. 4. 3 2
2 2 2 2
comp output Example 2
NO

Meaning of the questions: Create a new Sudoku way to give a n * n Sudoku, each row is number 1 ~ n, and each column is the number of 1 ~ n, k gives the first line, asked if they could do this Sudoku can output any of a complete Sudoku, can not output "no".

 

Ideas: first determine the feasibility of the first k rows, and the number of lines per tag number, where the number of columns appears (the next can not appear), the remaining k + 1 ~ n of the rows in each row is 1 ~ n appears once (in fact, it is now can not consider the number of tags for each row). Bipartite graph matching, while bipartite graph is the number of 1 ~ n, and the other side is 1 ~ n columns. Hungarian algorithm to the long run, as long as a little change determination condition.

 

Code:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
#define inf 0x3f3f3f3f 
using namespace std;
typedef long long ll;
const int maxn=150;
bool he[150][150],li[150][150];
int g[150][150],used[150],mat[150],n,k,flag;
//the number j-th column G [i] [j] recorded i-th row, he [i] [j] is a j-th row if the number i appears.
 // Li [i] [j] is the j-th column number i appears . MAT [i] is the i th column of the digital matched 
BOOL DFS ( int X, int U) // Hungarian algorithm 
{ // X is the row number, u is a number 
    for ( int Y = . 1 ; Y <= n-; Y ++ ) 
    { 
        IF (! Used [Y] && Li [U] [Y] == 0 ) // this column had no U 
        { 
            Used [Y] = . 1 ; 
             IF (MAT [Y] || DFS (X, MAT [Y! ])) // match 
            { 
                Li [MAT [Y]] [Y] = 0 ;// match the previous row cancellation 
                Li [U] [Y] = . 1 ; // Mark the column to U 
                G [X] [Y] = U; 
                MAT [Y] = U;
                 return  to true ; 
            } 
        } 
    } 
    return  to false ; 
} 

int main () 
{ 
    In Flag = 0 ; 
    Memset (of He, 0 , the sizeof (of He)); 
    Memset (Li, 0 , the sizeof (Li)); 
    Memset (G, 0 , the sizeof (G));
    scanf("%d%d",&n,&k);
    for(int i=1;i<=k;i++)
    {
        for(int j=1;j<=n;j++)
        {
            scanf("%d",&g[i][j]);
            if(he[g[i][j]][i]||li[g[i][j]][j])//给出的k行不符合,标记 
                flag=1;
            he[g[i][j]][i]=1;
            li[g[i][j]][j]=1;
        }
    } 
    IF (In Flag) 
        the printf ( " NO \ n- " );
     the else 
    { 
        for ( int I = K + . 1 ; I <= n-; I ++) // for each row run again bipartite graph matching 
        { 
            Memset (MAT, 0 , the sizeof (MAT ));
             for ( int U = . 1 ; U <= n-; U ++ ) 
            { 
                Memset (Used, 0 , the sizeof (Used));
                 IF ! (DFS (I, U)) // not match mark, if there is no use, because the first k rows that match, there must be an answer 
                { 
                    Flag = 1;
                    break;
                }        
            }
            if(flag)
                break;    
        }
        if(flag)
            printf("no\n");
        else
        {
            printf("yes\n");
            for(int i=1;i<=n;i++)
            {
                printf("%d",g[i][1]);
                for(int j=2;j<=n;j++)
                    printf(" %d",g[i][j]);
                printf("\n");    
            }    
        } 
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/xiongtao/p/11264294.html