Hexagonal Grid
Difficulty: Hard

You are given a hexagonal grid consisting of two rows, each row consisting of $n$ cells. The cells of the first row are labelled $a_1, a_2, \ldots a_n$ and the cells of the second row are labelled $b_1, b_2, \ldots, b_n$.  

For example, for $n = 6$:

![Grid Shape](https://s3.amazonaws.com/hr-assets/0/1495564532-9e16e289ea-3.png "The grid shape.")

(Note that the $b_i$ is connected with $a_{i+1}$.)

Your task is to tile this grid with $2\times 1$ *tiles* that look like the following:

![Orientations](https://s3.amazonaws.com/hr-assets/0/1495564517-f725e11e0b-1.png "Orientations of the 2x1 tile.")

As you can see above, there are three possible orientations in which a tile can be placed.

Your goal is to tile the whole grid such that every cell is covered by a tile, and no two tiles occupy the same cell. To add to the woes, certain cells of the hexagonal grid are *blackened*. No tile must occupy a blackened cell.

Is it possible to tile the grid?

Here's an example. Suppose we want to tile this grid:

![Example Blank](https://s3.amazonaws.com/hr-assets/0/1495564538-9a59a63208-4.png "Example of a grid with some blackened cells.")

Then we can do the tiling as follows:

![Example Tiled](https://s3.amazonaws.com/hr-assets/0/1495564526-f0c430b796-2.png "Example of a tiled grid.")

Input Format:
The first line contains a single integer $t$, the number of test cases.  

The first line of each test case contains a single integer $n$ denoting the length of the grid.  
The second line contains a binary string of length $n$. The $i^\text{th}$ character describes whether cell $a_i$ is blackened.  
The third line contains a binary string of length $n$. The $i^\text{th}$ character describes whether cell $b_i$ is blackened.  
A `0` corresponds to an empty cell and a `1` corresponds to blackened cell.  

Output Format:
For each test case, print `YES` if there exists at least one way to tile the grid, and `NO` otherwise.

Sample Input:
    6
    6
    010000
    000010
    2
	00
	00
	2
	00
	10
	2
	00
	01
	2
	00
	11
	2
	10
	00


Sample Output:
    YES
	YES
	NO
	NO
	YES
	NO


Python 3 starter code:
#
# Complete the 'hexagonalGrid' function below.
#
# The function is expected to return a STRING.
# The function accepts following parameters:
#  1. STRING a
#  2. STRING b
#

def hexagonalGrid(a, b):
    # Write your code here



Editorial: Hexagonal Grid - 101 Hack March Editorial
<style id="MathJax_SVG_styles">.MathJax_SVG_Display {text-align: center; margin: 1em 0em; position: relative; display: block!important; text-indent: 0; max-width: none; max-height: none; min-width: 0; min-height: 0; width: 100%}
.MathJax_SVG .MJX-monospace {font-family: monospace}
.MathJax_SVG .MJX-sans-serif {font-family: sans-serif}
.MathJax_SVG {display: inline; font-style: normal; font-weight: normal; line-height: normal; font-size: 100%; font-size-adjust: none; text-indent: 0; text-align: left; text-transform: none; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0; min-height: 0; border: 0; padding: 0; margin: 0}
.MathJax_SVG * {transition: none; -webkit-transition: none; -moz-transition: none; -ms-transition: none; -o-transition: none}
.mjx-svg-href {fill: blue; stroke: blue}
</style><svg style="display: none;"><defs id="MathJax_SVG_glyphs"></defs></svg><p><a href="https://www.hackerrank.com/contests/101mar14/challenges/hexagonal-grid">Hexagonal Grid</a> <br>
<strong>Difficulty Level</strong> : Easy-Medium <br>
<strong>Required Knowledge</strong> : Recursion or DP <br>
<strong>Problem Setter</strong> : <a href="https://www.hackerrank.com/Seyaua">Seyaua</a>  <br>
<strong>Problem Tester</strong> : <a href="https://www.hackerrank.com/abhiranjan">Abhiranjan Kumar</a>  <br></p>

<p><strong>Solution</strong> <br>
There are a lot of ways to solve this problem. The easiest one is to just run a recursion, where in each step we will try to fill just two empty cells. The constraints are very small, so this solution works very fast. As an alternative solution you can write something like dynamic programming by submasks.</p>

<p><strong>Setter's Code</strong></p>

<pre><code>#include &lt;iostream&gt;
#include &lt;cstdio&gt;
#include &lt;string&gt;
#include &lt;cstring&gt;
#include &lt;set&gt;
#include &lt;map&gt;
#include &lt;vector&gt;
#include &lt;cmath&gt;
#include &lt;ctime&gt;
#include &lt;algorithm&gt;
#include &lt;bitset&gt;
#include &lt;queue&gt;
#pragma comment(linker, "/STACK:256000000")

using namespace std;

const int maxN = 12;

int a[2][maxN];
int n;
bool fnd = false;

bool is_in(int i, int j) {
  return (0 &lt;= i &amp;&amp; i &lt; 2 &amp;&amp; j &gt;= 0 &amp;&amp; j &lt; n);
}

int dx[] = {0, 1, 1};
int dy[] = {1, 0, -1};

void rec() {
  if (fnd) return;
  int change = false;
  for (int i = 0; i &lt; 2 &amp;&amp; !change &amp;&amp; !fnd; ++i) {
    for (int j = 0; j &lt; n &amp;&amp; !change &amp;&amp; !fnd; ++j) {
      if (a[i][j] == 0) {
        change = true;
        for (int k = 0; k &lt; 3 &amp;&amp; !fnd; ++k) {
          if (is_in(i + dx[k], j + dy[k]) &amp;&amp; a[i + dx[k]][j + dy[k]] == 0) {
            a[i][j] = 1;
            a[i + dx[k]][j + dy[k]] = 1;
            rec();
            a[i][j] = 0;
            a[i + dx[k]][j + dy[k]] = 0;
          }
        }
      }
    }
  }
  if (!change) {
    fnd = true;
  }
}

void solve() {
  scanf("%d", &amp;n);
  string s1, s2;
  cin &gt;&gt; s1 &gt;&gt; s2;

  for (int i = 0; i &lt; n; ++i) {
    a[0][i] = s1[i] - '0';
    a[1][i] = s2[i] - '0';
  }

  fnd = false;
  rec();
  if (fnd) {
    cout &lt;&lt; "YES" &lt;&lt; endl;
  } else {
    cout &lt;&lt; "NO" &lt;&lt; endl;
  }
}

int main() {
  //freopen("input.txt", "r", stdin);
  //freopen("output.txt", "w", stdout);

  int t;
  scanf("%d", &amp;t);
  for (int i = 0; i &lt; t; ++i) {
    solve();
  }

  return 0;
}
</code></pre>

<p><strong>Tester's Code</strong></p>

<pre><code>#include &lt;bits/stdc++.h&gt;
using namespace std;

int a[2][101];
int dp[105][105];
int N;

bool solve(int l, int r) {
    if(l == -1 &amp;&amp; r == -1)
        return 1;
    if(l &lt; -1 ||r &lt; -1)
        return 0;
    if(l &lt; 0 &amp;&amp; r &gt;= 0 &amp;&amp; a[1][r] == 1)
        return solve(l, r-1);
    if(r &lt; 0 &amp;&amp; l &gt;= 0 &amp;&amp; a[0][l] == 1)
        return solve(l-1, r);
    if(l &lt; 0 || r &lt; 0)
        return 0;

    int &amp;ret = dp[l][r];
    if(ret != -1)
        return ret;

    ret = 0;

    if(a[0][l] == 1)
        ret |= solve(l-1, r);
    if(a[1][r] == 1)
        ret |= solve(l, r-1);

    if(l &gt; 0 &amp;&amp; a[0][l] == 0 &amp;&amp; a[0][l-1] == 0)
        ret |= solve(l-2, r);
    if(r &gt; 0 &amp;&amp; a[1][r] == 0 &amp;&amp; a[1][r-1] == 0)
        ret |= solve(l, r-2);

    if(l == r &amp;&amp; a[0][l] == 0 &amp;&amp; a[1][r] == 0)
        ret |= solve(l-1, r-1);

    if(l == r+1 &amp;&amp; a[0][l] == 0 &amp;&amp; a[1][r] == 0)
        ret |= solve(l-1, r-1);

    return ret;
}

int main()
{
    int t;
    scanf("%d", &amp;t);
    assert(t &gt;= 1 &amp;&amp; t &lt;= 100);

    while(t--) {
        memset(dp, -1, sizeof(dp));
        cin &gt;&gt; N;
        assert( N &gt;= 1 &amp;&amp; N &lt;= 10);

        string st[2];
        cin &gt;&gt; st[0] &gt;&gt; st[1];
        assert((int)st[0].length() == N);
        assert((int)st[1].length() == N);

        for(int i = 0; i &lt; (int)N; ++i) {
            assert(st[0][i] == '0' || st[0][i] == '1');
            assert(st[1][i] == '0' || st[1][i] == '1');

            a[0][i] = st[0][i] == '0' ? 0 : 1;
            a[1][i] = st[1][i] == '0' ? 0 : 1;
        }
        cout &lt;&lt; (solve(N-1, N-1) == 1 ? "YES" : "NO") &lt;&lt; "\n";
    }

    return 0;
}
</code></pre>

Reference solution (C++):
###C++  
```cpp  


#include <bits/stdc++.h>
using namespace std;
    
int a[2][101];
int dp[105][105];
int N;
    
bool solve(int l, int r) {
    if(l == -1 && r == -1)
        return 1;
    if(l < -1 ||r < -1)
        return 0;
    if(l < 0 && r >= 0 && a[1][r] == 1)
        return solve(l, r-1);
    if(r < 0 && l >= 0 && a[0][l] == 1)
        return solve(l-1, r);
    if(l < 0 || r < 0)
        return 0;
    
    int &ret = dp[l][r];
    if(ret != -1)
        return ret;
    
    ret = 0;
    
    if(a[0][l] == 1)
        ret |= solve(l-1, r);
    if(a[1][r] == 1)
        ret |= solve(l, r-1);
    
    if(l > 0 && a[0][l] == 0 && a[0][l-1] == 0)
        ret |= solve(l-2, r);
    if(r > 0 && a[1][r] == 0 && a[1][r-1] == 0)
        ret |= solve(l, r-2);
    
    if(l == r && a[0][l] == 0 && a[1][r] == 0)
        ret |= solve(l-1, r-1);
    
    if(l == r+1 && a[0][l] == 0 && a[1][r] == 0)
        ret |= solve(l-1, r-1);
    
    return ret;
}
    
int main()
{
    int t;
    scanf("%d", &t);
    assert(t >= 1 && t <= 100);
    
    while(t--) {
        memset(dp, -1, sizeof(dp));
        cin >> N;
        assert( N >= 1 && N <= 10);
    
        string st[2];
        cin >> st[0] >> st[1];
        assert((int)st[0].length() == N);
        assert((int)st[1].length() == N);
    
        for(int i = 0; i < (int)N; ++i) {
            assert(st[0][i] == '0' || st[0][i] == '1');
            assert(st[1][i] == '0' || st[1][i] == '1');
    
            a[0][i] = st[0][i] == '0' ? 0 : 1;
            a[1][i] = st[1][i] == '0' ? 0 : 1;
        }
        cout << (solve(N-1, N-1) == 1 ? "YES" : "NO") << "\n";
    }
    
    return 0;
}
```