博客
关于我
Oh, my goddess(bfs)
阅读量:625 次
发布时间:2019-03-13

本文共 2628 字,大约阅读时间需要 8 分钟。

 

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 5O##########O#O#3 4
样例输出
14
题解:坑,水,搞定#花费3,过去1,总共应该是4;
代码:
1 #include
2 #include
3 #include
4 using namespace std; 5 struct Node{ 6 int x,y,step; 7 friend bool operator < (Node a,Node b){ 8 return a.step>b.step; 9 }10 };11 char map[55][55];12 int vis[55][55];13 int disx[4]={0,0,1,-1};14 int disy[4]={1,-1,0,0};15 int M,N,ex,ey;16 void bfs(int sx,int sy){17 memset(vis,0,sizeof(vis));18 vis[sx][sy]=1;19 priority_queue
dl;20 Node a,b;21 a.x=sx;a.y=sy;a.step=0;22 dl.push(a);23 while(!dl.empty()){24 a=dl.top();25 dl.pop();26 for(int i=0;i<4;i++){27 b.x=a.x+disx[i];b.y=a.y+disy[i];28 if(b.x<1||b.y<1||b.x>M||b.y>N||vis[b.x][b.y])continue;29 if(map[b.x][b.y]=='O'){30 b.step=a.step+1;31 }32 if(map[b.x][b.y]=='#')b.step=a.step+4;33 if(b.x==ex&&b.y==ey){34 printf("%d\n",b.step);35 return;36 }37 vis[b.x][b.y]=1;38 dl.push(b);39 }40 }41 }42 int main(){43 while(~scanf("%d%d",&M,&N)){44 for(int i=1;i<=M;i++)scanf("%s",map[i]+1);45 scanf("%d%d",&ex,&ey);46 bfs(1,1);47 }48 return 0;49 }

 

转载地址:http://izeaz.baihongyu.com/

你可能感兴趣的文章
mysql 快照读 幻读_innodb当前读 与 快照读 and rr级别是否真正避免了幻读
查看>>
MySQL 快速创建千万级测试数据
查看>>
mysql 快速自增假数据, 新增假数据,mysql自增假数据
查看>>
MySql 手动执行主从备份
查看>>
Mysql 批量修改四种方式效率对比(一)
查看>>
mysql 批量插入
查看>>
Mysql 报错 Field 'id' doesn't have a default value
查看>>
MySQL 报错:Duplicate entry 'xxx' for key 'UNIQ_XXXX'
查看>>
Mysql 拼接多个字段作为查询条件查询方法
查看>>
mysql 排序id_mysql如何按特定id排序
查看>>
Mysql 提示:Communication link failure
查看>>
mysql 插入是否成功_PDO mysql:如何知道插入是否成功
查看>>
Mysql 数据库InnoDB存储引擎中主要组件的刷新清理条件:脏页、RedoLog重做日志、Insert Buffer或ChangeBuffer、Undo Log
查看>>
mysql 数据库中 count(*),count(1),count(列名)区别和效率问题
查看>>
mysql 数据库备份及ibdata1的瘦身
查看>>
MySQL 数据库备份种类以及常用备份工具汇总
查看>>
mysql 数据库存储引擎怎么选择?快来看看性能测试吧
查看>>
MySQL 数据库操作指南:学习如何使用 Python 进行增删改查操作
查看>>
MySQL 数据库的高可用性分析
查看>>
MySQL 数据库设计总结
查看>>