POJ1324(Astar)

题目链接:http://poj.org/problem?id=1324

状态记录:只记录头的位置,其他的部分只要从头部开始沿着连续的方向走就好了,所以只要记录身体与前一段的方向即可。

单纯的BFS也能做,这里作为练习用A*。

先贴A*模板

struct node
{
    type s;
    int f, g, h;
}st,ed;
short c[];
int f[];

struct cmp
{
    int operator () (const node& a, const node& b)
    {
        if (a.f == b.f)
            return a.g > b.g;
        return a.f > b.f;
    }
};

int GetH(const node& now)
{
    
}

int GetS(const node& now)
{
    
}

bool next(const node& now, snake& New, int i)
{
    
}

int Astar()
{
    memset(c,0,sizeof(c));
    priority_queue<snake, vector<snake>, cmp >Q;

    c[st.s] = 1;
    st.f = st.h = GetH(st);
    f[st.s] = st.f;
    st.g = 0;
    Q.push(st);
    while (!Q.empty())
    {
        node now = Q.top();
        Q.pop();

        if (now.s == ed.s)
            return now.g;

        node New;
        for (int i=0; i<4; i++)
            if (next(now,New,i))
        {
            s = GetS(New);
            New.g = now.g + 1;
            New.h = GetH(New);
            New.f = New.g + New.h;

            if (c[New.s] == 1)
            {
                if (New.f < f[New.s])
                {
                    f[New.s] = New.f;
                    Q.push(New);
                }
            }
            else if (c[s] == 2)
            {
                if (New.f < f[New.s])
                {
                    f[New.s] = New.f;
                    c[New.s] = 1;
                    Q.push(New);
                }
            }
            else
            {
                f[New.s] = New.f;
                c[New.s] = 1;
                Q.push(New);
            }

        }
        c[now.s] = 2;
    }

    return -1;
}

题目代码:

#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#include <string>
#include <map>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;

int POW[7];
const int dx[4] = {-1,0,1,0};
const int dy[4] = {0,1,0,-1};
struct Head
{
    int x, y;
};
struct snake
{
    Head head;
    short len;
    short body[7];
    int f, g, h;
}Holedox;
bool mp[25][25];
bool vis[25][25][16400];
short c[25][25][16400];
int f[25][25][16400];
int n, m, L, K;

struct cmp
{
    int operator () (const snake& a, const snake& b)
    {
        if (a.f == b.f)
            return a.g > b.g;
        return a.f > b.f;
    }
};

int GetH(const snake& now)
{
    return abs(now.head.x-1) + abs(now.head.y-1);
}

int GetS(const snake& now)
{
    int ret = 0;
    for (int i=0; i<now.len; i++)
        ret += now.body[i]*POW[i];
    return ret;
}

bool next(const snake& now, snake& New, int i)
{
    int newx = now.head.x+dx[i];
    int newy = now.head.y+dy[i];
    if (newx < 1 || newx > n || newy < 1 || newy > m)
        return false;
    if (!mp[newx][newy])
        return false;
    int nowx = now.head.x, nowy = now.head.y;
    for (int i=0; i<now.len; i++)
    {
        nowx += dx[now.body[i]]; nowy += dy[now.body[i]];
        if (nowx == newx && nowy == newy)
            return false;
    }

    New.head.x = newx; New.head.y = newy;
    New.body[0] = (i+2) % 4;
    for (int i=1; i<now.len; i++)
        New.body[i] = now.body[i-1];
    New.len = now.len;
    return true;
}

int Astar()
{
    memset(c,0,sizeof(c));
    priority_queue<snake, vector<snake>, cmp >Q;

    int s = GetS(Holedox);
    c[Holedox.head.x][Holedox.head.y][s] = 1;
    Holedox.f = Holedox.h = GetH(Holedox);
    f[Holedox.head.x][Holedox.head.y][s] = Holedox.f;
    Holedox.g = 0;
    Q.push(Holedox);
    while (!Q.empty())
    {
        snake now = Q.top();
        Q.pop();

        if (now.head.x == 1 && now.head.y == 1)
            return now.g;

        snake New;
        for (int i=0; i<4; i++)
            if (next(now,New,i))
        {
            s = GetS(New);
            New.g = now.g + 1;
            New.h = GetH(New);
            New.f = New.g + New.h;

            if (c[New.head.x][New.head.y][s] == 1)
            {
                if (New.f < f[New.head.x][New.head.y][s])
                {
                    f[New.head.x][New.head.y][s] = New.f;
                    Q.push(New);
                }
            }
            else if (c[New.head.x][New.head.y][s] == 2)
            {
                if (New.f < f[New.head.x][New.head.y][s])
                {
                    f[New.head.x][New.head.y][s] = New.f;
                    c[New.head.x][New.head.y][s] = 1;
                    Q.push(New);
                }
            }
            else
            {
                f[New.head.x][New.head.y][s] = New.f;
                c[New.head.x][New.head.y][s] = 1;
                Q.push(New);
            }

        }
        c[now.head.x][now.head.y][s] = 2;
    }

    return -1;
}

int main()
{
    int cnt = 0;

    POW[0] = 1;
    for (int i=1; i<7; i++)
        POW[i] = POW[i-1]*4;

    while (scanf("%d%d%d",&n,&m,&L))
    {
        if (n == 0 && m == 0 && L == 0)
            break;

        cnt++;

        Holedox.len = L-1;
        scanf("%d%d",&Holedox.head.x,&Holedox.head.y);
        int lastx = Holedox.head.x, lasty = Holedox.head.y;
        for (int i=0; i<L-1; i++)
        {
            int x, y;
            scanf("%d%d",&x,&y);
            switch (x-lastx)
            {
                case -1: Holedox.body[i] = 0; break;
                case 1 : Holedox.body[i] = 2; break;
                case 0 : Holedox.body[i] = y-lasty == -1 ? 3 : 1;
            }
            lastx = x; lasty = y;
        }
        memset(mp,true,sizeof(mp));
        scanf("%d",&K);
        for (int i=0; i<K; i++)
        {
            int x, y;
            scanf("%d%d",&x,&y);
            mp[x][y] = false;
        }

        int ans = Astar();

        printf("Case %d: %d\n",cnt, ans);
    }

    return 0;
}


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值