Hdu 4771 Stealing Harry Potter's Precious (搜索)

题目大意:

地图上有最多4件物品,小偷要全部拿走,问最少的路程。


思路分析:

考虑到物品数量只有4。

可以先用最多5次bfs求出每个目标点到其他目标点的距离。

然后枚举依次拿取物品的顺序,用next_permutation...


#include 
#include 
#include 
#include 
#include 

using namespace std;

char mp[105][105];
struct node
{
    int x,y,step;
};
int dis[5][5];
int dx[] = {0,0,-1,1};
int dy[] = {-1,1,0,0};
int n,m;

bool ok(node t)
{
    if(t.x >= 0 && t.x < n && t.y>=0 && t.y < m)return true;
    return false;
}
bool vis[105][105];
void bfs(node st,int id)
{
    queueQ;
    memset(vis,false,sizeof vis);
    vis[st.x][st.y]=true;

    node w,e;
    Q.push(st);

    while(!Q.empty())
    {
        w=Q.front();
        Q.pop();

        for(int d=0;d<4;d++)
        {
            e=w;
            e.x+=dx[d];
            e.y+=dy[d];
            e.step++;
            if(ok(e) && !vis[e.x][e.y] && mp[e.x][e.y]!='#')
            {
                if(mp[e.x][e.y]=='0' || mp[e.x][e.y]=='1' || mp[e.x][e.y]=='2' || mp[e.x][e.y]=='3' || mp[e.x][e.y]=='4')
                {
                    dis[id][mp[e.x][e.y]-'0'] = min(e.step,dis[id][mp[e.x][e.y]-'0']);
                    dis[mp[e.x][e.y]-'0'][id] = min(e.step,dis[mp[e.x][e.y]-'0'][id]);
                }
                vis[e.x][e.y]=true;
                Q.push(e);
            }
        }
    }
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(n==0 && m==0)break;

        node init;

        for(int i=0;i


你可能感兴趣的:(搜索)