HDU 1242 Rescue (DFS+剪枝,一个起点多个终点)
来源:互联网 发布:新闻资讯网站php源码 编辑:程序博客网 时间:2024/05/21 09:30
Discription
题意:天使被困在监狱,他的朋友们想见他,监狱的地形复杂,包括路(用点标示),墙(用#标示),天使的位置(用a标示),他的朋友(用r标示),监狱里还有守卫(用x标示),他的朋友只能向左右上下四个方向走,走以不花一单位时间,若碰上守卫,消灭守卫需要额外花费一单位时间。问最少多长时间天使能见到他的朋友。
Sample Input
7 8
#.#####.#.a#..r.#..#x.....#..#.##...##...#..............
Sample Output
13
Solution
注意不只有一个朋友,所以采用一个起点搜索多个终点,注意中间剪枝,如果采用多个起点搜索一个终点思路会T。
#include <iostream>#include <cstdio>#include <algorithm>#include <vector>#include <cstring>#include <ctime>#include <utility>using namespace std;#define ONLINE_JUDGEchar m[400][400];int visit[400][400];int COUNT, N, M;int xx[4] = {0, 0, -1, 1};int yy[4] = {-1, 1, 0, 0};void init(){ memset(m, 0, sizeof(m)); memset(visit, 0, sizeof(visit));}void dfs(int x, int y, int count){ if (m[x][y] == 'r') { COUNT = min(COUNT, count); return; } if (count >= COUNT) return; for (int i = 0; i < 4; i++) { int nx = x + xx[i]; int ny = y + yy[i]; if (!visit[nx][ny] && nx >= 1 && nx <= N && ny >= 1 && ny <= M && m[nx][ny] != '#') { visit[nx][ny] = 1; if (m[x][y] == 'x') dfs(nx, ny, count + 2); else dfs(nx, ny, count + 1); visit[nx][ny] = 0; } }}int main(){#ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); long _begin_time = clock();#endif pair<int, int> start; while (~scanf("%d%d", &N, &M)) { init(); for (int i = 1; i <= N; i++) { for (int j = 1; j <= M; j++) { cin >> m[i][j]; if (m[i][j] == 'a') start = make_pair(i, j); } } COUNT = 0x3f3f3f3f; visit[start.first][start.second] = 1; dfs(start.first, start.second, 0); if (COUNT < 0x3f3f3f3f) printf("%d\n", COUNT); else printf("Poor ANGEL has to stay in the prison all his life.\n"); }#ifndef ONLINE_JUDGE long _end_time = clock(); printf("time = %.2f ms\n", double(_end_time - _begin_time) / CLOCKS_PER_SEC * 1000);#endif return 0;}
阅读全文
0 0
- HDU 1242 Rescue (DFS+剪枝,一个起点多个终点)
- HDU 1010 Tempter of the Bone (DFS+奇偶剪枝,一个起点一个终点)
- HDU 1242 Rescue 【DFS】
- HDU 1242 Rescue dfs
- HDU 1242 Rescue DFS
- HDU 1242 Rescue (DFS)
- HDOJ 1242 Rescue(DFS+剪枝)
- Rescue DFS()+剪枝
- hdu 2066 一个人的旅行(dijkstra 多个起点和终点)
- UVA208在一个图中,给定终点和起点,搜索路线,巧妙剪枝
- HDU 1242 Rescue (搜索 DFS)
- HDU 1242 Rescue - DFS 回溯
- hdu 1242Rescue(DFS)
- hdu-1242 Rescue DFS解法
- HDU 1242 Rescue(dfs)
- HDU 1242 Rescue(DFS入门)
- HDU 2612 Find a way 多起点 多终点 BFS
- HDU-2066 个人的旅行 【多起点多终点dijkstra】
- Linux Centos 可以平通IP和域名,但打开火狐却上不了网
- 算法系列——Power of Two
- 锚伪类-关于"<a>"链接状态
- 解决 -- java 调用webservice 服务端收到参数为null
- CSS Hack详解 支持IE 6-11、Chrome、FireFox、Safari、Opera
- HDU 1242 Rescue (DFS+剪枝,一个起点多个终点)
- VS2013 配置使用微软开源sdk: C++ REST SDK 及运行官方的 JSON例子
- ArcGIS Engine 添加SDE数据库
- bzoj 3597: [Scoi2014]方伯伯运椰子 spfa判负环+分数规划
- 搭建本地服务器的方法:
- SpringCloud学习笔记之Feign结合Hystrix实现服务容错保护
- 几种将存储为str的其他类型转换回去的方法
- C#191课的主要内容
- mybatis mapper把传递来的参数当作字段写入结果集中