nyoj--635--Oh, my goddess(dfs)
2016-02-06 22:32
435 查看
Oh, my goddess
时间限制:3000 ms | 内存限制:65535 KB[align=center]难度:3[/align]
描述
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
很简单的一个bfs,但是写的真揪心
#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 vis[55][55],hp[55][55];
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
struct node
{
int x;
int y;
int step;
friend bool operator<(node n1,node n2)
{
return n2.step<n1.step;
}
}p,temp;
int check(int xx,int yy)
{
if(xx<=0||yy<0||xx>n||yy>=m)
return 1;
if(vis[xx][yy])
return 1;
return 0;
}
int bfs()
{
int i;
priority_queue<node>q;
p.x=1;
p.y=0;
p.step=0;
vis[p.x][p.y]=1;
q.push(p);
while(!q.empty())
{
p=q.top();
q.pop();
if(p.x==ex&&p.y==ey-1)
{
return p.step;
}
for(i=0;i<4;i++)
{
temp.x=p.x+dx[i];
temp.y=p.y+dy[i];
if(check(temp.x,temp.y)==0)
{
temp.step=p.step+hp[temp.x][temp.y]+1;
vis[temp.x][temp.y]=1;
q.push(temp);
}
}
}
return 0;
}
int main()
{
int i,j;
int sum;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(hp,0,sizeof(hp));
memset(vis,0,sizeof(vis));
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;
}
相关文章推荐
- nyoj--635--Oh, my goddess(dfs)
- 2.新建第一个Django项目
- Django 笔记 模板
- Django 笔记 模板
- django 自定义 密码加密方式 及自定义验证方式
- 1.Django开发环境搭建
- [Algorithm]Maze Prim算法与A*寻路算法(下)
- 【分享】GEARS of DRAGOON 1+2【日文硬盘版】[带全CG存档&攻略+SSG修改+打开存档补丁]
- django 投票小项目
- HD1847 Good Luck in CET-4 Everybody!(巴什博弈)
- OC+2-ARC-Category-block
- Django vs Flask vs Pyramid 选择你的WEB框架
- Django vs Flask vs Pyramid 选择你的WEB框架
- 混合使用django模板和jinja模板
- 混合使用django模板和jinja模板
- Django 笔记 动态URL
- Django 笔记 动态URL
- migrate遇到err mongoid[not found]
- migrate遇到err mongoid[not found]
- [Algorithm]Maze Prim算法与A*寻路算法(中)