题意:

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+状压)的更多相关文章

  1. 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 ...

  2. HDU 4771 Stealing Harry Potter's Precious dfs+bfs

    Stealing Harry Potter's Precious Problem Description Harry Potter has some precious. For example, hi ...

  3. HDU 4771 Stealing Harry Potter's Precious

    Stealing Harry Potter's Precious Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 ...

  4. 【HDU 4771 Stealing Harry Potter's Precious】BFS+状压

    2013杭州区域赛现场赛二水... 类似“胜利大逃亡”的搜索问题,有若干个宝藏分布在不同位置,问从起点遍历过所有k个宝藏的最短时间. 思路就是,从起点出发,搜索到最近的一个宝藏,然后以这个位置为起点, ...

  5. HDU Stealing Harry Potter's Precious(状压BFS)

    状压BFS 注意在用二维字符数组时,要把空格.换行处理好. #include<stdio.h> #include<algorithm> #include<string.h ...

  6. hdu 4771 Stealing Harry Potter's Precious (2013亚洲区杭州现场赛)(搜索 bfs + dfs) 带权值的路径

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4771 题目意思:'@'  表示的是起点,'#' 表示的是障碍物不能通过,'.'  表示的是路能通过的: ...

  7. hdu 4771 Stealing Harry Potter&#39;s Precious(bfs)

    题目链接:hdu 4771 Stealing Harry Potter's Precious 题目大意:在一个N*M的银行里,贼的位置在'@',如今给出n个宝物的位置.如今贼要将全部的宝物拿到手.问最 ...

  8. Stealing Harry Potter's Precious BFS+DFS

    Problem Description Harry Potter has some precious. For example, his invisible robe, his wand and hi ...

  9. HDU 5025:Saving Tang Monk(BFS + 状压)

    http://acm.hdu.edu.cn/showproblem.php?pid=5025 Saving Tang Monk Problem Description   <Journey to ...

随机推荐

  1. 查看Win10商店应用更新日期

    查看Win10商店应用更新日期 需要用到一个工具--WP Snitch,网址 https://wpsnitch.appspot.com/ 打开网址后他会给出一个示例,比如给出的是 https://ww ...

  2. PHP怎么遍历对象?

    对于php来说,foreach是非常方便好用的一个语法,几乎对于每一个PHPer它都是日常接触最多的请求之一.那么对象是否能通过foreach来遍历呢? 答案是肯定的,但是有个条件,那就是对象的遍历只 ...

  3. DISCUZ论坛添加页头及页尾背景图片的几种方法

    先给大家分享页头添加背景图片的两种方法:1. 第一种效果,是给discuz的整体框架添加背景图片,见图示: 添加方法如下:找到你现在使用模板common文件下的header.html文件,在<h ...

  4. Docker系列(8)- 常用其他命令(1) | 日志、元数据、进程的查看

    后台启动容器 # 命令 docker run -d 镜像名 [root@localhost ~]# docker run -d centos #问题:docker ps,发现centos停止了 #常见 ...

  5. FastAPI logger日志记录方案 loguru模块

    实现方式: 采用 loguru 模块.跟flask直接挂载到app上有区别,当然也可以尝试去这样做. 但是 好像没有这个必要.要的就是个快速.整那些子虚乌有的东西完全木有意义. 1.首先是去项目git ...

  6. PolarDB PostgreSQL 快速入门

    什么是PolarDB PostgreSQL PolarDB PostgreSQL(下文简称为PolarDB)是一款阿里云自主研发的云原生数据库产品,100%兼容PostgreSQL,采用基于Share ...

  7. 多个ssh key 配置多个网址

    多个 ssh key 配置多个网站 一.生成ssh key ssh-keygen -t rsa -C "你的邮箱" -f ~/.ssh/id_rsa_one ssh-keygen ...

  8. SpringSecurity系列学习(一):初识SpringSecurity

    SpringSecurity Spring Security是spring采用AOP思想,基于servlet过滤器实现的安全框架.它提供了完善的认证机制和方法级的 授权功能.是一款非常优秀的权限管理框 ...

  9. 51nod1355-斐波那契的最小公倍数【min-max容斥】

    正题 题目链接:http://www.51nod.com/Challenge/Problem.html#problemId=1355 题目大意 定义\(f_i\)表示斐波那契的第\(i\)项,给出一个 ...

  10. windows环境下配置Kafaka

    一.安装Zookeeper(Kafaka依赖于zookeeper进行服务注册和管理) 1. 1   下载zookeeper:http://mirror.bit.edu.cn/apache/zookee ...