HDU2216 Game III

来源:互联网 发布:我的世界随机性知乎 编辑:程序博客网 时间:2024/06/05 20:47

Problem Description

Zjt and Sara will take part in a game, named Game III. Zjt and Sara will be in a maze, and Zjt must find Sara. There are some strang rules in this maze. If Zjt move a step, Sara will move a step in opposite direction.
Now give you the map , you shold find out the minimum steps, Zjt have to move. We say Zjt meet Sara, if they are in the same position or they are adjacent .
Zjt can only move to a empty position int four diraction (up, left, right, down). At the same time, Sara will move to a position in opposite direction, if there is empty. Otherwise , she will not move to any position.
The map is a N*M two-dimensional array. The position Zjt stays now is marked Z, and the position, where Sara stays, is marked E.

. : empty position
X: the wall
Z: the position Zjt now stay
S: the position Sara now stay

Your task is to find out the minimum steps they meet each other.

Input

The input contains several test cases. Each test case starts with a line contains three number N ,M (2<= N <= 20, 2 <= M <= 20 ) indicate the size of the map. Then N lines follows, each line contains M character. A Z and a S will be in the map as the discription above.

Output

For each test case, you should print the minimum steps. “Bad Luck!” will be print, if they can’t meet each other.

Sample Input

4 4
XXXX
.Z..
.XS.
XXXX
4 4
XXXX
.Z..
.X.S
XXXX
4 4
XXXX
.ZX.
.XS.
XXXX

Sample Output

1
1
Bad Luck!

Author

zjt

除了特殊的標記之外這個題沒什麼套路。
我卻在輸入上WA了10次,不禁感慨技不如人。
下面是AC代碼:

#include<iostream>#include<cstring>#include<cstdio>#include<queue>#include<cmath>using namespace std;//千万不要小瞧人生啊!int n, m;char s[25][25];int v[25][25][25][25];int vis[25][25];int mov[4][2] = { 0,1,0,-1,1,0,-1,0 };struct node {    int x;    int y;    int ti;//步骤数    int x1;//S当前位置    int y1;};int check(node t){    if (t.y >= m || t.x < 0 || t.x >= n || t.y < 0)        return 0;    if (t.y1 >= m || t.x1 < 0 || t.x1 >= n || t.y1 < 0)        return 0;    if (vis[t.x][t.y] == 1)        return 0;    if (vis[t.x1][t.y1] == 1)        return 0;    return 1;}int anscheck(node t){    /*for (int i = 0; i < 4; i++)    {        if ((t.x1 + mov[i][0]) == t.x && (t.y1 + mov[i][1]) == t.y)            return 1;    }*/    if (abs(t.x - t.x1) + abs(t.y - t.y1) <= 1)    {        return 1;    }    return 0;}int check2(int x,int y){    if (vis[x][y] == 1)//如果是墻        return 1;    if (y >= m || x < 0 || x >= n || y < 0)        return 1;    return 0;}node pre;//初始位置void bfs(){    bool flag = 0;    queue<node> q;    while (!q.empty())        q.pop();    q.push(pre);    while (!q.empty())    {        node temp;        temp = q.front();        q.pop();        v[temp.x][temp.y][temp.x1][temp.y1]=1;//走過        if (anscheck(temp))//檢測到結果        {            printf("%d\n", temp.ti);            //return;            flag = 1;            break;        }        node nxt;        for (int i = 0; i < 4; i++)        {            nxt.x = temp.x + mov[i][0];            nxt.y = temp.y + mov[i][1];            nxt.x1 = temp.x1 - mov[i][0];            nxt.y1 = temp.y1 - mov[i][1];            if (check2(nxt.x1, nxt.y1))//如果S被擋住 退回;            {                nxt.x1 = temp.x1;                nxt.y1 = temp.y1;            }            if (check(nxt)&&v[nxt.x][nxt.y][nxt.x1][nxt.y1]==0)            {                nxt.ti = temp.ti + 1;                q.push(nxt);            }        }    }    if(!flag) printf("Bad Luck!\n");}int main(){    while (scanf("%d%d", &n, &m) != EOF)    {        memset(v, 0, sizeof(v));        memset(vis, 0, sizeof(vis));        for (int i = 0; i<n; i++)            scanf("%s", &s[i]);        for (int i = 0; i<n; i++)        {            for (int j = 0; j<m; j++)            {                if (s[i][j] == 'S')                {                    pre.x1 = i;                    pre.y1 = j;                }                if (s[i][j] == 'Z')                {                    pre.x = i;                    pre.y = j;                                  }                if (s[i][j] == 'X')                {                    vis[i][j] = 1;                }            }        }        v[pre.x][pre.y][pre.x1][pre.y1] = 1;        bfs();    }    return 0;}