[USACO 6.4.3]Wisconsin Squares

题解

  暴搜即可.

代码

/*
TASK:wissqu
LANG:C++
*/
#include <cstdio>
#include <cstring>

using namespace std;

char matrix[8][8], path[16][10];
bool v[4][4], flag;
int ans, num[5] = {3, 3, 3, 4, 3};

bool judge(int x0, int y0, int x1, int y1, int k)
{
    for (int i = x0; i <= x1; ++i)
        for (int j = y0; j <= y1; ++j)
            if (i >= 0 && i < 4 && j >= 0 && j < 4 && matrix[i][j] == k + ‘A‘) return false;
    return true;
}

void dfs(int dep)
{
    if (dep == 16)
    {
        if (flag)
        {
            for (int i = 0; i < 16; ++i) printf("%s\n", path[i]);
            flag = false;
        }
        ans++;
        return;
    }
    for (int u = 0; u < 5; ++u)
        if (num[u])
        {
            num[u]--;
            for (int i = 0; i < 4; ++i)
                for (int j = 0; j < 4; ++j)
                    if (v[i][j] && judge(i-1, j-1, i+1, j+1, u))
                    {
                        v[i][j] = false;
                        if (flag)
                        {
                            path[dep][0] = u + ‘A‘;
                            path[dep][1] = ‘ ‘;
                            path[dep][2] = i + ‘0‘ + 1;
                            path[dep][3] = ‘ ‘;
                            path[dep][4] = j + ‘0‘ + 1;
                        }
                        char tmp = matrix[i][j];
                        matrix[i][j] = u + ‘A‘;
                        dfs(dep + 1);
                        matrix[i][j] = tmp;
                        v[i][j] = true;
                    }
            num[u]++;
        }
}

int main()
{
    freopen("wissqu.in", "r", stdin);
    freopen("wissqu.out", "w", stdout);
    for (int i = 0; i < 4; ++i) scanf("%s", matrix[i]);
    memset(v, true, sizeof(v));
    memset(path, 0, sizeof(path));
    flag = true;
    dfs(0);
    printf("%d\n", ans);
    return 0;
}
时间: 2024-10-18 09:33:55

[USACO 6.4.3]Wisconsin Squares的相关文章

USACO 6.4 Wisconsin Squares

Wisconsin Squares It's spring in Wisconsin and time to move the yearling calves to the yearling pasture and last year's yearlings to the greener pastures of the north 40. Farmer John has five kinds of cows on his farm (abbreviations are shown in pare

USACO 6.5 All Latin Squares

All Latin Squares A square arrangement of numbers 1 2 3 4 5 2 1 4 5 3 3 4 5 1 2 4 5 2 3 1 5 3 1 2 4 is a 5 x 5 Latin Square because each whole number from 1 to 5 appears once and only once in each row and column. Write a program that will compute the

【USACO 1.2】Palindromic Squares

进制转换,然后判断是否是回文 /******************************************* TASK: palsquare LANG: C++ Created Time: 2016年09月07日 星期三 21时18分46秒 *********************************/ #include<iostream> #include<cstdio> #include<cstring> #include<algorithm&

USACO6.4-Wisconsin Squares:搜索

Wisconsin Squares It's spring in Wisconsin and time to move the yearling calves to the yearling pasture and last year's yearlings to the greener pastures of the north 40. Farmer John has five kinds of cows on his farm (abbreviations are shown in pare

USACO 1.2 Palindromic Squares

Palindromic SquaresRob Kolstad Palindromes are numbers that read the same forwards as backwards. The number 12321 is a typical palindrome. Given a number base B (2 <= B <= 20 base 10), print all the integers N (1 <= N <= 300 base 10) such that

[USACO 6.5.1]All Latin Squares

题解 搜索剪枝. 置换圈个数相同及对应的置换圈内元素个数相同即视为相同方案. 代码 /* TASK:latin LANG:C++ */ #include <cstdio> #include <cstring> #include <iostream> using namespace std; int n, s[10][10], fact[] = {1, 1, 2, 6, 24, 120, 720, 5040}; bool row[10][10], col[10][10];

USACO Section1.2 Palindromic Squares 解题报告

palsquare解题报告 —— icedream61 博客园(转载请注明出处)------------------------------------------------------------------------------------------------------------------------------------------------[题目] 把1~300中,其平方在B进制下是回文数的数进行输出.每个数x输出一行,输出B进制下的x和x²,用空格隔开. 注意,10~

usaco Palindromic Squares

我会告诉你进制转换我都忘了,翻出了数字逻辑课本才想起来的. /* ID: modengd1 PROG: palsquare LANG: C++ */ #include <iostream> #include <stdio.h> #include <string.h> #include <stack> using namespace std; char leter[20]={'0','1','2','3','4','5','6','7','8','9','A'

usaco Magic Squares

各一个2x4的字符数组,有三种不同的操作,要求使用最少的操作,从“12345678”,转换成目标字符,输出最少步数,以及操作次序. 对C++类库的用法还是不够熟啊. /* ID: modengd1 PROG: msquare LANG: C++ */ #include <iostream> #include <stdio.h> #include <queue> #include <map> #include <string> using name