愛悠閑 > HDU 5025 Saving Tang Monk

HDU 5025 Saving Tang Monk

分類: 搜索  |  作者: u013007900 相關  |  發布日期 : 2014-10-07  |  熱度 : 38°

bfs()

記錄 x , y , step , num of killing snakes , num of your keys


//By LH
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

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

int n, m, sx, sy, ex, ey, ans;
char a[102][102];
bool v[102][102][10][32];
int stot, snake[6][2];
int qx[4000000], qy[4000000], qstep[4000000], qk[4000000], qs[4000000];

void bfs()
{
    int i, j, k, l;
    int head, tail, tx, ty, ts, tmp;
    
    stot = 0;
    for (i = 0; i < n; i++)
    for (j = 0; j < n; j++)
    if (a[i][j] == 'K')
    {
        sx = i; sy = j;
    }
    else if (a[i][j] == 'T')
    {
        ex = i; ey = j;
    }
    else if (a[i][j] == 'S')
    {
        stot++;
        snake[stot][0] = i;
        snake[stot][1] = j;
    }
    for (i = 0; i < n; i++)
    for (j = 0; j < n; j++)
    for (k = 0; k <= m; k++)
    for (l = 0; l <= ((1<<stot)-1); l++) v[i][j][k][l] = false;
    head = 1; tail = 1; qx[1] = sx; qy[1] = sy; qk[1] = 0; qs[1] = 0;
    v[sx][sy][0][0] = true; qstep[head] = 0; ans = 1000000000;
    while (head <= tail)
    {
        for (i = 0; i < 4; i++)
        {
            tx = qx[head] + dx[i];
            ty = qy[head] + dy[i];
            if (tx < 0 || tx > n-1 || ty < 0 || ty > n-1) continue;
            if (a[tx][ty] == '#') continue;
            if (a[tx][ty] == 'T')
            {
                if (qk[head] == m && !v[tx][ty][qk[head]][qs[head]])
                {
                    ans = min(qstep[head]+1, ans);
                    tail++;
                    qx[tail] = tx;
                    qy[tail] = ty;
                    qk[tail] = qk[head];
                    qs[tail] = qs[head];
                    qstep[tail] = qstep[head]+1;
                }
                else
                {
                    if (!v[tx][ty][qk[head]][qs[head]])
                    {
                        tail++;
                        qx[tail] = tx;
                        qy[tail] = ty;
                        qk[tail] = qk[head];
                        qs[tail] = qs[head];
                        qstep[tail] = qstep[head]+1;
                        v[tx][ty][qk[tail]][qs[tail]] = true;
                    }
                }
            }
            else if (a[tx][ty] == 'S')
            {
                for (j = 1; j <= stot; j++)
                if (snake[j][0]== tx && snake[j][1] == ty)
                {
                    tmp = j;
                    break;
                }
                if ((qs[head]&(1<<(tmp-1))) > 0)
                {
                    if (!v[tx][ty][qk[head]][qs[head]])
                    {
                        tail++;
                        qx[tail] = tx;
                        qy[tail] = ty;
                        qk[tail] = qk[head];
                        qs[tail] = qs[head];
                        qstep[tail] = qstep[head]+1;
                        v[tx][ty][qk[tail]][qs[tail]] = true;
                    }
                }
                else
                {
                    ts = (qs[head]|(1<<(tmp-1)));
                    if (!v[tx][ty][qk[head]][ts])
                    {
                        tail++;
                        qx[tail] = tx;
                        qy[tail] = ty;
                        qk[tail] = qk[head];
                        qs[tail] = ts;
                        qstep[tail] = qstep[head]+2;
                        v[tx][ty][qk[tail]][qs[tail]] = true;
                    }
                }
            }
            else if (a[tx][ty] >= '1' && a[tx][ty] <= '9')
            {
                if (!v[tx][ty][qk[head]][qs[head]])
                {
                    tail++;
                    qx[tail] = tx;
                    qy[tail] = ty;
                    qk[tail] = qk[head];
                    qs[tail] = qs[head];
                    qstep[tail] = qstep[head]+1;
                    v[tx][ty][qk[tail]][qs[tail]] = true;
                }
                if (a[tx][ty]-'0' != qk[head]+1) continue;
                if (!v[tx][ty][qk[head]+1][qs[head]])
                {
                    tail++;
                    qx[tail] = tx;
                    qy[tail] = ty;
                    qk[tail] = qk[head]+1;
                    qs[tail] = qs[head];
                    qstep[tail] = qstep[head]+1;
                    v[tx][ty][qk[tail]][qs[tail]] = true;
                }
            }
            else
            {
                if (!v[tx][ty][qk[head]][qs[head]])
                {
                    tail++;
                    qx[tail] = tx;
                    qy[tail] = ty;
                    qk[tail] = qk[head];
                    qs[tail] = qs[head];
                    qstep[tail] = qstep[head]+1;
                    v[tx][ty][qk[tail]][qs[tail]] = true;
                }
            }
        }
        head++;
    }
}

int main()
{
    int i;
    
    while (1)
    {
        scanf("%d%d", &n, &m);
        if (n == 0 && m == 0) break;
        for (i = 0; i < n; i++) scanf("%s", a[i]);
        bfs();
        if (ans == 1000000000) printf("impossible\n");
        else printf("%d\n", ans);
    }
    return 0;
}


快乐彩中奖说明