当前位置:网站首页>1256:献给阿尔吉侬的花束
1256:献给阿尔吉侬的花束
2022-04-23 13:58:00 【暴揍键盘的程序猿】
1256:献给阿尔吉侬的花束
时间限制: 1000 ms 内存限制: 65536 KB
提交数: 9651 通过数: 4023
【题目描述】
阿尔吉侬是一只聪明又慵懒的小白鼠,它最擅长的就是走各种各样的迷宫。今天它要挑战一个非常大的迷宫,研究员们为了鼓励阿尔吉侬尽快到达终点,就在终点放了一块阿尔吉侬最喜欢的奶酪。现在研究员们想知道,如果阿尔吉侬足够聪明,它最少需要多少时间就能吃到奶酪。
迷宫用一个R×C的字符矩阵来表示。字符S表示阿尔吉侬所在的位置,字符E表示奶酪所在的位置,字符#表示墙壁,字符.表示可以通行。阿尔吉侬在1个单位时间内可以从当前的位置走到它上下左右四个方向上的任意一个位置,但不能走出地图边界。
【输入】
第一行是一个正整数T(1 ≤ T ≤ 10),表示一共有T组数据。
每一组数据的第一行包含了两个用空格分开的正整数R和C(2 ≤ R, C ≤ 200),表示地图是一个R×C的矩阵。
接下来的R行描述了地图的具体内容,每一行包含了C个字符。字符含义如题目描述中所述。保证有且仅有一个S和E。
【输出】
对于每一组数据,输出阿尔吉侬吃到奶酪的最少单位时间。若阿尔吉侬无法吃到奶酪,则输出“oop!”(只输出引号里面的内容,不输出引号)。每组数据的输出结果占一行。
【输入样例】
3
3 4
.S..
###.
..E.
3 4
.S..
.E..
....
3 4
.S..
####
..E.
【输出样例】
5
1
oop!
【AC代码】
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e3+10;
const int INF=0x3f3f3f3f;
inline int read()
{
char ch=getchar();
int n=0,m=1;
while(ch<'0'||ch>'9')
{
if(ch=='-')m=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')n=(n<<3)+(n<<1)+ch-48,ch=getchar();
return n*m;
}
void write(int n)
{
if(n>9)write(n/10);
putchar(n%10+'0');
}
int t,r,c,sx,sy,dir[4][2]={
{-1,0},{1,0},{0,-1},{0,1}};
char m[N][N];
bool vis[N][N];
struct point
{
int px,py,pt;
};
queue<point> q;
void bfs(int tx,int ty,int tt)
{
q.push((point){tx,ty,tt});
vis[tx][ty]=1;
while(!q.empty())
{
point p=q.front();
q.pop();
if(m[p.px][p.py]=='E')
{
cout<<p.pt<<"\n";
return;
}
for(int i=0;i<4;i++)
{
int nx=p.px+dir[i][0],ny=p.py+dir[i][1];
if(nx>0&&nx<=r&&ny>0&&ny<=c&&m[nx][ny]!='#'&&vis[nx][ny]==0)
{
q.push((point){nx,ny,p.pt+1});
vis[nx][ny]=1;
}
}
}
cout<<"oop!\n";
}
int main(int argc,char **argv)
{
t=read();
while(t--)
{
r=read(),c=read();
memset(vis,0,sizeof vis);
memset(m,'\0',sizeof m);
while(!q.empty())q.pop();
for(int i=1;i<=r;i++)
for(int j=1;j<=c;j++)
{
cin>>m[i][j];
if(m[i][j]=='S')sx=i,sy=j;
}
bfs(sx,sy,0);
}
return 0;
}
版权声明
本文为[暴揍键盘的程序猿]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Xiaorui_xuan/article/details/124353457
边栏推荐
- 商家案例 | 运动健康APP用户促活怎么做?做好这几点足矣
- Special window function rank, deny_ rank, row_ number
- 10g database cannot be started when using large memory host
- What is the difference between blue-green publishing, rolling publishing and gray publishing?
- 解决方案架构师的小锦囊 - 架构图的 5 种类型
- UML Unified Modeling Language
- Apache seatunnel 2.1.0 deployment and stepping on the pit
- AtomicIntegerArray源码分析与感悟
- JS force deduction brush question 103 Zigzag sequence traversal of binary tree
- freeCodeCamp----time_ Calculator exercise
猜你喜欢
JUC interview questions about synchronized, ThreadLocal, thread pool and atomic atomic classes
freeCodeCamp----time_ Calculator exercise
Special window function rank, deny_ rank, row_ number
Express②(路由)
Crontab timing task output generates a large number of mail and runs out of file system inode problem processing
Three characteristics of volatile keyword [data visibility, prohibition of instruction rearrangement and no guarantee of operation atomicity]
[machine learning] Note 4. KNN + cross validation
Express middleware ③ (custom Middleware)
Apache seatunnel 2.1.0 deployment and stepping on the pit
Basic SQL query and learning
随机推荐
[code analysis (3)] communication efficient learning of deep networks from decentralized data
Decentralized Collaborative Learning Framework for Next POI Recommendation
Tensorflow Download
Oracle RAC database instance startup exception analysis IPC send timeout
Quartus Prime硬件实验开发(DE2-115板)实验二功能可调综合计时器设计
[code analysis (5)] communication efficient learning of deep networks from decentralized data
Express ② (routing)
Solution of discarding evaluate function in surprise Library
2022年江西最新建筑八大员(质量员)模拟考试题库及答案解析
项目中遇到的问题(五)操作Excel接口Poi的理解
Static interface method calls are not supported at language level '5'
淘宝发布宝贝提示“您的消保保证金额度不足,已启动到期保障”
elmo(BiLSTM-CRF+elmo)(Conll-2003 命名实体识别NER)
MySQL [SQL performance analysis + SQL tuning]
Oracle and MySQL batch query all table names and table name comments under users
Ora-600 encountered in Oracle environment [qkacon: fjswrwo]
Dolphin scheduler configuring dataX pit records
【项目】小帽外卖(八)
Detailed explanation of redis (Basic + data type + transaction + persistence + publish and subscribe + master-slave replication + sentinel + cache penetration, breakdown and avalanche)
Haruki Murakami -- Excerpt from "what do I talk about when I talk about running"