猫史档案馆


sodoku

用户:YorushikaYorushika查看:0 回复:2 评论:0 创建时间:2020-06-04T17:49:25


我这段数独代码哪里错了?!

原题:luogu.org/problem/P1784

#include <bits/stdc++.h>
#define init(array) memset(array,0,sizeof(array))
#define mal(array,type,size) type*array=(type*)malloc(sizeof(type)*size);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf = 0x3f3f3f3f;
int board[10][10] = {{0}};
struct triplet{
	int r, f, n;
	triplet(int rr, int ff, int nn){
	    r = rr;
	    f = ff;
	    n = nn;
	}
	triplet(const triplet &t){
	    this->r = t.r;
	    this->f = t.f;
	    this->n = t.n;
	}
};
void readBoard(){
  	string a;
  	for (int i = 1; i <= 9; i++){
      	getline(cin, a);
      	for (int j = 0; j < 9; j++) board[i][j + 1] = a[j] - '0';
  	}
}
void showCurrent(){
	cout << endl;
  	for (int i = 1; i <= 9; i++){
  	    for (int j = 1; j <= 9; j++) cout << board[i][j];
  	    cout << endl;
  	}
  	cout << endl;
}
bool check(int r, int f, int n){
  	for (int i = 1; i <= 9; i++){
   	 	if (board[r][i] == n) return false;
   	 	if (board[i][f] == n) return false;
   	 	if (board[3 * ((r - 1) / 3) + ((i - 1) / 3) + 1][3 * ((f - 1) / 3) + ((i - 1) % 3) + 1] == n) return false;
 	}
  	return true;
}
bool findNext(stack<triplet>& s){
	int rr = s.top().r, ff = s.top().f;
	if (rr > 9 || ff > 9) return false;
	while (true){
		if (ff == 9){
		    rr++;
		    ff = 1;
		}
		else ff++;
		if (rr > 9 || ff > 9) return false;
		if (board[rr][ff] > 0) continue;
		for (int i = 1; i <= 9; i++){
			if (check(rr, ff, i)){
				s.push(triplet(rr, ff, i));
				board[rr][ff] = i;
				return true;
			}
		}
		triplet temp(s.top());
		while (true){
			if (s.empty()) return false;
			if (temp.n == 9) {
				board[temp.r][temp.f] = 0;
				s.pop();
				temp = s.top();
				continue;
			}
			if (check(temp.r, temp.f, temp.n++)){
				s.pop();
				s.push(temp);
				board[temp.r][temp.f] = temp.n;
				return true;
			}
		}
		rr = s.top().r;
		ff = s.top().f;
	}
	return true;
}
int main(int argc, char * argv[]) {
	readBoard();	
	stack<triplet> s;
	s.push(triplet(1, 0, 0));
	while (findNext(s)) ;
	showCurrent();	
    return 0;
}

请各位C++大佬帮忙看一看,谢谢!


回复

上一页1 页 / 共 1下一页
开朗de网友开朗de网友

看不懂emotion_编程猫_溜了溜了

点赞0


评论


YorushikaYorushika

呵呵,C++的都看不懂

点赞0


评论