Exemplo n.º 1
0
func (b *Bitmapper) ReadInput(r myio.Reader) {
	b.q = list.New()
	b.valueMap = make(map[Point]*Info)
	line := r.Read()
	elements := strings.Split(line, " ")
	numRows, _ := strconv.Atoi(elements[0])
	numCols, _ := strconv.Atoi(elements[1])
	b.numRows = numRows
	b.numCols = numCols
	for i := 0; i < numRows; i++ {
		colLine := r.Read()
		colArr := strings.Split(colLine, "")
		for j := 0; j < numCols; j++ {
			val, _ := strconv.Atoi(colArr[j])
			point := Point{i, j}
			if val == 1 {
				b.valueMap[point] = &Info{true, 0}
				b.q.PushBack(point)
				//fmt.Printf("pushing a 1")
			} else {
				b.valueMap[point] = &Info{false, -1}
			}
		}
	}
}
Exemplo n.º 2
0
func (b *Board) Create(r myio.Reader) {
	boardArr := make([]int, 16)

	// Read board from input.
	for i := 0; i < 4; i++ {
		nums := strings.Split(r.Read(), " ")
		for j, num := range nums {
			numI, _ := strconv.Atoi(num)
			k := i*4 + j
			boardArr[k] = numI
		}
	}

	b.board = FromBoard(boardArr)
}
Exemplo n.º 3
0
func (s *SudokuB) Solve(r myio.Reader) {
	sol := &SudokuSolver{}
	state0 := s.Create(r.Read())
	sol.Init(state0)
	idest, numGuesses := GraphSearch(sol.frontier, sol.explored, sol)
	if idest != nil {
		dest := idest.(*SNode)
		if !dest.state.DidISolveTheBoard() {
			log.Fatalf("You didn't really solve the board correctly dummy!!")
		}
		cost := dest.h + dest.f
		fmt.Printf("***********")
		fmt.Printf("Solved it with cost %v. f: %v. Guesses: %v\n",
			cost, dest.f, numGuesses)
		fmt.Printf("Solution board:\n")
		dest.state.Visualize()
	} else {
		fmt.Println("There is no way to solve this puzzle.\n")
	}
}