Shining Knight is the embodiment of justice and he has a very sharp sword can even cleavewall. Many bad guys are dead on his sword.
One day, two evil sorcerer cgangee and Jackchess decided to give him some colorto see. So they kidnapped Shining Knight's beloved girl--Miss Ice! They built a M x Nmaze with magic and shut her up in it.
Shining Knight arrives at the maze entrance immediately. He can reach any adjacent emptysquare of four directions -- up, down, left, and right in 1 second. Or cleave one adjacent wall in 3
seconds, namely,turn it into empty square. It's the time to save his goddess! Notice: ShiningKnight won't leave the maze before he find Miss Ice.
3 5 O#### ##### #O#O# 3 4
14
个人理解:
1.首次接触此类关于优先队列的问题,所用的bfs(),真的是不怎么熟悉,也不是太明白怎么回事,首先的感觉就是,代码明显变长了很多!
2.做这题时,时间有限,表示真的没怎么太明白题目中的所有代码内容,但我相信后续的题目中还会有涉及这类的题目,做多了,理解也会更深刻。
结果 | 时间 | 内存 | 语言 |
Accepted | 28 | 324 | c++ |
int judge()
{
if (temp.x<1||temp.x>n)
return 0;
if (temp.y<1||temp.y>m)
return 0;
if(vis[temp.x][temp.y]==1)
return 0;
if(temp.step>=ans)
return 0;
return 1;
}
void bfs()
{
a.x=x;
a.y=y
a.step=0;
priority_queue<node>q;
q.push(a);
memset(vis,0,sizeof(vis));
vis [x][y]=1;
while (!q.empty())
{
a=q.top();
q.pop();
for (int i=0;i<4;i++)
{
temp.x=a.x+dx[i]
temp.y=a.y+dy[i]
if (mp[temp.x][temp.y]=='#')
temp.step=a.step+4;
else
temp.step=a.step+1;
if (judge())
{
if (temp.x==e&&temp.y==ey)
{
ans=temp.step;
return;
}
q.push(temp);
V[temp.x][temp.y]=1;
}
}
}
}
int main()
{
while (~scanf("%d%d",&n,&m));
{
ans =INF
for (int i=1;i<=n;i++)
{
scanf ("%s",M[i]+1);
continue;
}
bfs()
printf("%d\n",ans);
}
return 0;
}