Problem: ioi06_f
Categories: graph, search
Difficulty: medium
Time limit: 15000 ms | Memory limit: 256 MB

This task uses a 15 second time limit and provides a grader interface for interacting with the black box.

The Black Box Game is played with a square-shaped black box lying flat on a table. Each of its four sides has $n$ holes (for a total of $4\cdot n$ holes) into which a ball can be thrown. A thrown ball will eventually exit from one of the $4\cdot n$ holes, potentially the same hole into which it was thrown.

The black box's internals can be envisioned as an $n \times n$ grid. The holes in the sides are the starts and ends of rows and columns. Each of the box's squares is either empty or occupied by a deflector. A deflector is a piece of hardware that changes the direction of the ball by 90 degrees. Consider this example of a $5 \times 5$ box.

 ![](https://ioi.contest.codeforces.com/espresso/ff98996d1f014917d9047397f1ac94be98d14b72.png) 

A ball thrown into the box follows a straight line until it either hits a deflector or exits the box. When a ball hits a deflector, the ball changes direction and the deflector toggles its position (by "toggle" we mean rotate 90 degrees). The examples below show the action of a deflector.

 ![](https://ioi.contest.codeforces.com/espresso/0aed97ae1bb4bf5d5068d49c4255efda2bca9128.png) 

1. A ball is thrown through a hole; it hits a deflector and changes direction.
2. After the first ball was thrown, the deflector has toggled its position. A new ball is thrown into the same hole, hits the deflector and is deflected in a direction opposite to that of the first ball.
3. The deflector toggles every time it is hit.

Whenever a deflector is hit, it makes a beep. The number of times the ball was deflected can be deduced by counting the beeps. It can be proved that the ball always exits the box. The box has a button that resets it to its original state and another button that toggles all of its deflectors.

You will be provided with an interface to black box via grader. You must determine the internals of the box as best as possible .

$1 \le n \le 30$

Input

You are given a grader that provides the following functions

```
int throwBall(int holeIn, int sideIn, int &holeOut, int &sideOut);
```

Throws a ball into the box through hole number holeIn in side sideIn, sides are numbered as 1 — Top, 2 — Right, 3 — Bottom and 4 — Left. Holes are numbered from left to right and from top to bottom starting from number 1 in each side. In holeOut and sideOut you'll receive the hole and side number where the ball exits the box. The function throwBall returns the number of beeps caused by the ball hitting a deflector.

```
void ResetBox();
```

Resets every deflector in the box to its initial position.

```
void ToggleDeflectors();
```

Toggles every deflector in the box.

You need to implement function

```
void solve(vector<string>& ans);
```

This function should put answer in to given vector. $ans[i][j]$ should be equal to content of cell $(i, j)$ with same symbols, that are used for input below. If you can't determinate content of cell, you can fill it with ?, and still receive part of points. Size of vector is equal to size of field. Initially all elements are filled with ?

A sample interaction for the box in the previous figure could be:

throwBall(3, 4, holeOut, sideOut);

A ball is thrown into hole number 3 (third from the top) on the left side. Returns 1, indicating that the ball hit 1 deflector. When the function returns, holeOut will equal 2 and sideOut will equal 3 indicating that the ball exited through hole 2 (second from the left) of the bottom side of the box.

Grader will read input in following format:

LINE 1: Contains n, the number of holes on each side.

n LINES: Each line describes a row of the box, starting from the topmost row to the bottom row. Each line must contain exactly n characters; each character corresponds to a column (running from left to right).

* "." means that the square is empty.
* "/" means the square contains a deflector with initial position "/"
* "\" means the square contains a deflector with initial position "\"

Input for test 01 can be found in problem graders archive. Solving it will not be rewarded by any points.

Scoring

If your submission has a '.", "/" or "\" character in an incorrect position, you'll get zero points for that box.

Let $n$ be the size of box, and let $B$ be the number of discovered positions in your submission, then percentage of your score for that test would be $\frac{100 \cdot B}{n^2}$.

Interactor / grader files:
grader.cpp: #include "blackbox.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N 32
#define K (N * N)

static char F[N][N];
static char _F[N][N];

static int _x[K], _y[K];
static char _v[K];
static int pt = 0;

static int n;

void ToggleDeflectors() {
  int i;
  for (i = 0; i < pt; i++)
    F[_y[i]][_x[i]] ^= '/' ^ '\\';
}

void ResetBox() {
  int i;
  for (i = 0; i < pt; i++)
    F[_y[i]][_x[i]] = _v[i];
}

static void internal_init() {
  int i, j;
  scanf("%s", F[0]);
  n = strlen(F[0]);
  for (i = 1; i < n; i++)
    scanf("%s", F[i]);
  memcpy(_F, F, sizeof(F));
  for (i = 0; i < n; i++)
    for (j = 0; j < n; j++)
      if (F[i][j] != '.')
        _x[pt] = j, _y[pt] = i, _v[pt] = F[i][j], pt++;
}

static int vy[] = {1, 0, -1, 0};
static int vx[] = {0, -1, 0, 1};

// 0 - down, 1 - left, 2 - up, 3 - right

int throwBall(int h, int s, int &newh, int &news) {
  if (!(1 <= s && s <= 4)) {
    printf("Invalid side : %d\n", s);
    exit(0);
  }
  if (!(1 <= h && h <= n)) {
    printf("Invalid hole : %d\n", h);
    exit(0);
  }
  --s;
  --h;
  int x, y;
  if (s % 2)
    y = h, x = (s == 1) * (n - 1);
  else
    x = h, y = (s == 2) * (n - 1);
  int v = s;
  int count = 0;
  while (1) {
    if (F[y][x] != '.') {
      v = ((F[y][x] == '/') ?
                            // 0 - 1, 2 - 3
               1 ^ v
                            :
                            // 0 - 3, 1 - 2
               3 ^ v);
      count++;
      F[y][x] ^= ('/' ^ '\\');
    }
    y += vy[v];
    x += vx[v];
    if (y < 0 || x < 0 || y >= n || x >= n)
      break;
  }
  h = (v % 2) ? y : x;
  h++;
  s = 2 ^ v;
  s++;
  news = s;
  newh = h;
  return count;
}

static int notFound;

static void CheckAnswer(std::vector<std::string> pans) {
  int i, j;
  ResetBox();
  for (i = 0; i < n; i++)
    for (j = 0; j < n; j++) {
      if (pans[i][j] != F[i][j] && pans[i][j] != '?') {
        printf("Position (%d,%d) doesn't match", i + 1, j + 1);
        exit(0);
      } else if (pans[i][j] == '?')
        notFound++;
    }
}

int main() {
  internal_init();
  std::vector<std::string> pans(n, std::string(n, '?'));
  solve(pans);
  CheckAnswer(pans);
  printf("OK %d cells not found\n", notFound);
  return 0;
}

blackbox.h: #pragma once

#include <vector>
#include <string>

void ToggleDeflectors();
void ResetBox();
int throwBall(int sideIn, int holeIn, int& sideOut, int& holeOut);
void solve(std::vector<std::string> &ans);