Guest User

Untitled

a guest
Jul 20th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. )
  6.  
  7. /*
  8. Given a grid. Find maximum number of connected colors
  9. */
  10.  
  11. type Cell struct {
  12. X int32
  13. Y int32
  14. Color int32
  15. }
  16.  
  17. type Square struct {
  18. Color int32
  19. Area map[string]*Cell
  20. }
  21.  
  22. func (s *Square) Merge(another *Square) {
  23. for k, v := range another.Area {
  24. s.Area[k] = v
  25. }
  26. }
  27.  
  28. func (s *Square) Add(cell *Cell) {
  29. s.Area[cell.Key()] = cell
  30. }
  31.  
  32. func (c Cell) Key() string {
  33. return fmt.Sprintf("%d%d", c.X, c.Y)
  34. }
  35.  
  36. var input = [][]Cell{
  37. {Cell{0, 0, 0}, Cell{0, 1, 0}, Cell{0, 2, 1}, Cell{0, 3, 0},},
  38. {Cell{1, 0, 0}, Cell{1, 1, 2}, Cell{1, 2, 1}, Cell{1, 3, 2},},
  39. {Cell{2, 0, 1}, Cell{2, 1, 2}, Cell{2, 2, 2}, Cell{2, 3, 2},},
  40. }
  41.  
  42. func main() {
  43. var squares = make(map[string]*Square)
  44. var maxSquare = &Square{}
  45.  
  46. fmt.Println("Input:")
  47. for i, raw := range input {
  48. for j, cell := range raw {
  49. fmt.Printf("%d ", cell.Color)
  50.  
  51. if i-1 >= 0 {
  52. check(cell, &input[i-1][j], squares, maxSquare)
  53. }
  54. if j-1 >= 0 {
  55. check(cell, &input[i][j-1], squares, maxSquare)
  56. }
  57. if i+1 < len(input) {
  58. check(cell, &input[i+1][j], squares, maxSquare)
  59. }
  60. if j+1 < len(raw) {
  61. check(cell, &input[i][j+1], squares, maxSquare)
  62. }
  63. }
  64. fmt.Println()
  65. }
  66. fmt.Printf("Biggest area size: %d cells\ncolor: %d\n", len(maxSquare.Area), maxSquare.Color)
  67. fmt.Println("Cells:")
  68. for _, value := range maxSquare.Area {
  69. fmt.Printf("x=%d y=%d\n", value.X, value.Y)
  70. }
  71. }
  72.  
  73. func check(cell Cell, neighbourCell *Cell, squares map[string]*Square, maxSquare *Square) {
  74. currentSquare, currentIsInSquare := squares[cell.Key()]
  75. neighbourSquare, neighbourIsInSquare := squares[neighbourCell.Key()]
  76.  
  77. //init square with current cell
  78. if !currentIsInSquare {
  79. currentArea := map[string]*Cell{cell.Key(): &cell}
  80. currentSquare = &Square{
  81. Color: cell.Color,
  82. Area: currentArea,
  83. }
  84. squares[cell.Key()] = currentSquare
  85. }
  86.  
  87. //main logic
  88. if neighbourIsInSquare && currentSquare != neighbourSquare && currentSquare.Color == neighbourSquare.Color {
  89. currentSquare.Merge(neighbourSquare)
  90. squares[neighbourCell.Key()] = currentSquare
  91. } else if !neighbourIsInSquare && currentSquare.Color == neighbourCell.Color {
  92. currentSquare.Add(neighbourCell)
  93. squares[neighbourCell.Key()] = currentSquare
  94. }
  95.  
  96. //find biggest square
  97. if len(maxSquare.Area) < len(currentSquare.Area) {
  98. maxSquare.Area = currentSquare.Area
  99. maxSquare.Color = currentSquare.Color
  100. }
  101. }
Add Comment
Please, Sign In to add comment