POJ 3074

同上题

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string.h>
#include <vector>
using namespace std;

int puzzle[10][10];
char tmp[100];
const int SLOT=0;
const int ROW=1;
const int COL=2;
const int SUB=3;
const int maxn=400;
const int maxnode=9*9*9*9*9*4+100;
const int maxr=9*9*9*9*9*4+100;

struct DLX
{
  int n , sz;                                                 // 行数,节点总数
  int S[maxn];                                                // 各列节点总数
  int row[maxnode],col[maxnode];                              // 各节点行列编号
  int L[maxnode],R[maxnode],U[maxnode],D[maxnode];            // 十字链表

  int ansd,ans[maxn];                                         // 解

  void init(int n )
  {
    this->n = n ;
    for(int i = 0 ; i <= n; i++ )
    {
      U[i] = i ;
      D[i] = i ;
      L[i] = i - 1;
      R[i] = i + 1;
    }
    R[n] = 0 ;
    L[0] = n;
    sz = n + 1 ;
    memset(S,0,sizeof(S));
  }
  void addRow(int r,vector<int> c1)
  {
    int first = sz;
    for(int i = 0 ; i < c1.size(); i++ ){
      int c = c1[i];
      L[sz] = sz - 1 ; R[sz] = sz + 1 ; D[sz] = c ; U[sz] = U[c];
      D[U[c]] = sz; U[c] = sz;
      row[sz] = r; col[sz] = c;
      S[c] ++ ; sz ++ ;
    }
    R[sz - 1] = first ; L[first] = sz - 1;
  }
  // 顺着链表A,遍历除s外的其他元素
  #define FOR(i,A,s) for(int i = A[s]; i != s ; i = A[i])

  void remove(int c){
    L[R[c]] = L[c];
    R[L[c]] = R[c];
    FOR(i,D,c)
      FOR(j,R,i) {U[D[j]] = U[j];D[U[j]] = D[j];--S[col[j]];}
  }
  void restore(int c){
    FOR(i,U,c)
      FOR(j,L,i) {++S[col[j]];U[D[j]] = j;D[U[j]] = j; }
    L[R[c]] = c;
    R[L[c]] = c;
  }
  bool dfs(int d){
    if(R[0] == 0 ){
      ansd = d;
      return true;
    }
    // 找S最小的列c
    int c = R[0] ;
    FOR(i,R,0) if(S[i] < S[c]) c = i;

    remove(c);
    FOR(i,D,c){
      ans[d] = row[i];
      FOR(j,R,i) remove(col[j]);
      if(dfs(d + 1)) return true;
      FOR(j,L,i) restore(col[j]);
    }
    restore(c);

    return false;
  }
  bool solve(vector<int> & v){
    v.clear();
    if(!dfs(0)) return false;
    for(int i = 0 ; i< ansd ;i ++ ) v.push_back(ans[i]);
    return true;
  }
};
DLX slover;

int encode(int a,int b,int c){
	return a*81+b*9+c+1;
}

void decode(int code,int &a,int &b,int &c){
	code--;
	c=code%9; code/=9;
	b=code%9; code/=9;
	a=code;
}

bool read(){
	scanf("%s",tmp);
	if(strcmp(tmp,"end")==0) return false;
	for(int i=0;i<9;i++){
		for(int j=0;j<9;j++){
			if(tmp[i*9+j]==‘.‘)
			puzzle[i][j]=0;
			else	puzzle[i][j]=tmp[i*9+j]-‘0‘;
		}
	}
	return true;
}

int main(){
	while(read()){
		slover.init(324);
		for(int r=0;r<9;r++){
			for(int c=0;c<9;c++){
				for(int v=0;v<9;v++){
					if(puzzle[r][c]==0||puzzle[r][c]==v+1){
						vector<int> columns;
						columns.push_back(encode(SLOT,r,c));
						columns.push_back(encode(ROW,r,v));
						columns.push_back(encode(COL,c,v));
						columns.push_back(encode(SUB,(r/3)*3+c/3,v));
						slover.addRow(encode(r,c,v),columns);
					}
				}
			}
		}
		vector<int>ans;
		slover.solve(ans);
		for(int i=0;i<ans.size();i++){
			int r,c,v;
			decode(ans[i],r,c,v);
			puzzle[r][c]=v+1;
		}
		for(int i=0;i<9;i++){
		for(int j=0;j<9;j++)
		printf("%d",puzzle[i][j]);
		}
		printf("\n");
	}
	return 0;
}

  

