#include<stdio.h>

#include<string.h>//通过只记录每一步此时点的状态。题非常好

#include<queue>

using namespace std;

#define N 110

int map[N][N][30];//一共最多16种状态,记录状态和此时已得到宝物的多少

char ma[N][N];//读入

struct node {

int x,y,t,h;

};

int chu[N][N];//记录刚开始宝物每个点的宝物的数量

int n,m,total;//total表示得到所有宝物时的状态

int dis[4][2]={1,0,-1,0,0,1,0,-1};//四个方向

int judge(node start) {//判断是否越界

if(start.x<1||start.x>n||start.y<1||start.y>m)

return 0;

return 1;

}

int bfs(node start) {

int i;

queue<node>q;

node cur,next;

memset(map,0,sizeof(map));

start.h=chu[start.x][start.y];

start.t=0;

q.push(start);

map[start.x][start.y][start.h]=1;//初始化

while(!q.empty()) {

cur=q.front ();

if(cur.h==total) 

return cur.t;

q.pop ();

for(i=0;i<4;i++) {

next.x=cur.x+dis[i][0];

next.y=cur.y+dis[i][1];

next.t=cur.t+1;

if(judge(next)) {

next.h=cur.h|chu[next.x][next.y];

if(map[next.x][next.y][next.h]==0&&ma[next.x][next.y]!='#') {//记录不是墙时的状态

map[next.x][next.y][next.h]=1;//这个状态已走过

q.push (next);

}

}

}

}

return -1;

}

int main(){

int i,j,r,a,b,k;

while(scanf("%d%d",&n,&m),n||m) {

for(i=1;i<=n;i++)

scanf("%s",ma[i]+1);

node start;

for(i=1;i<=n;i++)

for(j=1;j<=m;j++)

if(ma[i][j]=='@') {//找起始点

start.x=i;

start.y=j;

}

scanf("%d",&k);

memset(chu,0,sizeof(chu));

r=1; total=0;

for(i=1;i<=k;i++) {

total=total|r;

scanf("%d%d",&a,&b);

chu[a][b]=r;

r*=2;

}

  printf("%d\n",bfs(start));

}

return 0;

}

hdu 4771好题的更多相关文章

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

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

  2. HDU-1042-N!(Java大法好 &amp;&amp; HDU大数水题)

    N! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Subm ...

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

  4. HDU 4771

    http://acm.hdu.edu.cn/showproblem.php?pid=4771 给一个地图,@是起点,给一些物品坐标,问取完所有物品的最小步数,不能取完输出-1 物品数最多只有四个,状态 ...

  5. HDU 自动刷题机 Auto AC (轻轻松松进入HDU首页)

    前言: 在写这篇文章之前,首先感谢给我思路以及帮助过我的学长们 以下4篇博客都是学长原创,其中有很多有用的,值得学习的东西,希望能够帮到大家! 1.手把手教你用C++ 写ACM自动刷题神器(冲入HDU ...

  6. hdu 5326(基础题) work

    http://acm.hdu.edu.cn/showproblem.php?pid=5326 一道水题,题目大意是在公司里,给出n个员工和目标人数m,然后下面的n-1行是表示员工a管理b,问在这些员工 ...

  7. 【转载】 HDU 动态规划46题【只提供思路与状态转移方程】

    1.Robberies 连接 :http://acm.hdu.edu.cn/showproblem.php?pid=2955      背包;第一次做的时候把概率当做背包(放大100000倍化为整数) ...

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

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

  9. hdu 5162(水题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5162 题解:看了半天以为测试用例写错了.这题玩文字游戏.它问的是当前第i名是原数组中的第几个. #i ...

随机推荐

  1. [转]linux之date命令MYSQL用户管理

    转自:http://www.cnblogs.com/hencehong/archive/2013/03/19/mysql_user.html 一.        用户登录 格式: mysql -h主机 ...

  2. [转]Visual F# Samples and Walkthroughs

    本文转自:http://msdn.microsoft.com/en-US/library/vstudio/ee241126.aspx This topic provides links to samp ...

  3. BOW-js浏览器对象模型

  4. C#中的事件机制

    这几天把事件学了一下,总算明白了一些.不多说了,直接代码. class Program { static void Main(string[] args) { CatAndMouse h = new ...

  5. 老潘 - ListView分析 - 学以致用篇(一)

    ListView分析学以致用篇(1) 在我们查看别人的博客的时候,一个人是一个风格的.先说下我的风格,我喜欢思想类比,然后介绍知识,不太喜欢填鸭式的灌输.如果只是想单纯的从我的博客中直接看到代码,我个 ...

  6. UI动画效果

    UI界面的动画效果总结 方式1:头尾式 //开始动画 [UIView beginAnimations:nil context:nil]; //设置动画时间 [UIView setAnimationDu ...

  7. Android(java)学习笔记199:JNI之JNI概念

    1. JNI是什么? java native interface (java本机接口) 比如方法声明: public final native Class<?>  getClass(): ...

  8. java将字段映射成另一个字段,关于 接口传参 字段不对应转换

    在接口开发中我们经常会遇到一个问题,打个比方,我们的实体类A中有两个字段user和pwd但是接口中需要username和password这怎么办呢,我想到了两种方法:1.新创建一个实体类B或者new一 ...

  9. Windos无法验证文件数组签名

    参考链接:https://jingyan.baidu.com/article/09ea3ede6982c4c0aede39e6.html Windows无法验证文件数字签名而无法启动,照以下去做,可以 ...

  10. zend studio汉化离线语言包安装方法