文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>状态压缩的搜索 Tianjin University 2010 ACM-ICPC Team Sellect Contest 2 1008解题报告

状态压缩的搜索 Tianjin University 2010 ACM-ICPC Team Sellect Contest 2 1008解题报告

时间:2010-09-09  来源:ronaflx

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int SIZE = 52, CAP = (1 << 5), INF = 0x7f7f7f7f;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, -1, 1};

 

struct pos
{
    int x, y, s;
} beg, end, tmp, nxt;
int r, c, key;
int status[SIZE][SIZE], blood[SIZE][SIZE][CAP];
char maps[SIZE][SIZE];

int bfs()
{
    int ans = INF;
    beg.s = 0;
    blood[beg.x][beg.y][beg.s] = 0;
    queue<pos> q;
    q.push(beg);
    while (!q.empty())
    {
        beg = q.front();
        q.pop();
        for (int i = 0; i < 4; i++)
        {
            nxt.x = beg.x + dx[i], nxt.y = beg.y + dy[i], nxt.s = beg.s;
            if (nxt.x > 0 && nxt.y > 0 && nxt.x <= r && nxt.y <= c && maps[nxt.x][nxt.y] != '#')
            {
                if (maps[nxt.x][nxt.y] >= 'A' && maps[nxt.x][nxt.y] <= 'E')
                    if (!((1 << (maps[nxt.x][nxt.y] - 'A')) & nxt.s))
                        continue;
                nxt.s |= status[nxt.x][nxt.y];
                int delt = 0;
                if (maps[nxt.x][nxt.y] >= '1' && maps[nxt.x][nxt.y] <= '9')
                    delt = maps[nxt.x][nxt.y] - '0';
                if (maps[nxt.x][nxt.y] >= 'A' && maps[nxt.x][nxt.y] <= 'E')
                {
                    if (!((1 << (maps[nxt.x][nxt.y] - 'A')) & nxt.s))
                        continue;
                    if (blood[nxt.x][nxt.y][nxt.s] > blood[beg.x][beg.y][beg.s] + delt)
                    {
                        blood[nxt.x][nxt.y][nxt.s] = blood[beg.x][beg.y][beg.s] + delt;
                        q.push(nxt);
                    }
                }
                else if (blood[nxt.x][nxt.y][nxt.s] > blood[beg.x][beg.y][beg.s] + delt)
                {
                    blood[nxt.x][nxt.y][nxt.s] = blood[beg.x][beg.y][beg.s] + delt;
                    q.push(nxt);
                }
                if (maps[nxt.x][nxt.y] == 'T')
                    ans = min(ans, blood[nxt.x][nxt.y][nxt.s]);
            }
        }
    }
    if (ans != INF)
        return ans;
    return -1;
}

int main()
{
    int txt, x, y;
    scanf("%d", &txt);
    while (txt--)
    {
        scanf("%d %d %d", &r, &c, &key);
        memset(status, 0, sizeof (status));
        memset(blood, 0x7f, sizeof (blood));
        for (int i = 1; i <= r; i++)
            scanf("%s", maps[i] + 1);
        for (int i = 0; i < key; i++)
        {
            scanf("%d %d", &x, &y);
            status[x][y] |= (1 << i);
        }
        for (int i = 1; i <= r; i++)
            for (int j = 1; j <= c; j++)
                if (maps[i][j] == 'S')
                    beg.x = i, beg.y = j;
        printf("%d\n", bfs());
    }
    return 0;
}

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载