/**
* 精确覆盖问题:由0和1组成的矩阵,从中选出若干行,使得每列恰好只包含一个1 解决思路: 当所有列都已包含,终止,否则: 选定一列c(确定)
* 选择一行r,使得A[r][c]==1(随机) 将该行包含进解集 对于该行中任意列c1,若满足A[r][c1]==1,则:
* 删除c1,且对于所有满足A[r1][c1]的行r1,删除r1 循环直至满足终止条件
*
*/
public class ExactCover {
public static boolean useRow[];
public static boolean useCol[];
public static int rowCount = 0;
public static int colCount = 0;
public static void main(String args[]){
int a[][] = {{0,0,1,0,1,1,0},
{1,0,0,1,0,0,1},
{0,1,1,0,0,1,0},
{1,0,0,1,0,0,0},
{0,1,0,0,0,0,1},
{0,1,1,0,1,1,0}};
printExactCover(a, 6, 7);
}
public static void printExactCover(int a[][], int rowSize, int colSize ){
rowCount = rowSize;
colCount = colSize;
useRow = new boolean[rowSize];
for(int i=0; i<rowSize; i++)
useRow[i] = false;
useCol = new boolean[colSize];
for(int j=0; j<colSize; j++ )
useCol[j] = false;
int mark[] = new int[rowSize];
for(int i=0; i<rowSize; i++ )
mark[i] = -1;
findExactCover(a,rowSize,colSize,mark,0,0);
}
public static void printMark( int mark[], int markIndex ){
for(int i=0; i<markIndex && mark[i]!=-1 ; i++ ){
System.out.print(mark[i]+" ");
}
System.out.println();
}
public static void findExactCover(int a[][], int rowSize, int colSize, int mark[], int markIndex, int col){
if( colCount==0 ){//succeed
printMark(mark,markIndex);
return;
}
if( rowCount==0 ){//failed
return;
}
while( useCol[col] ){
col++;
}
for( int i=0; i<rowSize; i++ ){
int markRow[]= new int [rowSize];
int markCol[]= new int [colSize];
int markRowSize = 0;
int markColSize = 0;
if( !useRow[i] && a[i][col]==1 ){
mark[markIndex] = i;
useRow[i] = true;
rowCount--;
markRow[markRowSize++] = i;
for( int j=0; j<colSize; j++ ){
if( a[i][j]==1 && !useCol[j] ){
useCol[j] = true;
colCount--;
markCol[markColSize++] = j;
for( int ui = 0; ui<rowSize; ui++ ){
if( a[ui][j]==1 && !useRow[ui] ){
useRow[ui] = true;
rowCount--;
markRow[markRowSize++] = ui;
}
}
}
}
findExactCover(a, rowSize, colSize, mark, markIndex+1,col+1);
while(markRowSize>0){
int index = markRow[--markRowSize];
useRow[index] = false;
rowCount++;
}
while(markColSize>0){
int index = markCol[--markColSize];
useCol[index] = false;
colCount++;
}
}
}
}
}
* 精确覆盖问题:由0和1组成的矩阵,从中选出若干行,使得每列恰好只包含一个1 解决思路: 当所有列都已包含,终止,否则: 选定一列c(确定)
* 选择一行r,使得A[r][c]==1(随机) 将该行包含进解集 对于该行中任意列c1,若满足A[r][c1]==1,则:
* 删除c1,且对于所有满足A[r1][c1]的行r1,删除r1 循环直至满足终止条件
*
*/
public class ExactCover {
public static boolean useRow[];
public static boolean useCol[];
public static int rowCount = 0;
public static int colCount = 0;
public static void main(String args[]){
int a[][] = {{0,0,1,0,1,1,0},
{1,0,0,1,0,0,1},
{0,1,1,0,0,1,0},
{1,0,0,1,0,0,0},
{0,1,0,0,0,0,1},
{0,1,1,0,1,1,0}};
printExactCover(a, 6, 7);
}
public static void printExactCover(int a[][], int rowSize, int colSize ){
rowCount = rowSize;
colCount = colSize;
useRow = new boolean[rowSize];
for(int i=0; i<rowSize; i++)
useRow[i] = false;
useCol = new boolean[colSize];
for(int j=0; j<colSize; j++ )
useCol[j] = false;
int mark[] = new int[rowSize];
for(int i=0; i<rowSize; i++ )
mark[i] = -1;
findExactCover(a,rowSize,colSize,mark,0,0);
}
public static void printMark( int mark[], int markIndex ){
for(int i=0; i<markIndex && mark[i]!=-1 ; i++ ){
System.out.print(mark[i]+" ");
}
System.out.println();
}
public static void findExactCover(int a[][], int rowSize, int colSize, int mark[], int markIndex, int col){
if( colCount==0 ){//succeed
printMark(mark,markIndex);
return;
}
if( rowCount==0 ){//failed
return;
}
while( useCol[col] ){
col++;
}
for( int i=0; i<rowSize; i++ ){
int markRow[]= new int [rowSize];
int markCol[]= new int [colSize];
int markRowSize = 0;
int markColSize = 0;
if( !useRow[i] && a[i][col]==1 ){
mark[markIndex] = i;
useRow[i] = true;
rowCount--;
markRow[markRowSize++] = i;
for( int j=0; j<colSize; j++ ){
if( a[i][j]==1 && !useCol[j] ){
useCol[j] = true;
colCount--;
markCol[markColSize++] = j;
for( int ui = 0; ui<rowSize; ui++ ){
if( a[ui][j]==1 && !useRow[ui] ){
useRow[ui] = true;
rowCount--;
markRow[markRowSize++] = ui;
}
}
}
}
findExactCover(a, rowSize, colSize, mark, markIndex+1,col+1);
while(markRowSize>0){
int index = markRow[--markRowSize];
useRow[index] = false;
rowCount++;
}
while(markColSize>0){
int index = markCol[--markColSize];
useCol[index] = false;
colCount++;
}
}
}
}
}