hdu 4771 Stealing Harry Potter's Precious (2013亚洲区杭州现场赛)(搜索 bfs + dfs) 带权值的路径
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4771
题目意思:'@' 表示的是起点,'#' 表示的是障碍物不能通过,'.' 表示的是路能通过的;
目的:让你从 '@' 点出发,然后每个点只能走一次,求出最小的距离;
解题思路:先用 bfs 求解出任意两点之间的距离,用 ans[i][j],表示点 i 到点 j 的距离;
然后用 dfs 递归求出从起点经过所有点的距离中,比较出最小的;
AC代码:
- #include<iostream>
- #include<algorithm>
- #include<cstdio>
- #include<cstring>
- #include<queue>
- #include<string>
- #include<cmath>
- using namespace std;
- int dir[][]={,,,-,,,-,};
- int m,n;
- int sx,sy,T;
- int ans[][];//点之间的距离
- char Map[][];
- int vis[][];//标记路径
- struct node
- {
- int x,y,step;
- }a[],now,eed;
- int bfs(int T1,int ee,int vv)
- {
- int TT=;
- vis[a[T1].x][a[T1].y]=vv;//每次标记的都发生了变化,这样vis数组不用每次都清零
- queue< node >p;
- now.x = a[T1].x;
- now.y = a[T1].y;
- now.step = ;
- p.push(now);
- while(!p.empty())
- {
- now=p.front();
- p.pop();
- for(int i=T1+; i<=T; i++)
- {
- if(now.x == a[i].x && now.y == a[i].y)
- {
- ans[T1][i]=ans[i][T1] = now.step;
- TT++;
- }
- }
- for(int i=; i<; i++)
- {
- eed.x = now.x + dir[i][]; eed.y = now.y + dir[i][];
- if(eed.x>= && eed.x<=m && eed.y>= && eed.y<=n && vis[eed.x][eed.y]!=vv && Map[eed.x][eed.y]!='#')
- {
- eed.step = now.step+;
- vis[eed.x][eed.y] = vv;
- p.push(eed);
- }
- }
- if(TT == ee) //如果该访问的点都访问了,直接返回;
- return ;
- }
- return - ;//如果其中有点不能访问到,直接返回-1,输出 -1 ;
- }
- int net[],ans1;
- void dfs(int x,int step,int sum)
- {
- if(step==T)
- {
- if(ans1>sum) ans1=sum;
- return;
- }
- for(int i=;i<=T;i++)
- if(!net[i])
- {
- net[i]=;
- dfs(i,step+,sum+ans[x][i]);
- net[i]=;
- }
- }
- int main()
- {
- int x,y;
- // freopen("in1.txt","r",stdin);
- // freopen("out1.txt","w",stdout);
- while(cin>>m>>n && m+n)
- {
- ans1=;
- memset(ans,,sizeof(ans));
- memset(net,,sizeof(net));
- memset(vis,,sizeof(vis));
- for(int i=; i<=m; i++)
- for(int j=; j<=n; j++)
- {
- scanf(" %c",&Map[i][j]);
- if(Map[i][j] == '@')
- {
- sx= i; sy = j;
- }
- }
- scanf("%d",&T);
- a[].x=sx; a[].y=sy;
- for(int i=; i<=T; i++)
- {
- scanf("%d %d",&x,&y);
- a[i].x = x;
- a[i].y = y;
- }
- int flag = ;
- for(int i = ; i<T; i++)
- {
- flag = bfs(i,T-i,i+);
- if(flag == -)
- break;
- }
- if(flag == -)
- printf("-1\n");
- else
- {
- net[]=;
- dfs(,,);
- printf("%d\n",ans1);
- }
- }
- return ;
- }
hdu 4771 Stealing Harry Potter's Precious (2013亚洲区杭州现场赛)(搜索 bfs + dfs) 带权值的路径的更多相关文章
- 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 4771 Stealing Harry Potter's Precious (BFS+状压)
题意: n*m的迷宫,有一些格能走("."),有一些格不能走("#").起始点为"@". 有K个物体.(K<=4),每个物体都是放在& ...
- hdu 4771 Stealing Harry Potter's Precious(bfs)
题目链接:hdu 4771 Stealing Harry Potter's Precious 题目大意:在一个N*M的银行里,贼的位置在'@',如今给出n个宝物的位置.如今贼要将全部的宝物拿到手.问最 ...
- hdu 4771 Stealing Harry Potter's Precious
题目:给出一个二维图,以及一个起点,m个中间点,求出从起点出发,到达每一个中间的最小步数. 思路:由于图的大小最大是100*100,所以要使用bfs求出当中每两个点之间的最小距离.然后依据这些步数,建 ...
- 2013 ACMICPC 杭州现场赛 I题
#include<iostream> #include<cstring> #include<algorithm> #include<cmath> #in ...
- 2013 Asia acm Hangzhou Regional Contest 杭州现场赛
B Stealing Harry Potter's Precious 题目大意:给定一个n*m的地图,某些点可以走,某些点可以走某些点不可以走,给定一个起点,又给出了k个点k<=4,要求从起点 ...
随机推荐
- SparkSQL之更改表结构
本文篇幅较短,内容源于自己在使用SparkSQL时碰到的一个小问题,因为在之后的数据处理过程中多次使用,所以为了加深印象,在此单独成文,以便回顾. 场景 在使用SparkSQL进行数据处理时,碰到这样 ...
- 安装PIL的坑
今天在centos中使用pip安装PIL死活不成功,报错: Could not find a version that satisfies the requirement PIL (from vers ...
- 【转】Go maps in action
原文: https://blog.golang.org/go-maps-in-action ------------------------------------------------------ ...
- android apktool 基本的安装与使用
apktool: 1. 安装 http://ibotpeaches.github.io/Apktool/install/ 2. 基本使用 http://ibotpeaches.github.io/Ap ...
- 区域设置 ID (LCID) 表, 及获取方法
区域设置 ID (LCID) 表, 及获取方法 中国的区域设置 ID 是 2052, 如果经常打开微软软件的安装目录应该经常见到.获取很简单, 有现成的 API 函数: GetThreadLocale ...
- 使用 ssh 从 Gerrit 获取 patch 信息
使用命令行(ssh)对Gerrit进行查询, 官方地址:https://review.openstack.org/Documentation/cmd-query.html 程序例子 import os ...
- MPEG2 PS和TS流格式
http://blog.csdn.net/alangdangjia/article/details/9495193 应该说真正了解TS,还是看了朋友推荐的<数字电视业务信息及其编码>一书之 ...
- websphere中的会话超时设置 和 web应用中web.xml中session-timeout关系
Tomcat默认的会话的超时时间设置 设置Tomcat session有效期的三种方式有: 1.在tomcat/conf/web.xml中修改session-timeout的值,该设置是TOMCAT全 ...
- 【转】Spring的中IoC及AOP
1. Spring介绍 Spring是轻量级的J2EE应用程序框架.Spring的核心是个轻量级容器(container),实现了IoC(Inversion of Control)模式的容器,Spri ...
- .htaccess 文件中详细介绍
#如果存在rewrite_module 模块则执行里面的代码 <IfModule rewrite_module> #开启重写机制 RewriteEngine On #告诉apache这里不 ...