HDU 4230 Robot Navigation (记忆化广度优先搜索)

来源:互联网 发布:淘宝账号被冻结支付宝 编辑:程序博客网 时间:2024/05/23 02:02

这题WA了十多次。昨晚调试了一晚上,今天下午又调试了好久还是AC不能,于是决定重敲。重敲之后一次AC,然后又进行了一些优化,109ms。


Run IDSubmit TimeJudge StatusPro.IDExe.TimeExe.MemoryCode Len.LanguageAuthor58626522012-04-28 15:44:41Accepted4230109MS956K4406 BG++ahfywff

#include <iostream>#include <cstdio>#include <cstring>#include <queue>using namespace std;const int maxn = 111;const int mod = 1000000;const int oo = 1 << 30;int n, m;char map[maxn][maxn];int sx, sy, sd, tx, ty;int dir[4][2] = {{2, 3}, {3, 2}, {1, 0}, {0, 1}};int steps[maxn][maxn][4];int sum[maxn][maxn][4];struct Node{    int x, y, d;};queue<Node> Q;void walk(Node cur, Node next){    if (steps[next.x][next.y][next.d] == -1)    {        steps[next.x][next.y][next.d] = steps[cur.x][cur.y][cur.d] + 1;        sum[next.x][next.y][next.d] += sum[cur.x][cur.y][cur.d];        sum[next.x][next.y][next.d] %= mod;        Q.push(next);    }    else if (steps[next.x][next.y][next.d] == steps[cur.x][cur.y][cur.d] + 1)    {        sum[next.x][next.y][next.d] += sum[cur.x][cur.y][cur.d];        sum[next.x][next.y][next.d] %= mod;    }}bool ok(Node n){    return (map[n.x][n.y] != '*');}void bfs(){    for (int i = 0; i < n; ++i)    {        for (int j = 0; j < m; ++j)        {            for (int k = 0; k < 4; ++k)            {                sum[i][j][k] = 0;                steps[i][j][k] = -1;            }        }    }        while (!Q.empty())        Q.pop();    Node s;    s.x = sx; s.y = sy; s.d = sd;    steps[sx][sy][sd] = 0;    sum[sx][sy][sd] = 1;    Q.push(s);    Node cur, next;        int minSteps = oo;    while (!Q.empty())    {        cur = Q.front();        Q.pop();                if (steps[cur.x][cur.y][cur.d] > minSteps)            continue;        if (map[cur.x][cur.y] == 'X')        {            minSteps = min(minSteps, steps[cur.x][cur.y][cur.d]);            continue;        }                for (int i = 0; i < 2; ++i)        {            next = cur;            next.d = dir[cur.d][i];            walk(cur, next);        }                if (cur.d == 0)        {            for (int i = 1; cur.x - i >= 0; ++i)            {                next = cur;                next.x = cur.x - i;                if (ok(next))                {                    walk(cur, next);                }                else break;            }        }        else if (cur.d == 1)        {            for (int i = 1; cur.x + i < n; ++i)            {                next = cur;                next.x = cur.x + i;                if (ok(next))                {                    walk(cur, next);                }                else break;            }        }        else if (cur.d == 2)        {            for (int i = 1; cur.y - i >= 0; ++i)            {                next = cur;                next.y = next.y - i;                if (ok(next))                {                    walk(cur, next);                }                else break;            }        }        else if (cur.d == 3)        {            for (int i = 1; cur.y + i < m; ++i)            {                next = cur;                next.y = next.y + i;                if (ok(next))                {                    walk(cur, next);                }                else break;            }        }    }    if (minSteps == oo)        printf("0 0\n");    else    {        int cnt = 0;        for (int i = 0; i < 4; ++i)            if (steps[tx][ty][i] == minSteps)                cnt += sum[tx][ty][i];        printf("%d %d\n", minSteps, cnt % mod);    }}int main(){    //freopen("in.txt", "r", stdin);    //freopen("out.txt", "w", stdout);    while (scanf("%d %d", &n, &m))    {        if (n == 0 && m == 0)            return 0;                for (int i = 0; i < n; ++i)        {            scanf("%s", map[i]);            for (int j = 0; j < m; ++j)            {                if (map[i][j] != '.' && map[i][j] != '*' && map[i][j] != 'X')                {                    sx = i, sy = j;                    if (map[i][j] == 'N') sd = 0;                    else if (map[i][j] == 'S') sd = 1;                    else if (map[i][j] == 'W') sd = 2;                    else if (map[i][j] == 'E') sd = 3;                }                if (map[i][j] == 'X')                    tx = i, ty = j;            }        }        bfs();    }    return 0;}