当前位置:网站首页>[question 22] dungeons and Warriors (Beijing Institute of Technology / Beijing Institute of Technology / programming methods and practice / primary school)
[question 22] dungeons and Warriors (Beijing Institute of Technology / Beijing Institute of Technology / programming methods and practice / primary school)
2022-07-27 21:55:00 【Dream and wake up, happy and carefree】
Catalog

Description
The Dragon God felt bored and came to the dungeon , Here is a huge maze , There are some passable roads 、 Some impassable walls , There are also some monsters . Although the Dragon God can easily kill these monsters , But he thought it was too boring , He observed these monsters every k Seconds will disappear once ( for example k Equal to 3 when , Is the first 3 Commas Space 6 Commas Space 9 Commas Space Middle line ellipsis Second monsters disappear ), Every second, the Dragon God can choose to walk up, down, left and right ( You can't stay where you are ). The Dragon God wants to know that under the condition of avoiding all monsters , The shortest time required to reach the exit .
Input Enter an integer in the first line T Space Left parenthesis 1 Less than T Less than 10 Right bracket , Represents the number of use case groups . The first line of each set of use cases includes three integers n Commas Space m Space Left parenthesis 1 Less than n Commas Space m Less than 100 Right bracket and ,k Space Left parenthesis 1 Less than k Less than 50 Right bracket Respectively represent the number of lines in the dungeon maze 、 Number of columns 、 The disappearance interval of monsters . Next n Lines represent mazes ,. Means a passable road ,# Represents a wall ,* It means monster ,S The starting point ,E It's for export .
Output Output an integer , Indicates the shortest time for the Dragon God to get out of the dungeon maze , If the Dragon God can't get out of the maze, it will output -1.
Source BITACM2018 The first round of points ( 3、 ... and )- Problem J
Ideas
This question , Still bfs.
But it's different from the previous one .
You can go up. csdn search bfs Maze example , The most basic thing is to go through mazes : A picture , Some walls , A starting point , A destination , Shortest path . The simplicity of this problem is that the maze itself is already a graph , So you just need to bfs that will do
At present, I have seen three variants of this maze :
- 21 This kind of question , I didn't give you a picture , Need to use map<int,vector<int>> To construct
- 22 This kind of question , Given the ready-made map , But there is something strange , And it's not the shortest path , But the shortest time
- There is another kind. , I want you to output the shortest path
The general idea of this question is not difficult , Namely Go through the maze , Regardless of time , set csdn Just use the template .
The key is to understand why we need to open three-dimensional vis Array , Suppose my third dimension keeps updating , So no repetition will trigger the accessed tag , Until I passed a k cycle , It may trigger .
In fact, in theory, if you don't consider time, you don't need to open vis Of , Because under the action of monsters , We can go back ( At this time, it is no longer the maze problem of the shortest number of steps )
The reason to open vis, In order to reduce unnecessary turning back , But there is a new problem : How big is the third dimension ?
In order to solve the problem of unclear upper limit , I used % To limit the scope of , Save a space
% The rationality of : In time time And time time+k Accessing the same location is completely equivalent !
However time and time+k The weirdness of these two time points is exactly the same ,
in other words , In cycle , Instead, there is no strange influence , In other words, it is completely meaningless to take this step , So skip
Code
The original title is ,J
My version
#include<cstdio>
#include<cstring>
#include<queue>
#define SIZE 101
const int dir[2][4] = { {1,0,0,-1},
{0,1,-1,0} };
char maze[SIZE][SIZE];
bool vis[SIZE][SIZE][51];
struct point {
int x;
int y;
int time;
//point(int tx, int ty, int ttime)// Define a constructor for quick entry
//{
// x = tx;
// y = ty;
// time = ttime;
//}
};
int main(void)
{
freopen("input.txt", "r", stdin);
int T;
scanf("%d", &T);
while (T--)
{
// Operate on each map
// initialization
point start, now, next, end;
int n, m, k, ch;
int ans = -1;
std::queue <point> q;
memset(vis, false, sizeof(vis));
// Read in the picture
scanf("%d %d %d\n", &n, &m, &k);// Pay attention to absorbing line breaks
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
ch = getchar();
maze[i][j] = ch;
if (ch == 'S')// Record the starting point
{
start.x = i;
start.y = j;
start.time = 0;
}
if (ch == 'E')
{
end.x = i;
end.y = j;
}
}
getchar();// Every line has a newline
}
//bfs
q.push(start);
vis[start.x][start.y][start.time % k] = true;
while (!q.empty())
{
// Take out
now = q.front();
q.pop();
if (now.x == end.x && now.y == end.y)
{
ans = now.time;
break;
}
// Put it in the next step
for (int i = 0; i < 4; i++)
{
next.x = now.x + dir[0][i];
next.y = now.y + dir[1][i];
next.time = now.time + 1;
// Pay attention to the boundaries , wall , Repeat the visit , blame
if (next.x<1 || next.x>n || next.y > m || next.y < 1
|| maze[next.x][next.y] == '#'
|| vis[next.x][next.y][next.time % k]
|| (maze[next.x][next.y] == '*' && next.time % k != 0))// This piece is written for readability !=0, In fact, it's OK not to write
{
continue;
}
q.push(next);
vis[next.x][next.y][next.time % k] = true;
}
}
printf("%d\n", ans);
}
return 0;
}边栏推荐
- Analysis of STL source code
- Technology Management - we must focus on the big and let go of the small
- Software test interview question: does software acceptance test include formal acceptance test, alpha test and beta test?
- Under the epidemic, the mobile phone supply chain and offline channels are blocked! Sales plummeted and inventory was serious!
- Read Plato farm's eplato and the reason for its high premium
- Software test interview question: please say who is the best person to complete these tests, and what is the test?
- Small change project (two versions) with detailed ideas
- 哈希表的查找与插入及删除
- Cocoapods reload
- Huawei establishes global ecological development department: fully promote HMS global ecological construction
猜你喜欢

