Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Re:花了几个小时,思路一直都很清晰,想不明白错在哪里....最后才发现,是初始化输入出问题了...

Posted by 514555138 at 2015-06-27 20:41:37 on Problem 2676
In Reply To:花了几个小时,思路一直都很清晰,想不明白错在哪里....最后才发现,是初始化输入出问题了... Posted by:inowfordream at 2009-07-28 16:17:14
#include <iostream>
#include <string>
#define MAXN 9

using namespace std;

int map[MAXN][MAXN];
bool col[MAXN][MAXN], row[MAXN][MAXN], block[MAXN][MAXN];
int C;

bool Fill(int n, int m, int num)
{
    int b_col = m / 3 * 3, b_row = n / 3 * 3, b_num = b_row / 3 * 3 + b_col / 3;

    if (block[b_num][num] || row[n][num] || col[m][num]) return false;
    map[n][m] = num;
    block[b_num][num] = row[n][num] = col[m][num] = true;
    return true;
}

void Undo(int n, int m, int num)
{
    int b_col = m / 3 * 3, b_row = n / 3 * 3, b_num = b_row / 3 * 3 + b_col / 3;

    block[b_num][num] = row[n][num] = col[m][num] = false;
    map[n][m] = 0;
}

bool Full()
{
    for (int i = 0; i < MAXN; i++)
        for (int j = 0; j < MAXN; j++)
            if (map[i][j] == 0) return false;
    return true;
}

void Print()
{
    for (int i = 0; i < MAXN; i++) {
        for (int j = 0; j < MAXN; j++)
            cout << map[i][j];
        cout << "\n";
    }
}


void DFS(int n, int m)
{
    if (m < 0) { n -= 1; m = 8; }

    if (Full() || n < 0) {
        if (C == 0) Print();
        C++;
        return;
    }

    if (map[n][m] != 0) {
        DFS(n, m - 1);
    } else {
        for (int k = 1; k <= 9; k++)
            if (Fill(n, m, k)) {
                DFS(n, m - 1);
                Undo(n, m, k);
            }
    }
}

void Solve()
{
    char tmp;
    string s;

    for (int i = 0; i < MAXN; i++) {
        for (int j = 0; j < MAXN; j++) {
            map[i][j] = 0; 
            col[i][j] = false; 
            row[i][j] = false;
            row[i][j] = false;
        }
    }
    for (int i = 0; i < MAXN; i++)  {
        cin >> s;
        for (int j = 0; j < s.length(); j++) {
            if (s[j] != '0') Fill(i, j, s[j] - '0');
        }
    }

    C = 0;
    DFS(8, 8);
}


int main()
{
    int T;
    ios::sync_with_stdio(false);

    cin >> T;
    for (int i = 0; i < T; i++) Solve();
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator