题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4771

题目意思:'@'  表示的是起点,'#' 表示的是障碍物不能通过,'.'  表示的是路能通过的;

目的:让你从 '@' 点出发,然后每个点只能走一次,求出最小的距离;

解题思路:先用 bfs 求解出任意两点之间的距离,用 ans[i][j],表示点 i 到点  j 的距离;

然后用 dfs 递归求出从起点经过所有点的距离中,比较出最小的;

AC代码:

  1. #include<iostream>
  2. #include<algorithm>
  3. #include<cstdio>
  4. #include<cstring>
  5. #include<queue>
  6. #include<string>
  7. #include<cmath>
  8. using namespace std;
  9. int dir[][]={,,,-,,,-,};
  10. int m,n;
  11. int sx,sy,T;
  12. int ans[][];//点之间的距离
  13. char Map[][];
  14. int vis[][];//标记路径
  15. struct node
  16. {
  17. int x,y,step;
  18. }a[],now,eed;
  19. int bfs(int T1,int ee,int vv)
  20. {
  21. int TT=;
  22. vis[a[T1].x][a[T1].y]=vv;//每次标记的都发生了变化,这样vis数组不用每次都清零
  23. queue< node >p;
  24. now.x = a[T1].x;
  25. now.y = a[T1].y;
  26. now.step = ;
  27. p.push(now);
  28. while(!p.empty())
  29. {
  30. now=p.front();
  31. p.pop();
  32. for(int i=T1+; i<=T; i++)
  33. {
  34. if(now.x == a[i].x && now.y == a[i].y)
  35. {
  36. ans[T1][i]=ans[i][T1] = now.step;
  37. TT++;
  38. }
  39. }
  40. for(int i=; i<; i++)
  41. {
  42. eed.x = now.x + dir[i][]; eed.y = now.y + dir[i][];
  43. if(eed.x>= && eed.x<=m && eed.y>= && eed.y<=n && vis[eed.x][eed.y]!=vv && Map[eed.x][eed.y]!='#')
  44. {
  45. eed.step = now.step+;
  46. vis[eed.x][eed.y] = vv;
  47. p.push(eed);
  48. }
  49. }
  50. if(TT == ee) //如果该访问的点都访问了,直接返回;
  51. return ;
  52. }
  53. return - ;//如果其中有点不能访问到,直接返回-1,输出 -1 ;
  54. }
  55.  
  56. int net[],ans1;
  57. void dfs(int x,int step,int sum)
  58. {
  59. if(step==T)
  60. {
  61. if(ans1>sum) ans1=sum;
  62. return;
  63. }
  64. for(int i=;i<=T;i++)
  65. if(!net[i])
  66. {
  67. net[i]=;
  68. dfs(i,step+,sum+ans[x][i]);
  69. net[i]=;
  70. }
  71. }
  72. int main()
  73. {
  74. int x,y;
  75. // freopen("in1.txt","r",stdin);
  76. // freopen("out1.txt","w",stdout);
  77. while(cin>>m>>n && m+n)
  78. {
  79. ans1=;
  80. memset(ans,,sizeof(ans));
  81. memset(net,,sizeof(net));
  82. memset(vis,,sizeof(vis));
  83. for(int i=; i<=m; i++)
  84. for(int j=; j<=n; j++)
  85. {
  86. scanf(" %c",&Map[i][j]);
  87. if(Map[i][j] == '@')
  88. {
  89. sx= i; sy = j;
  90. }
  91. }
  92. scanf("%d",&T);
  93. a[].x=sx; a[].y=sy;
  94. for(int i=; i<=T; i++)
  95. {
  96. scanf("%d %d",&x,&y);
  97. a[i].x = x;
  98. a[i].y = y;
  99. }
  100. int flag = ;
  101. for(int i = ; i<T; i++)
  102. {
  103.  
  104. flag = bfs(i,T-i,i+);
  105. if(flag == -)
  106. break;
  107. }
  108. if(flag == -)
  109. printf("-1\n");
  110. else
  111. {
  112. net[]=;
  113. dfs(,,);
  114. printf("%d\n",ans1);
  115. }
  116. }
  117. return ;
  118. }

hdu 4771 Stealing Harry Potter's Precious (2013亚洲区杭州现场赛)(搜索 bfs + dfs) 带权值的路径的更多相关文章

  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 4771 Stealing Harry Potter's Precious (BFS+状压)

    题意: n*m的迷宫,有一些格能走("."),有一些格不能走("#").起始点为"@". 有K个物体.(K<=4),每个物体都是放在& ...

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

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

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

    题目:给出一个二维图,以及一个起点,m个中间点,求出从起点出发,到达每一个中间的最小步数. 思路:由于图的大小最大是100*100,所以要使用bfs求出当中每两个点之间的最小距离.然后依据这些步数,建 ...

  8. 2013 ACMICPC 杭州现场赛 I题

    #include<iostream> #include<cstring> #include<algorithm> #include<cmath> #in ...

  9. 2013 Asia acm Hangzhou Regional Contest 杭州现场赛

     B Stealing Harry Potter's Precious 题目大意:给定一个n*m的地图,某些点可以走,某些点可以走某些点不可以走,给定一个起点,又给出了k个点k<=4,要求从起点 ...

随机推荐

  1. SparkSQL之更改表结构

    本文篇幅较短,内容源于自己在使用SparkSQL时碰到的一个小问题,因为在之后的数据处理过程中多次使用,所以为了加深印象,在此单独成文,以便回顾. 场景 在使用SparkSQL进行数据处理时,碰到这样 ...

  2. 安装PIL的坑

    今天在centos中使用pip安装PIL死活不成功,报错: Could not find a version that satisfies the requirement PIL (from vers ...

  3. 【转】Go maps in action

    原文: https://blog.golang.org/go-maps-in-action ------------------------------------------------------ ...

  4. android apktool 基本的安装与使用

    apktool: 1. 安装 http://ibotpeaches.github.io/Apktool/install/ 2. 基本使用 http://ibotpeaches.github.io/Ap ...

  5. 区域设置 ID (LCID) 表, 及获取方法

    区域设置 ID (LCID) 表, 及获取方法 中国的区域设置 ID 是 2052, 如果经常打开微软软件的安装目录应该经常见到.获取很简单, 有现成的 API 函数: GetThreadLocale ...

  6. 使用 ssh 从 Gerrit 获取 patch 信息

    使用命令行(ssh)对Gerrit进行查询, 官方地址:https://review.openstack.org/Documentation/cmd-query.html 程序例子 import os ...

  7. MPEG2 PS和TS流格式

    http://blog.csdn.net/alangdangjia/article/details/9495193 应该说真正了解TS,还是看了朋友推荐的<数字电视业务信息及其编码>一书之 ...

  8. websphere中的会话超时设置 和 web应用中web.xml中session-timeout关系

    Tomcat默认的会话的超时时间设置 设置Tomcat session有效期的三种方式有: 1.在tomcat/conf/web.xml中修改session-timeout的值,该设置是TOMCAT全 ...

  9. 【转】Spring的中IoC及AOP

    1. Spring介绍 Spring是轻量级的J2EE应用程序框架.Spring的核心是个轻量级容器(container),实现了IoC(Inversion of Control)模式的容器,Spri ...

  10. .htaccess 文件中详细介绍

    #如果存在rewrite_module 模块则执行里面的代码 <IfModule rewrite_module> #开启重写机制 RewriteEngine On #告诉apache这里不 ...