2014
03-01

# Nightmare Ⅱ

Last night, little erriyue had a horrible nightmare. He dreamed that he and his girl friend were trapped in a big maze separately. More terribly, there are two ghosts in the maze. They will kill the people. Now little erriyue wants to know if he could find his girl friend before the ghosts find them.
You may suppose that little erriyue and his girl friend can move in 4 directions. In each second, little erriyue can move 3 steps and his girl friend can move 1 step. The ghosts are evil, every second they will divide into several parts to occupy the grids within 2 steps to them until they occupy the whole maze. You can suppose that at every second the ghosts divide firstly then the little erriyue and his girl friend start to move, and if little erriyue or his girl friend arrive at a grid with a ghost, they will die.
Note: the new ghosts also can devide as the original ghost.

The input starts with an integer T, means the number of test cases.
Each test case starts with a line contains two integers n and m, means the size of the maze. (1<n, m<800)
The next n lines describe the maze. Each line contains m characters. The characters may be:
‘.’ denotes an empty place, all can walk on.
‘X’ denotes a wall, only people can’t walk on.
‘M’ denotes little erriyue
‘G’ denotes the girl friend.
‘Z’ denotes the ghosts.
It is guaranteed that will contain exactly one letter M, one letter G and two letters Z.

The input starts with an integer T, means the number of test cases.
Each test case starts with a line contains two integers n and m, means the size of the maze. (1<n, m<800)
The next n lines describe the maze. Each line contains m characters. The characters may be:
‘.’ denotes an empty place, all can walk on.
‘X’ denotes a wall, only people can’t walk on.
‘M’ denotes little erriyue
‘G’ denotes the girl friend.
‘Z’ denotes the ghosts.
It is guaranteed that will contain exactly one letter M, one letter G and two letters Z.

3
5 6
XXXXXX
XZ..ZX
XXXXXX
M.G...
......
5 6
XXXXXX
XZZ..X
XXXXXX
M.....
..G...

10 10
..........
..X.......
..M.X...X.
X.........
.X..X.X.X.
.........X
..XX....X.
X....G...X
...ZX.X...
...Z..X..X

1
1
-1

AC代码如下：

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <cmath>
using namespace std;

int N, M;
bool visit[2][801][801];
int maps[801][801];
int gx, gy, mx, my;
int z[2][2];
int step;
int moves[4][2] = { 1, 0, -1, 0, 0, 1, 0, -1 };
queue< pair<int,int> > q[2];

bool judge( int x, int y ){
if( x >= 0 && x < N && y >= 0 && y < M && maps[x][y] != 0 ){
for( int i = 0; i < 2; i++ ){
if( abs( ( x - z[i][0] ) ) + abs( y - z[i][1] ) <= 2 * step ){//这里不要把x-z[i][0]变成double 不然会超时
return false;
}
}
return true;
}
return false;
}

bool BFS( int num ){
int size = q[num].size();

while( size-- ){
int x = q[num].front().first;
int y = q[num].front().second;
q[num].pop();
if( !judge( x, y ) ){
continue;
}
for( int i = 0; i < 4; i++ ){
int xx = x + moves[i][0];
int yy = y +  moves[i][1];
if( judge( xx, yy ) ){
if( visit[num][xx][yy] ){
continue;
}
if( visit[num^1][xx][yy] ){
return true;
}
visit[num][xx][yy] = true;
q[num].push( make_pair( xx, yy ) );
}
}
}
return false;
}

int solve(){
while( !q[0].empty() ){
q[0].pop();
}
while( !q[1].empty() ){
q[1].pop();
}
q[0].push( make_pair( mx, my ) );
q[1].push( make_pair( gx, gy ) );
visit[0][mx][my] = visit[1][gx][gy] = true;
step = 0;

while( !q[0].empty() || !q[1].empty() ){
step++;
if( BFS( 0 ) ) return step;
if( BFS( 0 ) ) return step;
if( BFS( 0 ) ) return step;
if( BFS( 1 ) ) return step;
}
return -1;
}

int main(){
int Case;
string s;

cin >> Case;
while( Case-- ){
cin >> N >> M;
int temp = 0;
for( int i = 0; i < N; i++ ){
cin >> s;
for( int j = 0; j < M; j++ ){
if( s[j] == 'X' ){
maps[i][j] = 0;
}else if( s[j] == 'Z' ){
maps[i][j] = 0;
z[temp][0] = i;
z[temp++][1] = j;
}else if( s[j] == '.' ){
maps[i][j] = 1;
}else if( s[j] == 'M' ){
mx = i;
my = j;
maps[i][j] = 1;
}else{
gx = i;
gy = j;
maps[i][j] = 1;
}
}
}
memset( visit, false, sizeof( visit ) );

cout << solve() << endl;
}
return 0;
}

1. int half(int *array,int len,int key)
{
int l=0,r=len;
while(l<r)
{
int m=(l+r)>>1;
if(key>array )l=m+1;
else if(key<array )r=m;
else return m;
}
return -1;
}
这种就能避免一些Bug
l,m,r
左边是l,m;右边就是m+1,r;

2. 代码是给出了，但是解析的也太不清晰了吧！如 13 abejkcfghid jkebfghicda
第一步拆分为 三部分 (bejk, cfghi, d) * C(13,3)，为什么要这样拆分，原则是什么？

3. 为什么for循环找到的i一定是素数叻，而且约数定理说的是n=p1^a1*p2^a2*p3^a3*…*pk^ak，而你每次取余都用的是原来的m，也就是n