Problem:
A Two Floors Dungeon

It was the last day of the summer camp you strayed into the labyrinth on the way to Komaba Campus, the University of Tokyo.
The contest has just begun.
Your teammates must impatiently wait for you.
So you have to escape from this labyrinth as soon as possible.

The labyrinth is represented by a grid map.
Initially, each grid except for walls and stairs is either on the first floor or on the second floor.
Some grids have a switch which can move up or down some of the grids (the grids on the first floor move to the second floor, and the grids on the second floor to the first floor).

In each step, you can take one of the following actions:

Move to an adjacent grid (includes stairs) on the same floor you are now in.

Move to another floor (if you are in the stairs grid).

Operate the switch (if you are in a grid with a switch).

Luckily, you have just found a map of the labyrinth for some unknown reason.
Let's calculate the minimum step to escape from the labyrinth, and go to the place your teammates are waiting!

Input

The format of the input is as follows.

W H
M11M12M13...M1W
M21M22M23...M2W
........
MH1MH2MH3...MHW
S
MS111MS112MS113...MS11W
MS121MS122MS123...MS12W
........
MS1H1MS1H2MS1H3...MS1HW
MS211MS212MS213...MS21W
MS221MS222MS223...MS22W
........
MS2H1MS2H2MS2H3...MS2HW
MSS11MSS12MSS13...MSS1W
MSS21MSS22MSS23...MSS2W
........
MSSH1MSSH2MSSH3...MSSHW

The first line contains two integers W (3 ≤ W ≤ 50) and H (3 ≤ H ≤ 50). They represent the width and height of the labyrinth, respectively.

The following H lines represent the initial state of the labyrinth.
Each of Mij is one of the following symbols:

'#' representing a wall,

'|' representing stairs,

'_' representing a grid which is initially on the first floor,

'^' representing a grid which is initially on the second floor,

a lowercase letter from 'a' to 'j' representing a switch the grid has, and the grid is initially on the first floor,

an uppercase letter from 'A' to 'J' representing a switch the grid has, and the grid is initially on the second floor,

'%' representing the grid you are initially in (which is initially on the first floor) or

'&' representing the exit of the labyrinth (which is initially on the first floor).

The next line contains one integer S (0 ≤ S ≤ 10), and then the following SH lines represent the information of the switches.
Each of MSkij is one of:

'#' if Mij is a '#',

'|' if Mij is a '|',

'*' if the grid is moved by the switch represented by the k-th alphabet letter, or

'.' otherwise.

Note that the grid which contains a switch may be moved by operating the switch.
In this case, you will move together with the grid.

You may assume each of the '%' (start) and '&' (goal) appears exacyly once, that the map is surround by walls, and that each alphabet in the map is any of the letters from 'A' (or 'a') to S-th alphabet letter.

Output

Print the minimum step to reach the goal in one line.
If there is no solution, print "-1".

Sample Input 1

6 6
######
#_|A%#
#B#_|#
#^BBa#
#B&A##
######
2
######
#*|*.#
#.#.|#
#*.**#
#...##
######
######
#*|*.#
#*#.|#
#..**#
#..*##
######

Output for the Sample Input 1

21

Sample Input 2

8 3
########
#%||Aa&#
########
2
########
#*||*..#
########
########
#.||*.*#
########

Output for the Sample Input 2

7

Sample Input 3

3 4
###
#%#
#&#
###
0

Output for the Sample Input 3

1

Sample Input 4

3 5
###
#%#
#^#
#&#
###
0

Output for the Sample Input 4

-1

Solution in C++ (judge verdict: Accepted)

#include<iostream>
#include<map>
#include<algorithm>
#include<queue>
#define fs first
#define sc second
using namespace std;

typedef pair<int,int> P;
typedef pair<P,P> P2;

int w,h,s;
string g[50],ms[10][50];
bool f[1<<10][50][50];
int d[1<<10][2][50][50];
int sx,sy;
int dy[] = { -1,0,1,0}, dx[] = {0,1,0,-1};


int main(){
  cin >> w >> h;
  for(int i=0;i<h;i++){
    cin >> g[i];
    for(int j=0;j<w;j++){
      if(g[i][j] == '%'){
	sy = i;
	sx = j;
      }
    }
  }

  cin >> s;
  for(int i=0;i<s;i++)
    for(int j=0;j<h;j++)cin >> ms[i][j];

  for(int i=0;i<(1<<s);i++){
    for(int j=0;j<h;j++){
      for(int k=0;k<w;k++){
	d[i][0][j][k] = d[i][1][j][k] = -1;
	bool t = false;
	for(int l=0;l<s;l++){
	  if( (i>>l)&1 ){
	    if(ms[l][j][k] == '*')t = !t;
	  }
	}
	f[i][j][k] = t;
      }
    }
  }

  queue<P2> q;
  q.push(P2(P(0,0),P(sy,sx)));
  d[0][0][sy][sx] = 0;

  while(q.size()){
    P2 p = q.front(); q.pop();
    int s = p.fs.fs, fl = p.fs.sc, y = p.sc.fs, x = p.sc.sc;

    for(int i=0;i<4;i++){
      int ty = y + dy[i], tx = x + dx[i];
      if(g[ty][tx] == '#')continue;

      bool now = f[s][ty][tx];
      //cout << now << " " << ty << " " << tx << endl;
      if( (now == fl) && g[ty][tx] == '&'){
	cout << d[s][fl][y][x] + 1 << endl;
	return 0;
      }
      if( (now == fl) && g[ty][tx] == '^')continue;
      if( (now == fl) && 'A'<= g[ty][tx] && g[ty][tx] <= 'J')continue;
      if( (now != fl) && g[ty][tx] == '_')continue;
      if( (now != fl) && 'a'<= g[ty][tx] && g[ty][tx] <= 'j')continue;
      if( (now != fl) && (g[ty][tx] == '&' || g[ty][tx] == '%') )continue;

      if(d[s][fl][ty][tx]<0){
	d[s][fl][ty][tx] = d[s][fl][y][x] + 1;
	q.push(P2(P(s,fl),P(ty,tx)));
      }
    }

    if( (g[y][x] == '|') && (d[s][1-fl][y][x] < 0) ){
      d[s][1-fl][y][x] = d[s][fl][y][x] + 1;
      q.push(P2(P(s,1-fl),P(y,x)));
    }

    bool now = f[s][y][x];
    if( (now==fl) && 'a'<=g[y][x] && g[y][x]<='j'){
      int tmp = g[y][x] - 'a';
      int st = s ^ (1<<tmp);
      int tfl = fl;
      if(ms[tmp][y][x] == '*')tfl = 1-fl;
      if(d[st][tfl][y][x]<0){
	d[st][tfl][y][x] = d[s][fl][y][x] + 1;
	q.push(P2(P(st,tfl),P(y,x)));
      }
    }
    if( (now!=fl) && 'A'<=g[y][x] && g[y][x]<='J'){
      int tmp = g[y][x] - 'A';
      int st = s ^ (1<<tmp);
      int tfl = fl;
      if(ms[tmp][y][x] == '*')tfl = 1-fl;
      if(d[st][tfl][y][x]<0){
	d[st][tfl][y][x] = d[s][fl][y][x] + 1;
	q.push(P2(P(st,tfl),P(y,x)));
      }
    }
  }
  cout << -1 << endl;
}