ACM学习历程—UESTC 1222 Sudoku(矩阵)(2015CCPC H)

题目链接:http://acm.uestc.edu.cn/#/problem/show/1226

题目大意就是构造一个行列和每个角的2*2都是1234的4*4矩阵。

用dfs暴力搜索,不过需要每一步进行判断是否已经出现了重复,如果最后再判断的话复杂度有点高。

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <string>
#define LL long long

using namespace std;

char str[5][5];
int a[5][5];
bool vis[5];
bool flag;

bool judge(int x[][5])
{
    for (int i = 0; i < 4; ++i)
    {
        memset(vis, false, sizeof(vis));
        for (int j = 0; j < 4; ++j)
        {
            if (!x[i][j]) continue;
            if (vis[x[i][j]]) return false;
            else vis[x[i][j]] = true;
        }
    }
    for (int j = 0; j < 4; ++j)
    {
        memset(vis, false, sizeof(vis));
        for (int i = 0; i < 4; ++i)
        {
            if (!x[i][j]) continue;
            if (vis[x[i][j]]) return false;
            else vis[x[i][j]] = true;
        }
    }
    for (int i = 0; i <= 2; i += 2)
    {
        for (int j = 0; j <= 2; j += 2)
        {
            memset(vis, false, sizeof(vis));
            for (int xx = 0; xx <= 1; xx++)
            {
                for (int yy = 0; yy <= 1; yy++)
                {
                    if (!x[i+xx][j+yy]) continue;
                    if (vis[x[i+xx][j+yy]]) return false;
                    else vis[x[i+xx][j+yy]] = true;
                }
            }
        }
    }
    return true;
}

void input()
{
    for (int i = 0; i < 4; ++i)
    {
        scanf("%s", str[i]);
        for (int j = 0; j < 4; ++j)
        {
            if (str[i][j] == ‘*‘)
                a[i][j] = 0;
            else
                a[i][j] = str[i][j]-‘0‘;
        }
    }
}

void dfs(int i, int j)
{
    if (flag) return;
    i += j/4;
    j %= 4;
    if (i == 4)
    {
        if (judge(a))
        {
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                    printf("%d", a[i][j]);
                printf("\n");
            }
            flag = true;
        }
        return;
    }
    if (!judge(a)) return;
    if (!a[i][j])
    {
        for (int x = 1; x <= 4; ++x)
        {
            a[i][j] = x;
            dfs(i, j+1);
            a[i][j] = 0;
        }
    }
    else dfs(i, j+1);
}

void work()
{
    flag = false;
    dfs(0, 0);
}

int main()
{
    //freopen("test.in", "r", stdin);
    int T;
    scanf("%d", &T);
    for (int times = 1; times <= T; ++times)
    {
        printf("Case #%d:\n", times);
        input();
        work();
    }
    return 0;
}

时间: 2024-10-19 21:00:11

ACM学习历程—UESTC 1222 Sudoku(矩阵)(2015CCPC H)的相关文章

ACM学习历程—UESTC 1226 Huatuo&#39;s Medicine(数学)(2015CCPC L)

题目链接:http://acm.uestc.edu.cn/#/problem/show/1226 题目就是构造一个对称的串,除了中间的那个只有1个,其余的两边都是对称的两个,自然答案就是2*n-1. 代码: #include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <algorithm> #

ACM学习历程—UESTC 1218 Pick The Sticks(动态规划)(2015CCPC D)

题目链接:http://acm.uestc.edu.cn/#/problem/show/1218 题目大意就是求n根木棒能不能放进一个容器里,乍一看像01背包,但是容器的两端可以溢出容器,只要两端的木棒的重心还在容器中即可. 首先由于木棒可以两端溢出.一端溢出和不溢出三种情况,所以有状态p(flag, v)表示溢出个数为flag的容量为v的情况下的最值. 于是有: p[2][j] = max(p[2][j], p[2][j-a[i]]+v[i]); p[2][j] = max(p[2][j], 

ACM学习历程—HDU 4726 Kia&#39;s Calculation( 贪心&amp;&amp;计数排序)

DescriptionDoctor Ghee is teaching Kia how to calculate the sum of two integers. But Kia is so careless and alway forget to carry a number when the sum of two digits exceeds 9. For example, when she calculates 4567+5789, she will get 9246, and for 12

ACM学习历程—HDU 5023 A Corrupt Mayor&#39;s Performance Art(广州赛区网赛)(线段树)

Problem Description Corrupt governors always find ways to get dirty money. Paint something, then sell the worthless painting at a high price to someone who wants to bribe him/her on an auction, this seemed a safe way for mayor X to make money. Becaus

ACM学习历程——HDU5015 233 Matrix(矩阵快速幂)(2014陕西网赛)

Description In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 23333, or 233333 ... in the same meaning. And here is the question: Suppose we have a matrix called 233 matrix. In the first line, it would be 233, 233

ACM学习历程—HDU 3949 XOR(xor高斯消元)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3949 题目大意是给n个数,然后随便取几个数求xor和,求第k小的.(重复不计算) 首先想把所有xor的值都求出来,对于这个规模的n是不可行的. 然后之前有过类似的题,求最大的,有一种方法用到了线性基. 那么线性基能不能表示第k大的呢? 显然,因为线性基可以不重复的表示所有结果.它和原数组是等价的. 对于一个满秩矩阵 100000 010000 001000 000100 000010 000001

UESTC - 1222 Sudoku

Yi Sima was one of the best counselors of Cao Cao. He likes to play a funny game himself. It looks like the modern Sudoku, but smaller. Actually, Yi Sima was playing it different. First of all, he tried to generate a 4×44×4 board with every row conta

ACM学习历程—BestCoder Round #75

1001:King's Cake(数论) http://acm.hdu.edu.cn/showproblem.php?pid=5640 这题有点辗转相除的意思.基本没有什么坑点. 代码: #include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <algorithm> #include &l

ACM学习历程—HDU5585 Numbers(数论 || 大数)(BestCoder Round #64 (div.2) 1001)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5585 题目大意就是求大数是否能被2,3,5整除. 我直接上了Java大数,不过可以对末尾来判断2和5,对所有位的和来判断3. 代码就不粘了.