2015-10-21
- jennifer
- martym
| def diagonalDifference(a: Array[Array[Int]]): Int = { | |
| val dimension = a.length | |
| val primaryDiagonal = for{ | |
| x <- 0 until dimension | |
| y <- 0 until dimension | |
| if x==y | |
| }yield a(x)(y) | |
| val secondaryDiagonal = for{ | |
| x <- 0 until dimension |
| public abstract class ChessPiece{ | |
| private int row; | |
| private int col; | |
| public abstract char piece(); | |
| } |
| public class Bishop extends ChessPiece { | |
| public boolean attacks(ChessPiece dest) { | |
| return Math.abs(dest.getRow() - this.getRow()) == Math.abs(dest.getCol() - this.getCol()); | |
| } | |
| @Override | |
| public char piece() { | |
| return 'B'; | |
| } |
| public class Board { | |
| private int M; | |
| private int N; | |
| private char[][] board; | |
| private ArrayList<ChessPiece> usedPieces; | |
| } |
| /*** | |
| * @param board ChessBoard | |
| * @param pieces Chess Pieces selected by the user | |
| * @param solutions valid solutions to the problem | |
| * @param testedConfigurations Board configurations to which solutions were not found | |
| * @return return a list of possible solutions to the problem in the form of a list of filled chess boards | |
| */ | |
| public static Set<Board> solution(Board board, List<Character> pieces, Set<Board> solutions, Set<Board> testedConfigurations) { | |
| if (!pieces.isEmpty()) { // if pieces is empty I return the solutions | |
| for (int i = 1; i < board.getM(); i++) { |
| data PieceType = Rook | Bishop | Knight | Queen | King deriving(Show, Eq, Ord) | |
| data ChessPiece = Piece {row:: !Int, col:: !Int, piece:: PieceType} deriving(Eq, Ord) | |
| data Board = Board{m:: !Int, n:: !Int, usedPieces:: Set ChessPiece , numberOfPieces:: !Int} deriving(Eq, Ord) |
| findCandidate :: ChessPiece -> Board -> Set Board | |
| findCandidate p b | |
| = Data.Set.fromList $ | |
| [ newBoard | |
| | rr <- [1 .. (m b)] | |
| , cc <- [1 .. (n b)] | |
| , let pp = createPiece (piece p) rr cc | |
| , let newBoard = place b pp | |
| , isSafe b pp] |
| done::Board->Bool | |
| done Board{usedPieces = up, numberOfPieces=n} = length up == n |
| package main | |
| import "fmt" | |
| type ChessPiece struct { | |
| row int | |
| col int | |
| piece rune | |
| } | |