您的位置:首页 > 其它

广搜——路径寻找

2015-09-27 07:29 190 查看
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
8
......s...
..........
#ooooooo.o
#.........
#.........
#.........
#.....m...
#.........

输出

10


思路:
普通广搜+优先队列
代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>

using namespace std;
const int maxn = 2005;
int j[maxn][maxn],room[maxn][maxn];
long long int total = 0,m,n;
struct pos{
int x;
int y;
};
pos q[4000000];
pos dir[4];

int bfs(int y,int x){
int h = 0,t = 0;
q[0].y = y;
q[0].x = x;
int tx,ty;
while(h <= t){

int r3 = 0;
for(r3 = 0;r3 <  4;r3++){
x = q[h].x;
y = q[h].y;
tx = dir[r3].x;
ty = dir[r3].y;
if(y + ty >= 0 && y + ty < n && x + tx >= 0 && x + tx < m && room[y + ty][x + tx] && j[y + ty][x + tx]){
t++;
q[t].y = y + ty;
q[t].x = x + tx;
j[y + ty][x + tx] = 0;
total--;
}
}
h++ ;
}
}

int main(){
cin>>n>>m;
dir[0].x = -1;dir[0].y = 0;
dir[1].x = +1;dir[1].y = 0;
dir[2].x = 0;dir[2].y = -1;
dir[3].x = 0;dir[3].y = +1;
char cmd;
int r1 = 0,r2 = 0,temp = -1;
for(r1 = 0;r1 < n;r1++){
for(r2 = 0;r2 < m;r2++){
cin>>cmd;
if(cmd == '.') {
j[r1][r2] = 1;
room[r1][r2] = 1;
total++;
}else if(cmd == '#'){
j[r1][r2] = 1;
room[r1][r2] = 0;
}
}
}
r1 = r2 =0;
for(r1 = 0;r1 < n;r1++){
for(r2 = 0;r2 < m;r2++){
if(room[r1][r2] && j[r1][r2]){
j[r1][r2] = 0;
bfs(r1,r2);
}

}
}
cout<<total;
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: