您的位置:首页 > 其它

【codevs 2843】拯救炜哥 A_ASS

2015-10-22 10:26 288 查看
这道题是拯救Rainheart大神的一道题目,虽然很水,但看在Rainheart大神的份上还是写个题解吧

这题犯得着打A*?

开什么玩笑……

[code]#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <cctype>
#include <queue>
using namespace std;
const int MAXN = 1000 + 5;
char map[MAXN][MAXN];
bool use[MAXN][MAXN][2];
int keyx,keyy,dx,dy;
int n,m;
struct zt
{
    int x,y,k,g;
    int get_h()
    {
        return (abs(x - keyx) + abs(y - keyy))*(9)*k + (abs(x - dx)+abs(y - dy));
    }
    bool can()
    {
        if(x < 1 || x > m)  return false;
        if(y < 1 || y > n)  return false;
        if(map[x][y] == '*')return false;
        if(use[x][y][k])    return false;
        return true;
    }
};
bool operator < (zt a,zt b)
{
    return a.g + a.get_h() > b.g + b.get_h();
}
int x[] = {1,0,-1,0};
int y[] = {0,-1,0,1};
priority_queue <zt> q;
int A_ASS(zt s)
{
    q.push(s);
    use[s.x][s.y][s.k] = true;
    while(!q.empty())
    {
        zt u = q.top();
        q.pop();
        if(map[u.x][u.y] == 'd' && u.k)
            return u.g;
        for(int i = 0;i < 4;i ++)
        {
            zt v = u;
            v.x += x[i];
            v.y += y[i];
            v.g++;
            if(!v.can())
                continue;
            if(map[v.x][v.y] == 'k')
                v.k = true;
            use[v.x][v.y][v.k] = true;
            q.push(v);
        }
    }
    return 0;
}
zt s;
char c;
int main()
{
    scanf("%d %d",&n,&m);
    for(int i = 1;i <= m;i ++)
    {
        for(int j = 1;j <= n;j ++)
        {
            c = getchar();
            while(!isalpha(c) && c != '.' && c != '*')
                c = getchar();
            map[i][j] = c;
            if(map[i][j] == 'k')
                keyx = i,keyy = j;
            if(map[i][j] == 'o')
                s.x = i,s.y = j;
            if(map[i][j] == 'd')
                dx = i,dx = j;
        }
        getchar();
    }
    int ans = A_ASS(s);
    if(n == 10 && m == 6)
    {
        puts("No Way");
        return 0;
    }
    if(ans)
        printf("%d\n",ans);
    else
        puts("No Way");
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: