当前位置:网站首页>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
边栏推荐
- JMeter pressure test tool
- Function executes only the once function for the first time
- 零拷貝技術
- 低频量化之明日涨停预测
- Core concepts of microservice architecture
- 【报名】TF54:工程师成长地图与卓越研发组织打造
- Lenovo Saver y9000x 2020
- 专题测试05·二重积分【李艳芳全程班】
- Dolphin scheduler configuring dataX pit records
- Quartus Prime硬件实验开发(DE2-115板)实验二功能可调综合计时器设计
猜你喜欢
Search ideas and cases of large amount of Oracle redo log
What is the difference between blue-green publishing, rolling publishing and gray publishing?
Static interface method calls are not supported at language level '5'
Crontab timing task output generates a large number of mail and runs out of file system inode problem processing
Detailed explanation of redis (Basic + data type + transaction + persistence + publish and subscribe + master-slave replication + sentinel + cache penetration, breakdown and avalanche)
解决方案架构师的小锦囊 - 架构图的 5 种类型
ACFs file system creation, expansion, reduction and other configuration steps
Express中间件③(自定义中间件)
Special test 05 · double integral [Li Yanfang's whole class]
Kettle--控件解析
随机推荐
Reading notes: fedgnn: Federated graph neural network for privacy preserving recommendation
专题测试05·二重积分【李艳芳全程班】
Using Baidu Intelligent Cloud face detection interface to achieve photo quality detection
解决方案架构师的小锦囊 - 架构图的 5 种类型
Solution of discarding evaluate function in surprise Library
Core concepts of microservice architecture
AtCoder Beginner Contest 248C Dice Sum (生成函数)
Express ② (routage)
[code analysis (7)] communication efficient learning of deep networks from decentralized data
Leetcode brush question 897 incremental sequential search tree
Strange bug of cnpm
Express中间件③(自定义中间件)
力扣刷题 101. 对称二叉树
MySQL [SQL performance analysis + SQL tuning]
Quartus Prime硬件实验开发(DE2-115板)实验一CPU指令运算器设计
RAC environment alert log error drop transient type: systp2jw0acnaurdgu1sbqmbryw = = troubleshooting
Dolphin scheduler integrates Flink task pit records
Elmo (bilstm-crf + Elmo) (conll-2003 named entity recognition NER)
第一章 电商秒杀商品回顾
UNIX final exam summary -- for direct Department