首页 > 其他 > 详细

广搜——路径寻找

时间:2015-09-27 07:41:28      阅读:459      评论:0      收藏:0      [点我收藏+]

Tyvj 1117 拯救ice-cream

背景

天好热……Tina顶着那炎炎的烈日,向Ice-cream home走去……
可是……停电了……
冰淇淋们躺在Ice-cream home的冰柜里,慢慢地……慢慢地……融化…………
你说,她能赶在冰淇淋融化完之前赶到Ice-cream home去吗?

描述

给你一张坐标图,s为Tina的初始位置,m为Ice-cream home的位置,‘.’为路面,Tina在上面,每单位时间可以移动一格;‘#’为草地,Tina在上面,每两单位时间可以移动一格(建议不要模仿—毕竟Tina还小);‘o’是障碍物,Tina不能在它上面行动。也就是说,Tina只能在路面或草地上行走,必须绕过障碍物,并到达冰淇淋店。但是…………不保证到达时,冰淇淋还未融化,所以……就请聪明的你……选择最佳的方案啦…………如果,Tina到的时候,冰淇淋已经融化完了,那她可是会哭的。

输入格式

依次输入冰淇淋的融化时间t(0<t<1000),坐标图的长x,宽y(5<=x,y<=25){太长打起来好累……},和整张坐标图。

输出格式

判断按照最优方案是否可以赶在冰淇淋融化之前到达冰淇淋店(注:当T=最优方案所用时间,则判断为未赶到),如赶到,输出所用时间;如未赶到,输出Tina的哭声——“55555”(不包括引号)。

测试样例1

输入

11 
10 

......s... 
.......... 
#ooooooo.o 
#......... 
#......... 
#......... 
#.....m... 
#.........

输出

10
思路:
普通广搜+优先队列
代码:
技术分享
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<string>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<vector>
 7 #include<queue>
 8 using namespace std;
 9 struct node{
10     int x;
11     int y;
12     int dist;
13     friend bool operator < (node a,node b){
14         return a.dist > b.dist;
15     }
16 };
17 priority_queue<node> q;
18 int t,x,y,startx,starty,endx,endy,map[50][50],jud[50][50];
19 int dx[4] = {-1,0,1,0};
20 int dy[4] = {0,-1,0,1};
21 void input(){
22     cin>>t>>x>>y;
23     char cmd;
24     for(int i = 1;i <= y;i++){
25         for(int j = 1;j <= x;j++){
26             cin>>cmd;
27             if(cmd == .) map[i][j] = 1;
28             if(cmd == #) map[i][j] = 2;
29             if(cmd == o) map[i][j] = 3;
30             if(cmd == s){
31                 map[i][j] = 1;
32                 startx = j;
33                 starty = i;
34             }
35             if(cmd == m){
36                 map[i][j] = 1;
37                 endx = j;
38                 endy = i;
39             }
40         }
41     }
42     node tmp;
43     tmp.x = startx;
44     tmp.y = starty;
45     tmp.dist = 0;
46     q.push(tmp);
47     for(int i = 1;i <= 40;i++){
48         for(int j = 1;j <= 40;j++){
49             jud[i][j] = 100000000;
50         }
51     }
52 }
53 bool bfs(){
54     node now,next;
55     int nx,ny;
56     while(!q.empty()){
57         now = q.top();
58         q.pop();
59         for(int i = 0;i < 4;i++){
60             nx = now.x + dx[i];
61             ny = now.y + dy[i];
62             if(nx < 1 || nx > x || ny < 1 || ny > y || map[ny][nx] == 3 ||jud[ny][nx] <= now.dist + map[ny][nx]) continue;
63             next.x = nx;
64             next.y = ny;
65             next.dist = now.dist + map[ny][nx];
66             if(nx == endx && ny == endy){
67                 if(next.dist >= t) return false;
68                 else{
69                     cout<<next.dist<<endl;
70                     return true;
71                 }
72             }
73             q.push(next);
74             jud[ny][nx] = next.dist;
75         }
76     }
77 }
78 int main(){
79     input();
80     if(!bfs()) cout<<55555<<endl;
81     return 0;
82 } 
View Code

 

 
 
 

广搜——路径寻找

原文:http://www.cnblogs.com/hyfer/p/4841776.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!