nyoj 635 Oh, my goddess
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | #include<iostream> #include<algorithm> #include<queue> #include<cstdio> #include<cstring> using namespace std; char f[52][52]; bool vis[52][52]; int Ex,Ey,N,M; const int dir[4][2]={-1,0,1,0,0,1,0,-1}; struct Data { int time ; int x,y; }; struct cmp { bool operator() (Data a,Data b) { return a. time >b. time ; } }; priority_queue<Data,vector<Data>,cmp>Q; void BFS() { while (!Q.empty())Q.pop(); memset (vis,0, sizeof (vis)); Data t,elem; t. time =0;t.x=t.y=1;Q.push(t); int nx,ny;vis[1][1]= true ; while (!Q.empty()) { t=Q.top();Q.pop(); if (t.x==Ex&&t.y==Ey){ printf ( "%d\n" ,t. time ); break ;} for ( int i=0;i<4;i++) { nx=t.x+dir[i][0]; ny=t.y+dir[i][1]; if (nx>0&&nx<=N&&ny>0&&ny<=M&&!vis[nx][ny]) { elem. time =t. time +(f[nx][ny]== '#' ?4:1); elem.x=nx;elem.y=ny; Q.push(elem); vis[nx][ny]= true ; } } } } int main() { while (~ scanf ( "%d%d" ,&N,&M)) { for ( int i=1;i<=N;i++) scanf ( "%s" ,f[i]+1); scanf ( "%d%d" ,&Ex,&Ey); BFS(); } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器