In addition to "adding machines", in fact, your micro service can be optimized like this

LinkedList underlying source code

Exception -exception

哈希表的查找与插入及删除

MySQL execution process and order

Plato Farm在Elephant Swap上铸造的ePLATO是什么?为何具备高溢价?

零钱通项目(两个版本)含思路详解

一篇文章带你走进pycharm的世界----别再问我pycharm的安装和环境配置了!!!

腾讯云[HiFlow】| 自动化 -------HiFlow:还在复制粘贴?

Technical practice behind bloom model: how to refine 176billion parameter model?
随机推荐
Understanding of L1 regularization and L2 regularization [easy to understand]
Software testing interview question: what project documents need to be referred to in designing the system test plan?
Software testing interview question: when does the software testing project start? Why?
Custom recycleview delete & move animation
MySQL执行过程及执行顺序
V2.x synchronization is abnormal. There are a lot of posts that cannot be synchronized in the cloud, and the synchronization is blocked and slow
Analysis of STL source code
Why do server programs need to listen first
软件测试面试题:在windows下保存一个文本文件时会弹出保存对话框,如果为文件名建立测试用例,等价类应该怎样划分?
Log4j vulnerability is still widespread and continues to cause impact
@Detailed introduction of requestparam annotation
How long will it take to learn the four redis cluster solutions? I'll finish it for you in one breath
@RequestParam注解的详细介绍
除了「加机器」,其实你的微服务还能这样优化
一篇文章带你走进pycharm的世界----别再问我pycharm的安装和环境配置了!!!
Import word document pictures blocking and non blocking IO operations
2019q4 memory manufacturers' revenue ranking: Samsung fell 5%, only SK Hynix and micron maintained growth
2019Q4内存厂商营收排名:三星下滑5%,仅SK海力士、美光维持增长
Small change project (two versions) with detailed ideas
学完4种 Redis 集群方案要多久?我一口气给你说完