All pastes #2053512 Raw Edit

Untitled

public text v1 · immutable
#2053512 ·published 2011-05-03 15:49 UTC
rendered paste body
private static int[][] calculateHonogram(int[][] board){
		int numberOfChanges=1;
		while(numberOfChanges!=0){
			numberOfChanges=0;
			/*
			 * Check all possible row solutions
			 */
			for (int i=0; i<board.length; i++){
				List<int[]> rowMatches = new ArrayList<int[]>();
				for (int[] line: allSolutions){				
					if (matchPattern(line, rowPattern[i]) && matchCurrentRowSolution(line, i))
						rowMatches.add(line);
				}
						
				int[] intersection = calculateIntersection(rowMatches);
				for (int j=0; j<board[0].length; j++){
					if (board[i][j] != intersection[j]){
						board[i][j] = intersection[j];
						numberOfChanges++;
					}
				}
			}
			
			/*
			 * Check all possible column solutions
			 */
			for (int i=0; i<board.length; i++){
				List<int[]> columnMatches = new ArrayList<int[]>();
				for (int[] line: allSolutions){				
					if (matchPattern(line, columnPattern[i]) && matchCurrentColumnSolution(line, i))
						columnMatches.add(line);
				}
						
				int[] intersection = calculateIntersection(columnMatches);
				for (int j=0; j<board[0].length; j++){
					if (board[j][i]!= intersection[j]){
						board[j][i] = intersection[j];
						numberOfChanges++;
					}
				}					
			}		
		}
		if (!checkSolution(board)){
			int i=0;
			boolean solutionFound = false;
			while (i<board.length && !solutionFound){
				int j=0;
				while (j<board[0].length && !solutionFound){
					if (board[i][j]==0){
						board[i][j] = 1;
						board = calculateHonogram(board);
						if (checkSolution(board))
							solutionFound = true;
						else{
							board[i][j] = 2;
							board = calculateHonogram(board);
							if (checkSolution(board))
								solutionFound = true;
						}
					}
					j++;
				}
				i++;
			}
		}
		return board;		
	}
	
	/*
	 * Generates all possible lines with size @LineSize
	 */
	private static List<int[]> generateAllPossibleLineSolutions(int lineSize){
		if (lineSize==0)
			return null;
		List<int[]> lines = new ArrayList<int[]>();
		int[] firstPossibility = new int[]{2};
		int[] secondPossibility = new int[]{1};	
		List<int[]> subLines = generateAllPossibleLineSolutions(lineSize-1);
		if (subLines == null){
			lines.add(firstPossibility);
			lines.add(secondPossibility);
		}else{
			for (int[] subLine: subLines){
				lines.add(concatLines(firstPossibility, subLine));	
				lines.add(concatLines(secondPossibility, subLine));	
			}
		}
		return lines;
	}
	
	private static boolean matchCurrentRowSolution(int[] line, int row){
		boolean match = true;
		for (int i=0; i<line.length; i++)
			if (board[row][i]!=0 && board[row][i]!= line[i])
				match = false;
		return match;				
	}
	
	private static boolean matchCurrentColumnSolution(int[] line, int column){
		boolean match = true;
		for (int i=0; i<line.length; i++)
			if (board[i][column]!=0 && board[i][column]!= line[i])
				match = false;
		return match;				
	}
	
	/*
	 * Checks if the line matches the given pattern.
	 */
	private static boolean matchPattern(int[] line, int[] pattern){
		int index = 0;
		boolean match=true;
		for (int p: pattern){
			while (index<line.length && (line[index]==2))
				index++;
			int counter=0;
			while (index<line.length && line[index]==1){
				index++;
				counter++;
			}
			if (counter!=p)	
				match = false;
		}
		while (index<line.length && line[index]==2)
			index++;
		if (index!=line.length)
			match = false;
		return match;
	}
	
	private static boolean checkSolution(int[][] board){
		boolean result=true;
		for (int i=0; i<board.length; i++)
			if (!matchPattern(board[i], rowPattern[i]))
				result = false;
		
		for (int i=0; i<board[0].length; i++){
			int[] column = new int[board.length];
			for (int j=0; j<board.length; j++)
				column[j] = board[j][i];
			if (!matchPattern(column, columnPattern[i]))
				result = false;
		}			
			
		return result;
	}

	
	/*
	 * Returns the concatenation of two lines
	 */
	private static int[] concatLines(int[] line1, int[] line2){
		int[] newLine = new int[line1.length+line2.length];
		for (int i=0; i<line1.length; i++)
			newLine[i] = line1[i];
		for (int i=0; i<line2.length; i++)
			newLine[line1.length+i] = line2[i];
		return newLine;
	}
	
	private static int[] calculateIntersection(List<int[]> lines){
		int[] intersection = new int[lines.get(0).length];
		for (int i=0; i<intersection.length; i++){
			boolean check=true;
			int[] firstLine = lines.get(0);
			for (int[] line: lines)
				if (line[i]!=firstLine[i])
					check=false;
			if (check)
				if (firstLine[i]==0)
					intersection[i]=2;
				else
					intersection[i]=firstLine[i];
		}
		return intersection;
	}
	
	private static void printBoard(int[][] board){
		for (int i=0; i<board.length; i++){
			for (int j=0; j<board[0].length; j++)
				System.out.print(board[i][j]+" ");
			System.out.println();
		}
	}