hdu1242 Rescue(BFS +优先队列 or BFS ) (二)
s = q.front();
q.pop();
if(map[s.x][s.y] == 'a')
{
cout << s.step << endl;
prove =1;
}
//如取出的是在有警卫的格子中,即杀掉警卫再次进入队列
if(map[s.x][s.y] == 'x')
{
map[s.x][s.y] = '.';
s.step += 1;
q.push(s);
continue;
}
for(int i = 0; i < 4; i++)
{
e.x = s.x + dx[i]; e.y = s.y + dy[i];
if(!check(e.x, e.y) || visit[e.x][e.y]
|| map[e.x][e.y] == '#')
continue;
e.step = s.step + 1;
q.push(e);
visit[e.x][e.y] = 1;
}
}
}
int main()
{
while(cin >> N >> M)
{
for(int i = 0; i < N; i++)
for(int j = 0; j < M; j++)
{
cin >> map[i][j];
if(map[i][j] == 'r')
{ sx = i; sy = j; }
}
prove = 0;
BFS();
if(!prove)
cout << "Poor ANGEL has to stay in the prison all his life." << endl;
}
return 0;
}
| 评论 |
|
|