• HDU_5094_dfs


    http://acm.hdu.edu.cn/showproblem.php?pid=5094

    bfs,vis[x][y][z],z表示钥匙的状态,用二进制来表示,key[x][y]储存当前位置钥匙的二进制表示。

    注意起始点有钥匙的情况。

    #include<iostream>
    #include<cstdio>
    #include<string>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    using namespace std;
    
    struct point
    {
        int x,y,counts,mykey;
    }start;
    
    int n,m,p,k,s,flag,door[55][55][55][55],key[55][55],vis[55][55][2048],dir[4][2] = {-1,0,0,-1,1,0,0,1};
    
    int main()
    {
        while(~scanf("%d%d%d",&n,&m,&p))
        {
            memset(door,-1,sizeof(door));
            memset(key,0,sizeof(key));
            memset(vis,0,sizeof(vis));
            scanf("%d",&k);
            int x1,y1,x2,y2,type;
            while(k--)
            {
                scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&type);
                door[x1][y1][x2][y2] = type;
                door[x2][y2][x1][y1] = type;
            }
            scanf("%d",&s);
            int x,y,keytype;
            while(s--)
            {
                scanf("%d%d%d",&x,&y,&keytype);
                key[x][y] |= 1<<(keytype-1);
            }
            queue<point> q;
            start.x = 1;
            start.y = 1;
            start.counts = 0;
            start.mykey = key[1][1];
            vis[1][1][start.mykey] = 1;
            q.push(start);
            flag = 1;
            while(!q.empty())
            {
    
                point now = q.front();
                q.pop();
                if(now.x == n && now.y == m)
                {
                    flag = 0;
                    printf("%d
    ",now.counts);
                    break;
                }
                for(int i = 0;i < 4;i++)
                {
                    point temp;
                    temp.x = now.x+dir[i][0];
                    temp.y = now.y+dir[i][1];
                    if(temp.x < 1 || temp.x > n || temp.y < 1 || temp.y > m)    continue;
                    if(door[now.x][now.y][temp.x][temp.y] != -1 &&(now.mykey & 1<<(door[now.x][now.y][temp.x][temp.y]-1)) == 0)  continue;
                    temp.counts = now.counts+1;
                    temp.mykey = now.mykey | key[temp.x][temp.y];
                    if(vis[temp.x][temp.y][temp.mykey]) continue;
                    vis[temp.x][temp.y][temp.mykey] = 1;
                    q.push(temp);
                }
            }
            if(flag)    printf("-1
    ");
        }
    }
  • 相关阅读:
    Mac下发布Unity3d中Android平台下出现“android (invokation failed)”的错误
    图片切换特过渡效果
    文件下载
    ASP.NET前台绑定后台变量方法总结
    C# aspx 数据绑定集中 Bind Eval DataBinder.Eval
    MVC 3 Excel文件下载
    ASP.NET中Get和Post的用法 Request.QueryString,Request.Form,Request.Params的区别
    删除掉前一天文件夹里面的文件
    友情链接
    博客园管理记录
  • 原文地址:https://www.cnblogs.com/zhurb/p/5875790.html
Copyright © 2020-2023  润新知