帅气的HYC与冰淇淋
发布时间: 2015年11月1日 17:02 最后更新: 2015年11月3日 00:14 时间限制: 1000ms 内存限制: 128M
炎热的夏天,帅气的HYC要请整个实验室吃冰淇淋,于是……HYC顶着那炎炎的烈日,向Ice-cream home走去……
可是……停电了……
冰淇淋们躺在Ice-cream home的冰柜里,慢慢地……慢慢地……融化…………
你说,帅气的HYC能赶在冰淇淋融化完之前赶到Ice-cream home去吗?
给你一张坐标图,s为HYC的初始位置,m为Ice-cream home的位置,‘.’为路面,HYC在上面每单位时间可以移动一格;‘#’为草地,HYC在上面每两单位时间可以移动一格(;‘o’是障碍物,HYC不能在它上面行动。也就是说,HYC只能在路面或草地上行走,必须绕过障碍物,并到达冰淇淋店。但是…………不保证到达时,冰淇淋还未融化,所以……就请聪明的你……选择最佳的方案啦…………如果,HYC到的时候,冰淇淋已经融化完了,那他可是会发狂的。
依次输入冰淇淋的融化时间t(0<t<1000),坐标图的列x,行y(5<=x,y<=25){太长打起来好累……},和整张坐标图。
判断按照最优方案是否可以赶在冰淇淋融化之前到达冰淇淋店(注:当t=最优方案所用时间,则判断为未赶到),如赶到,输出所用时间;如未赶到,输出HYC的怒吼——“ouhouhouhou”(不包括引号)。
复制
11 10 8 ......s... .......... #ooooooo.o #......... #......... #......... #.....m... #.........
10
#include<stdio.h>
#include<queue>
#include<functional>
#include<vector>
#include<string.h>
using namespace std;
char map[100][100];
int go[4][2]={1,0,-1,0,0,1,0,-1};
int n,m,t;
struct node
{
int x;
int y;
int time;
bool operator<(const struct node &a) const
{
return time>a.time;
}
};
int check(node r)
{
if(r.x<0||r.x>=n||r.y<0||r.y>=m||map[r.x][r.y]=='o')
return 1;
return 0;
}
int bfs(int x,int y)
{
struct node re,pe;
priority_queue <struct node> q;
re.x=x;
re.y=y;
re.time=0;
map[re.x][re.y]='o';
q.push(re);
while(!q.empty())
{
pe=q.top();
q.pop();
if(pe.time ==t)
return 1;
for(int i=0;i<4;i++)
{
re=pe;
re.x+=go[i][0];
re.y+=go[i][1];
if(check(re))
continue;
if(map[re.x][re.y]=='m')
{
if(re.time+1==t)
continue;
printf("%d\n",re.time+1);
return 0;
}
re.time++;
if(map[re.x][re.y]=='#')
re.time++;
if(re.time>=t) continue;
map[re.x][re.y]='o';
q.push(re);
}
}
return 1;
}
int main()
{
int i,j,tx,ty;
while(~scanf("%d",&t)){
// getchar();
scanf("%d%d",&m,&n);
//printf("%d%d%d",t,m,n);
getchar();
for(i=0;i<n;i++)
scanf("%s",map[i]);
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{//printf("%c",map[i][j]);
if(map[i][j]=='s')
{
tx=i;
ty=j;
}
}
// printf("\n");
}
if(bfs(tx,ty))
printf("ouhouhouhou\n");
}
return 0;
}