时间: 2024-08-09 14:03:41

POJ 3074的相关文章

poj 3074 Sudoku(Dancing Links)

Sudoku Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8152   Accepted: 2862 Description In the game of Sudoku, you are given a large 9 × 9 grid divided into smaller 3 × 3 subgrids. For example, . 2 7 3 8 . . 1 . . 1 . . . 6 7 3 5 . . .

poj 3074 Sudoku dlx解数独

分析: dlx是从数据结构角度优化01矩阵精确覆盖和重复覆盖的数据结构,它用十字链表只存贮矩阵中的非0元,而01矩阵精确覆盖dfs过程中矩阵会越来越稀疏而且每次恢复现场会浪费大量时间,dlx恰好能解决这两个问题.本题关键是将数独问题转化为01矩阵精确覆盖.数独转化为精确覆盖问题的方法还是参照Knuth的论文,如果读取到一个格子是空的,那么加9行,分别表示这个格子填1到9这9个数字,如果读取到的格子是一个数字,那么就加一行就可以了,然后列有9*9*4列,前81列表示这一行表示填的是第i行第j列的格

POJ 3074 Sudoku (DLX)

Sudoku Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3074 Appoint description:  System Crawler  (2015-04-18) Description In the game of Sudoku, you are given a large 9 × 9 grid divided into sm

【POJ 3074】 Sudoku

[题目链接] http://poj.org/problem?id=3074 [算法] 将数独问题转化为精确覆盖问题,用Dancing Links求解 转化方法如下 : 我们知道,在一个数独中 : 1.每个格子填且只填一个数 2.每一行填1-9这九个数 3.每一列填1-9这九个数 4.每个格子填1-9这九个数 对于第一个约束条件,我们用81列,表示是否填入 对于第二个约束条件,我们每一行用9列,表示这一行是否有1-9 第三,四个约束条件的处理方式和第二个类似 [代码] #include <algo

POJ 3074&amp;&amp;2676 数独DFS

经典数独问题 用DFS模拟数独解法,找摒除解和余数解 数独解法:http://www.sudokufans.org.cn/forums/topic/8/ 2676 #include "stdio.h" #include "string.h" struct node { int x,y; int s[10]; // 对于每个空格,数字i是否可用 int sum; // 对于每个空格,一共可以填入的数字种数 }order[101]; int cnt; // 总空格数 c

DLX (poj 3074)

题目:Sudoku 匪夷所思的方法,匪夷所思的速度!!! https://github.com/ttlast/ACM/blob/master/Dancing%20Link%20DLX/poj%203074.cpp #include <iostream> #include <cstdio> #include <cstring> using namespace std; const int inf = 100000000; int flag; typedef long lo

POJ 3074 Sudoku DLX精确覆盖

DLX精确覆盖.....模版题 Sudoku Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8336   Accepted: 2945 Description In the game of Sudoku, you are given a large 9 × 9 grid divided into smaller 3 × 3 subgrids. For example, . 2 7 3 8 . . 1 . . 1 . .

POJ 3074 Sudoku 舞蹈链

#include <cstdio> #include <iostream> #include <algorithm> #include <queue> #include <stack> #include <cstdlib> #include <cmath> #include <set> #include <map> #include <vector> #include <cstri

(简单) POJ 3074 Sudoku, DLX+精确覆盖。

Description In the game of Sudoku, you are given a large 9 × 9 grid divided into smaller 3 × 3 subgrids. For example, . 2 7 3 8 . . 1 . . 1 . . . 6 7 3 5 . . . . . . . 2 9 3 . 5 6 9 2 . 8 . . . . . . . . . . . 6 . 1 7 4 5 . 3 6 4 . . . . . . . 9 5 1