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

请求各位大牛帮忙看下这道题的错误,查了很久,查不出来,郁闷啊

Posted by forgotaboutdre at 2011-01-24 18:59:05 on Problem 2676
代码如下
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

int map[9][9];    /*输入数组*/
bool row[9][9];     /*row[i][j]记录第i行出现j这个数*/
bool lie[9][9];     /*lie[i][j]记录第i列出现j
bool fangge[9][9];   /*记录map[i][j]所在3x3小方格出现的数*/
int DFS(int r, int c);   

int main() {
    int N;
    int i, j;
    char c;
    cin >> N;
    
    while (N--) {
    memset(map, 0, sizeof (map));
    memset(row, 0, sizeof (row));
    memset(lie, 0, sizeof (lie));
    memset(fangge, 0, sizeof (fangge));
	for (i = 0; i < 9; i++)
	{
	    for (j = 0; j < 9; j++) {
		cin >> c;
		map[i][j] = int(c - '0');
		if (map[i][j] != 0) {
		    row[i][map[i][j]] = 1;
		    lie[j][map[i][j]] = 1;
		    fangge[i/3*3+j/3][map[i][j]]=1;
		}
	    }
}
	DFS(0, 0);
	for (i =0;i<9;i++) {
	    for (j=0;j<9;j++)
		printf("%d", map[i][j]);
	    printf("\n");
	}
    }
    return 0;
}

int DFS(int r, int c) {
    if (r == 9) {
	return 1;
    }
    if (map[r][c]!=0){
	if (c==8)
	    return DFS(r+1,0);
	else
	    return DFS(r,c+1);
    }
    

    else {
	for (int i = 0; i <= 9; i++) {
	    if (row[r][i]==0&&lie[c][i]==0&&fangge[r/3*3+c/3][i]==0) {
        map[r][c]=i;
		row[r][i]=1;
		lie[c][i]=1;
		fangge[r/3*3+c/3][i]=1;

		if (c == 8) {
		    if (DFS(r + 1, 0) == 1)
			return 1;

		    else {
			map[r][c] = 0;
			row[r][i] = 0;
			lie[c][i] = 0;
			fangge[r/3*3+c/3][i]=0;
		    }
		} else {
		    if (DFS(r, c + 1) == 1)
			return 1;
		    else {
			map[r][c] = 0;
			row[r][i] = 0;
			lie[c][i] = 0;
			fangge[r/3*3+c/3][i]=0;
		    }
		}
	    }
	}
    }
    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