Wisconsin Squares [USACO]

这个也不记得了,反正不难

 

/*
ID: zhangyc1
LANG: C++
TASK: wissqu
*/
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
using namespace std;

char arrMatrix[4][5];
bool arrNewCow[4][4], bFound = false;
int arrCowNum[5] = {3, 3, 3, 4, 3};
int nValid = 0;
struct SRes 
{
    char ch;
    int nRow, nCol;
};
SRes arrRes[16];

void prepairData()
{
    for (int i = 0; i < 4; i++)
    {
        scanf("%s", arrMatrix[i]);
    }
    memset(arrNewCow, 0, sizeof(arrNewCow));
}

bool CheckPut(char ch, int nRow, int nCol)
{
    int nTop = nRow > 0 ? nRow - 1 : 0;
    int nBot = nRow < 3 ? nRow + 1 : 3;
    int nLft = nCol > 0 ? nCol - 1 : 0;
    int nRht = nCol < 3 ? nCol + 1 : 3;
    for (int i = nTop; i <= nBot; i++)
    {
        for (int j = nLft; j <= nRht; j++)
        {
            if (arrMatrix[i][j] == ch)
                return false;
        }
    }
    return true;
}

void dfs(char ch, int nDepth, int nRow, int nCol)
{
    arrRes[nDepth].ch = ch;
    arrRes[nDepth].nRow = nRow;
    arrRes[nDepth].nCol = nCol;
    char chOld = arrMatrix[nRow][nCol];
    arrMatrix[nRow][nCol] = ch;
    arrNewCow[nRow][nCol] = true;
    arrCowNum[ch - 'A']--;

    if (nDepth == 15)
    {
        if (!bFound)
        {
            bFound = true;
            for (int i = 0; i < 16; i++)
                printf("%c %d %d\n", arrRes[i].ch, arrRes[i].nRow + 1, arrRes[i].nCol + 1);
        }
        nValid++;
    }
    else
    {
        for (int k = 0; k < 5; k++)
        {
            if (arrCowNum[k] > 0)
            {
                char chCur = k + 'A';
                for (int i = 0; i < 4; i++)
                {
                    for (int j = 0; j < 4; j++)
                    {
                        if (!arrNewCow[i][j] && CheckPut(chCur, i, j))
                            dfs(chCur, nDepth + 1, i, j);
                    }
                }
            }
        }
    }
    
    arrMatrix[nRow][nCol] = chOld;
    arrNewCow[nRow][nCol] = false;
    arrCowNum[ch - 'A']++;
}


void process()
{
    for (int i = 0; i < 4; i++)
    {
        for (int j = 0; j < 4; j++)
        {
            if (!arrNewCow[i][j] && CheckPut('D', i, j))
                dfs('D', 0, i, j);
        }
    }
    printf("%d\n", nValid);
}

int main(){
    FILE *streamIn = freopen("wissqu.in","r",stdin);
    FILE *streamOut = freopen("wissqu.out","w",stdout);
    prepairData();
    process();
    fclose(streamIn);
    fclose(streamOut);
    return 0;
}

 

posted @ 2013-05-09 12:08  J.Z's World  阅读(238)  评论(0编辑  收藏  举报