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

正搜1400ms 分享代码

Posted by 2757585379 at 2017-07-30 15:45:47 on Problem 2676
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>

using namespace std;

string csudo[9];
int sudo[9][9];
int flag;

int check(int n, int x, int y){
	for(int i=0; i<9; i++){
		if(sudo[x][i] == n) return 0;
		if(sudo[i][y] == n) return 0;
	}
	int a=x/3*3;
	int b=y/3*3;
	for(int i=a; i<(a+3); i++){
		for(int j=b;j<(b+3); j++){
			if(sudo[i][j] == n) return 0;
		}
	}
	return 1;
}

void dfs(int x, int y){
	if(x==9){
		flag=1;
		return;
	}

	int nx = x+(y+1)/9;
	int ny = (y+1)%9;
	if(sudo[x][y] != 0){
		dfs(nx,ny);
	}else{
		for(int i=1; i<=9; i++){
			if(check(i,x,y)){
				sudo[x][y]=i;
				dfs(nx,ny);
				if(flag) return;
				sudo[x][y]=0;
			}
		}
	}
}

void printsudo(){
	for(int i=0; i<9; i++){
		for(int j=0; j<9; j++){
			cout<<sudo[i][j];
			if(j==8)
				cout<<endl;
		}
	}
}

int main(){
	//freopen ("in.txt" , "r" , stdin);
	//freopen ("out.txt" , "w" , stdout);	

	int icase;
	cin>>icase;
	while(icase--){
		for(int i=0; i<9; i++){
			cin>>csudo[i];
		}

		for(int i=0; i<9; i++){
			for(int j=0; j<9; j++){
				sudo[i][j] = csudo[i][j]-'0';
			}
		}

		flag=0;
		dfs(0,0);	
		printsudo();
	}
	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