hdu 4771 Stealing Harry Potter's Precious (BFS+状压)
题意:
n*m的迷宫,有一些格能走(“.”),有一些格不能走(“#”)。起始点为“@”。
有K个物体。(K<=4),每个物体都是放在“.”上。
问最少花多少步可以取完所有物体。
思路:
BFS+状压,看代码。
代码:
struct node{
    int x,s;
    node(int _x,int _s){
        x=_x, s=_s;
    }
};
int n,m,k,sx,sy;
char graph[105][105];
int px[5],py[5];
int dp[105][105][35];
int bfs(){
    queue<node> q;
    mem(dp,-1);
    int s=0;
    rep(i,1,k) if(px[i]==sx&&py[i]==sy) s=(1<<(i-1));
    dp[sx][sy][s]=0;
    q.push(node(sx*1000+sy,s));
    if(s==((1<<k)-1))
        return dp[sx][sy][s];
    while(!q.empty()){
        node f=q.front();  q.pop();
        rep(i,0,3){
            int nx=f.x/1000+uu[i], ny=f.x%1000+vv[i];
            int s=f.s;
            if(nx<0||ny<0||nx>=n||ny>=m) continue;
            if(graph[nx][ny]=='#') continue;
            rep(j,1,k) if(nx==px[j]&&ny==py[j]){
                s|=(1<<(j-1));
            }
            if(dp[nx][ny][s]!=-1) continue;
            dp[nx][ny][s]=dp[f.x/1000][f.x%1000][f.s]+1;
            q.push(node(nx*1000+ny,s));
            if(s==((1<<k)-1))
                return dp[nx][ny][s];
        }
    }
    return -1;
}
int main(){
    //freopen("test.in","r", stdin);
    while(scanf("%d%d",&n,&m),n||m){
        rep(i,0,n-1){
            scanf("%s",graph[i]);
            rep(j,0,m-1) if(graph[i][j]=='@'){
                sx=i, sy=j;
            }
        }
        scanf("%d",&k);
        rep(i,1,k) { scanf("%d%d",&px[i],&py[i]); --px[i], --py[i]; }
        printf("%d\n",bfs());
    }
    //fclose(stdin);
}
hdu 4771 Stealing Harry Potter's Precious (BFS+状压)的更多相关文章
- HDU 4771 Stealing Harry Potter's Precious (2013杭州赛区1002题,bfs,状态压缩)
		
Stealing Harry Potter's Precious Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 ...
 - HDU 4771 Stealing Harry Potter's Precious  dfs+bfs
		
Stealing Harry Potter's Precious Problem Description Harry Potter has some precious. For example, hi ...
 - HDU 4771    Stealing Harry Potter's Precious
		
Stealing Harry Potter's Precious Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 ...
 - 【HDU 4771 Stealing Harry Potter's Precious】BFS+状压
		
2013杭州区域赛现场赛二水... 类似“胜利大逃亡”的搜索问题,有若干个宝藏分布在不同位置,问从起点遍历过所有k个宝藏的最短时间. 思路就是,从起点出发,搜索到最近的一个宝藏,然后以这个位置为起点, ...
 - HDU  Stealing Harry Potter's Precious(状压BFS)
		
状压BFS 注意在用二维字符数组时,要把空格.换行处理好. #include<stdio.h> #include<algorithm> #include<string.h ...
 - hdu 4771 Stealing Harry Potter's Precious (2013亚洲区杭州现场赛)(搜索 bfs + dfs) 带权值的路径
		
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4771 题目意思:'@' 表示的是起点,'#' 表示的是障碍物不能通过,'.' 表示的是路能通过的: ...
 - hdu 4771 Stealing Harry Potter's Precious(bfs)
		
题目链接:hdu 4771 Stealing Harry Potter's Precious 题目大意:在一个N*M的银行里,贼的位置在'@',如今给出n个宝物的位置.如今贼要将全部的宝物拿到手.问最 ...
 - Stealing Harry Potter's Precious BFS+DFS
		
Problem Description Harry Potter has some precious. For example, his invisible robe, his wand and hi ...
 - HDU 5025:Saving Tang Monk(BFS + 状压)
		
http://acm.hdu.edu.cn/showproblem.php?pid=5025 Saving Tang Monk Problem Description <Journey to ...
 
随机推荐
- 使用manacher算法解决最长回文子串问题
			
要解决的问题 求一个字符串最长回文子串是什么.且时间复杂度 O(N) 具体描述可参考: LeetCode_5_最长回文子串 LintCode_200_最长回文子串 暴力解法 以每个字符为中心向左右两边 ...
 - shell 脚本获取数组字符串长度
			
#!/bin/sh source /etc/init.d/functions funOne() { array=(I am dfh kjlhfjksdf sdfj jdkfhaskl mjjoldfu ...
 - windows2012添加ssl证书
			
第一步: 先下载 rewrite_x64_zh-cn.msi ,并安装 (*这个是2.0版本,千万不要安装2.1版本,否则导致网站进程池全部关闭) https://www.microsoft. ...
 - [源码解析] PyTorch 流水线并行实现 (3)--切分数据和运行时系统
			
[源码解析] PyTorch 流水线并行实现 (3)--切分数据和运行时系统 目录 [源码解析] PyTorch 流水线并行实现 (3)--切分数据和运行时系统 0x00 摘要 0x01 分割小批次 ...
 - Gitee自动化部署python脚本
			
一.前期准备 1.1 安装环境 1.安装python3 2.打开命令行安装selenium pip install selenium 二.python代码 2.1 源码 #!/usr/bin/pyth ...
 - 简易集成websocket技术实现消息推送
			
Websocket 简介 首先介绍下WebSocket,它是一种网络通信技术,该技术最大的特点就是,服务器端可以主动往客户端发送消息:当然,客户端也可以主动往服务器发送消息,实现两端的消息通信,属于网 ...
 - mysql数据备份及恢复详细操作
			
一.数据库数据备份 1.全备 BakDir=/backup/full #创建全备目录 LogFile=/backup/full/bak.log #创建备份日志 Date=`date +%Y%m%d` ...
 - Java秘诀!Java逻辑运算符介绍
			
运算符丰富是 Java 语言的主要特点之一,它提供的运算符数量之多,在高级语言中是少见的. Java 语言中的运算符除了具有优先级之外,还有结合性的特点.当一个表达式中出现多种运算符时,执行的先后顺序 ...
 - C语言的return语句
			
Q1:函数中的Return语句有什么用? Q2:Return 0有什么含义吗? A1:Return的作用为,跳出当前的函数,并且返回到调用当前函数的主调函数,当前函数中Return语句一下代码将不会运 ...
 - kafka初认识(一)
			
首先贴出官网地址:https://kafka.apache.org/ 一. 简介 Kafka 是 linkedin 使用 Scala 编写具有高水平扩展和高吞吐量的分布式消息系统.Kafka 对消息保 ...