NYOJ 635 Oh, my goddess (简单BFS)
2015-08-16 20:24
507 查看
Oh, my goddess
时间限制:3000 ms | 内存限制:65535 KB难度:3
描述
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.
输入The input consists of blocks of lines. There is a blank line between two blocks.
The first line of each block contains two positive integers M <= 50 and N <= 50separated by one space. In each of the next M lines there is a string of length N contentsO and #.
O represents empty squares. # means a wall.
At last, the location of Miss Ice, ( x, y ). 1 <= x <= M, 1 <= y <= N.
(Shining Knight always starts at coordinate ( 1, 1 ). Both Shining and Ice's locationguarantee not to be a wall.)输出The least amount of time Shining Knight takes to save hisgoddess in one line.样例输入
3 5 O#### ##### #O#O# 3 4
样例输出
14
一个0和O的闹剧,把O看成了0,我也是醉了(衰
ac代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
int n,m;
int ex,ey;
char map[55][55];
int hp[55][55];
int v[55][55];
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
struct s
{
int x;
int y;
int step;
friend bool operator<(s n1,s n2)
{
return n2.step<n1.step;
}
};
int check(int xx,int yy)
{
if(xx<=0||yy<0||xx>n||yy>=m)
return 1;
if(v[xx][yy])
return 1;
return 0;
}
int bfs()
{
int i;
priority_queue<s>q;
s a,b;
a.x=1;
a.y=0;
a.step=0;
v[a.x][a.y]=1;
q.push(a);
while(!q.empty())
{
a=q.top();
q.pop();
if(a.x==ex&&a.y==ey-1)
{
return a.step;
}
for(i=0;i<4;i++)
{
int nx=a.x+dir[i][0];
int ny=a.y+dir[i][1];
if(check(nx,ny)==0)
{
b.step=a.step+hp[nx][ny]+1;
b.x=nx;b.y=ny;
v[b.x][b.y]=1;
q.push(b);
}
}
}
return 0;
}
int main()
{
int i,j;
int sum;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(hp,0,sizeof(hp));
memset(v,0,sizeof(v));
for(i=1;i<=n;i++)
{
scanf("%s",map[i]);
for(j=0;j<m;j++)
{
if(map[i][j]=='O')
hp[i][j]=0;
else if(map[i][j]=='#')
hp[i][j]=3;
}
}
scanf("%d%d",&ex,&ey);
sum=bfs();
printf("%d\n",sum);
}
return 0;
}
相关文章推荐
- Django(中)
- POJ 2195 D - Going Home 费用流
- GoAgent下local文件下proxy.ini文件各项参数介绍
- 如何直接从 Google Play 下载 APK 文件?
- 如何用Go语言打造一个高性能MySQL Proxy
- [Machine Learning & Algorithm] 决策树与迭代决策树(GBDT)
- 如何用Go语言打造一个高性能MySQL Proxy
- Google APAC 2016 University Graduates Test Practice Round APAC test
- django-rest-framework指南(3):Class Based Views(类视图)
- Google APAC 2016 University Graduates Test(google校招笔试)
- Google笔试(2015年8月)
- Codeforces Round #313 (Div. 2)-Gerald's Hexagon 找规律-几何规律-
- HDU_1847 Good Luck in CET-4 Everybody!(SG函数)
- hdu 3699 Aragorn's Story(树链剖分)
- GoldenGate 使用reperror进行错误处理
- Leetcode问题-1(Two Sum)
- win10 google浏览器设置
- poj 3107 Godfather 求树的所有重心 树形DP
- golang byte转string 字节数组转字符串的问题
- 欧几里得 & 拓展欧几里得算法 讲解 (Euclid & Extend- Euclid Algorithm)