Go Stone Puzzle

AtCoder
IDabc361_d
Time2000ms
Memory256MB
Difficulty
There are $N+2$ cells arranged in a row. Let cell $i$ denote the $i$\-th cell from the left. There is one stone placed in each of the cells from cell $1$ to cell $N$. For each $1 \leq i \leq N$, the stone in cell $i$ is white if $S_i$ is `W`, and black if $S_i$ is `B`. Cells $N+1$ and $N+2$ are empty. You can perform the following operation any number of times (possibly zero): * Choose a pair of adjacent cells that both contain stones, and move these two stones to the empty two cells while preserving their order. More precisely, choose an integer $x$ such that $1 \leq x \leq N+1$ and both cells $x$ and $x+1$ contain stones. Let $k$ and $k+1$ be the empty two cells. Move the stones from cells $x$ and $x+1$ to cells $k$ and $k+1$, respectively. Determine if it is possible to achieve the following state, and if so, find the minimum number of operations required: * Each of the cells from cell $1$ to cell $N$ contains one stone, and for each $1 \leq i \leq N$, the stone in cell $i$ is white if $T_i$ is `W`, and black if $T_i$ is `B`. ## Constraints * $2 \leq N \leq 14$ * $N$ is an integer. * Each of $S$ and $T$ is a string of length $N$ consisting of `B` and `W`. ## Input The input is given from Standard Input in the following format: $N$ $S$ $T$ [samples]
Samples
Input #1
6
BWBWBW
WWWBBB
Output #1
4

Using `.` to represent an empty cell, the desired state can be achieved in four operations as follows, which is the minimum:

*   `BWBWBW..`
*   `BW..BWBW`
*   `BWWBB..W`
*   `..WBBBWW`
*   `WWWBBB..`
Input #2
6
BBBBBB
WWWWWW
Output #2
\-1
Input #3
14
BBBWBWWWBBWWBW
WBWWBBWWWBWBBB
Output #3
7
API Response (JSON)
{
  "problem": {
    "name": "Go Stone Puzzle",
    "description": {
      "content": "There are $N+2$ cells arranged in a row. Let cell $i$ denote the $i$\\-th cell from the left. There is one stone placed in each of the cells from cell $1$ to cell $N$.   For each $1 \\leq i \\leq N$, the",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "abc361_d"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "There are $N+2$ cells arranged in a row. Let cell $i$ denote the $i$\\-th cell from the left.\nThere is one stone placed in each of the cells from cell $1$ to cell $N$.  \nFor each $1 \\leq i \\leq N$, the...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments