1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import java.util.Arrays;

public class cie {

  private static int dimensionsFound = 0;
  private static Poses[] posRanges;

  public static void main(String[] args) {
    int d = cielib.podajD();
    int k = cielib.podajK();
    int r = cielib.podajR();

    initPosRanges(d, r);
    int[] entekPosition = initEntekPosition(d, r);

    int currentDimension = 0;
    while (dimensionsFound != d) {
      setPositionForDimensionAsCloseAsPossible(entekPosition, currentDimension);
      currentDimension = (currentDimension + 1) % d;
    }

    printPos(entekPosition);

    cielib.znalazlem(entekPosition);
  }

  private static void setPositionForDimensionAsCloseAsPossible(int[] entekPosition, int currentDimension) {
    recursivePosSet(entekPosition, currentDimension);
  }

  private static void recursivePosSet(int[] entekPosition, int currentDimension) {
    int minPos = posRanges[currentDimension].minPos;
    int maxPos = posRanges[currentDimension].maxPos;
    if (minPos == -1) {
      return;
    }

    if (maxPos - minPos == 1) {
      findWinningPointWhenTwoLeft(entekPosition, currentDimension, minPos, maxPos);
      return;
    }

    entekPosition[currentDimension] = middle(minPos, maxPos);
    cielib.czyCieplo(entekPosition);
    int startPos = entekPosition[currentDimension];

    entekPosition[currentDimension] = minPos;
    boolean closerWhenMinPos = cielib.czyCieplo(entekPosition) == 1;
    if (closerWhenMinPos) {
      entekPosition[currentDimension] = middle(minPos, startPos);
      posRanges[currentDimension].maxPos = startPos;
      recursivePosSet(entekPosition, currentDimension);
    } else {
      entekPosition[currentDimension] = maxPos;
      boolean closerWhenMaxPos = cielib.czyCieplo(entekPosition) == 1;
      if (closerWhenMaxPos) {
        entekPosition[currentDimension] = middle(startPos, maxPos);
        posRanges[currentDimension].minPos = startPos;
        recursivePosSet(entekPosition, currentDimension);
      } else {
        entekPosition[currentDimension] = startPos;
      }
    }
  }

  private static void findWinningPointWhenTwoLeft(int[] entekPosition, int currentDimension,
                                                  int lower, int higher) {
    int startPos = entekPosition[currentDimension];
    cielib.czyCieplo(entekPosition);
    if (startPos == lower) {
      entekPosition[currentDimension] = higher;
      boolean closer = cielib.czyCieplo(entekPosition) == 1;
      if (closer) {
        posRanges[currentDimension].minPos = -1;
        posRanges[currentDimension].maxPos = -1;
        dimensionsFound++;
      } else {
        entekPosition[currentDimension] = lower;
        boolean closerWhenLower = cielib.czyCieplo(entekPosition) == 1;
        if (closerWhenLower) {
          posRanges[currentDimension].minPos = -1;
          posRanges[currentDimension].maxPos = -1;
          dimensionsFound++;
        }
      }
    } else {
      entekPosition[currentDimension] = lower;
      boolean closer = cielib.czyCieplo(entekPosition) == 1;
      if (closer) {
        posRanges[currentDimension].minPos = -1;
        posRanges[currentDimension].maxPos = -1;
        dimensionsFound++;
      } else {
        entekPosition[currentDimension] = higher;
        boolean closerWhenHigher = cielib.czyCieplo(entekPosition) == 1;
        if (closerWhenHigher) {
          posRanges[currentDimension].minPos = -1;
          posRanges[currentDimension].maxPos = -1;
          dimensionsFound++;
        }
      }
    }
  }

  private static int[] initEntekPosition(int d, int r) {
    int entekPosition[] = new int[d];
    int middleCoord = r / 2;
    for (int i = 0; i < d; ++i) {
      entekPosition[i] = middleCoord;
    }
    return entekPosition;
  }

  private static void initPosRanges(int d, int r) {
    posRanges = new Poses[d];
    for (int i = 0; i < d; i++) {
      posRanges[i] = new Poses(0, r);
    }
  }

  private static void printPos(int[] entekPosition) {
//    Arrays.stream(entekPosition).forEach(pos -> System.out.print(pos + " "));
//    System.out.println();
  }

  private static int middle(int start, int end) {
    return (start + end) / 2;
  }

  private static class Poses {
    private int minPos;
    private int maxPos;

    private Poses(int minPos, int maxPos) {
      this.minPos = minPos;
      this.maxPos = maxPos;
    }
  }

}