CDOJ 1222 Sudoku【暴力】

题目链接:

http://acm.uestc.edu.cn/#/problem/show/1222

题意:

给定矩阵,把空位置填上1234使得四个2*2小矩阵和4行4列均恰有1.2.3.4

分析:

数据那么小,暴力不能怂。。。

代码:

/* --I AM SUPER Robbish --Created by jiangyuzhu --2016/5/19 */
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
#define pr(x) cout << #x << ": " << x << " "
#define pl(x) cout << #x << ": " << x << endl;
#define sa(x) scanf("%d",&(x))
#define sal(x) scanf("%lld",&(x))
#define mdzz cout<<"mdzz"<<endl;
const int maxn = 1e5 + 5, maxm = 5;
int a[maxn][maxm];
char b[maxn][maxm];
int vis[maxm];
int main (void)
{
    int t;sa(t);
    int c = 1;
    int cnt;
    while(t--){
        for(int i = 0; i < 4; i++){
            scanf("%s", b[i]);
        }
        int tot = 0;
        for(int i = 0; i < 4; i++){
            for(int j = 0; j < 4; j++){
                if(b[i][j] == '*') {a[i][j] = 0; tot++;}
                else a[i][j] = b[i][j] - '0';
            }
        }
        int ans;
        int x, y;
        while(tot){
            for(int i = 0; i < 4; i++){
                for(int j = 0; j < 4; j++){
                    if(!a[i][j]){
                        memset(vis, 0, sizeof(vis));
                        cnt = 0;
                        for(int k = 0; k < 4; k++){
                            vis[a[i][k]] = 1;
                            vis[a[k][j]] = 1;
                        }
                         x = i / 2 * 2;
                         y = j / 2 * 2;
                         for(int k = x; k <= x + 1; k++){
                            for(int m = y; m <= y + 1; m++){
                                vis[a[k][m]] = 1;
                            }
                        }
                        for(int k = 1; k <= 4; k++){
                            if(!vis[k]){cnt++; ans = k;}
                        }
                        if(cnt == 1) {
                                a[i][j] = ans;
                                tot--;
                        }
                    }
                }
            }
        }
       printf("Case #%d:\n", c++);
       for(int i = 0; i < 4; i++){
            for(int j = 0; j < 4; j++){
                printf("%d", a[i][j]);
            }
            printf("\n");
       }
    }
    return 0;
}
/* 3 **** 2341 4123 3214 *243 *312 *421 *134 *41* **3* 2*41 4*2* */

你可能感兴趣的:(CDOJ 1222 Sudoku【暴力】)