+ def task_islands(self, A, f_A, B, f_B):
+ for X, f_X in [(A, f_A), (B, f_B)]:
+ while True:
+ i, j = torch.randint(self.height, (1,)), torch.randint(self.width, (1,))
+ if (
+ i == 0
+ or i == self.height - 1
+ or j == 0
+ or j == self.width - 1
+ or X[i, j] == 1
+ ):
+ break
+ while True:
+ di, dj = torch.randint(3, (2,)) - 1
+ if abs(di) + abs(dj) > 0:
+ break
+ X[i, j] = 1
+ while True:
+ i, j = i + di, j + dj
+ if i < 0 or i >= self.height or j < 0 or j >= self.width:
+ break
+ b = (
+ i == 0
+ or i == self.height - 1
+ or j == 0
+ or j == self.width - 1
+ or X[i, j] == 1
+ )
+ X[i, j] = 1
+ if b:
+ break
+