当前位置:网站首页>HDU 1026 search pruning problem within the labyrinth of Ignatius and the prince I
HDU 1026 search pruning problem within the labyrinth of Ignatius and the prince I
2022-07-06 19:48:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack .
This problem is a typical type of problem maze extensive Search for .
I see a lot of solutions on the Internet .
There's no problem-solving analysis , Don't point out the key points . The code is more like a copy . Some analysts also have big articles to analyze . Just don't hit the key all the time , What is broad and profound , Even search and find , Analyzing differences . Why search wide image quickly , What kind of storm calls search , All wrong. . The code is copied .
Through a long period of research , It finally got through .
Although this topic can be said to be extensive search . But the key is pruning . Why? ?
Because the maze can't search all the paths simply by searching widely , Even if the maze is a little bigger, you can't find out whether there is a path . Suppose there is no conditional pruning . Don't believe it , You strictly write a wide search and search the maze path to see . Of course, you wrote a wrong search . Naturally, I got the wrong answer .
The common mistake is to expand the maze one by one, thinking it is a wide search of the maze , wrong !
The real search is to map the maze . Then search widely .
In fact, the real key is pruning :
Pruning thinking is to think about when it should be extended to the next grid ? Whether the lattice is legal or not must be able to expand ? Of course not. , It is necessary to prune according to the meaning of the topic . The meaning of this question is to find the path with the smallest time . Therefore, it can be thought that it should be decided by time comparison whether it needs to be extended to the next grid .
That is, if the next lattice is possible to find a better solution, it will expand . Otherwise, do not expand .
After such a pruning . You can use the so-called wide search .
So why this topic . Or it can be said that the topic of this topic type cannot use deep search ?
Because the above pruning condition is that each layer is pruned , Suppose you use deep search , Then this pruning condition cannot be used .
Another way is to use priority queues . Give priority to expanding the lattice with the current minimum time . Then you don't have to search the next box repeatedly . This is also the use of the above pruning ideas .
Just understand the key pruning points above , Then this kind of problem can be solved at will .
As for the recording path of this question, it is the test of programming skills , Needless to say, what ideas . No, just say that the coding ability is not good .
Maybe some people who don't understand analysis also hit the code right , Maybe he's lucky . Maybe he is really a genius !
Anyway, the probability is very low , The greatest chance is that his code is copied .
#include <stdio.h>
#include <vector>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <limits.h>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
/*
Key understanding : Only when the next grid updates the minimum value does it need to be extended to this grid . Otherwise, there is no need to expand to this lattice . This is also equivalent to the pruning point of guangsearch . I can't understand this . There is no thorough understanding of this problem .*/namespace IgnatiusandthePrincessI1026{const int MAX_N = 101;char Maze[MAX_N][MAX_N];int dx[] = {-1, 0, 1, 0}, dy[] = {0, -1, 0, 1};struct Node{ int sec, x, y; Node *p;};Node mazeRec[MAX_N][MAX_N];int N, M;inline bool isLegal(int r, int c){ return 0<=r && 0<=c && r<N && c<M && Maze[r][c] != 'X';}inline int getSec(int r, int c){ if (Maze[r][c] == '.') return 1; return Maze[r][c] - '0' + 1;}void getPath(){ for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { mazeRec[i][j].sec = INT_MAX; mazeRec[i][j].x = i, mazeRec[i][j].y = j; mazeRec[i][j].p = NULL; } } queue<Node *> qu; Node *p = &mazeRec[N-1][M-1]; // Pay attention to the calculation error :p->sec = Maze[N-1][M-1] or = getSec(N-1, M-1) p->sec = getSec(N-1, M-1)-1;// The end may also be an enemy , The starting point stipulates invincible qu.push(p); while (!qu.empty()) { p = qu.front(); qu.pop(); for (int i = 0; i < 4; i++) { int tx = p->x + dx[i], ty = p->y + dy[i]; if (!isLegal(tx, ty)) continue; int sec = getSec(tx, ty); Node *tmp = &mazeRec[tx][ty]; if (p->sec+sec < tmp->sec) { tmp->sec = p->sec+sec; tmp->p = p; qu.push(tmp); } /* Key understanding : Only when the next grid updates the minimum value does it need to be extended to this grid . Otherwise, there is no need to expand to this lattice . This is also equivalent to the pruning point of guangsearch . I can't understand this , There is no thorough understanding of this problem .*//* All kinds of mistakes and lessons ! qu.push(tmp); tmp.vis = true; // Multiple errors else. Logic error else tmp->vis = true //Maze[tx][ty] = 'X'; tmp.sec = p.sec+sec; tmp.p = &mazeRec[p.x][p.y]; // error :tmp->p = p; // error :tmp->sec = min(tmp->sec, p->sec+sec);*/ } }}int main(){ while (~scanf("%d %d", &N, &M)) { while (getchar() != '\n'); for (int i = 0; i < N; i++) { gets(Maze[i]); } getPath(); Node *p = &mazeRec[0][0]; if (p->sec == INT_MAX) puts("God please help our poor hero."); else { printf("It takes %d seconds to reach the target position, let me show you the way.\n", p->sec); int s = 1; for (; p->p; p = p->p) { int x = p->p->x, y = p->p->y; printf("%ds:(%d,%d)->(%d,%d)\n", s++, p->x, p->y, x, y); if (Maze[x][y] == '.') continue; int fig = Maze[x][y] - '0';// Fewer mistakes -'0' for (int i = 0; i < fig; i++) { printf("%ds:FIGHT AT (%d,%d)\n", s++, x, y); } } } puts("FINISH"); } return 0;}Copyright notice : The author Jing Xin . Landscape space address :http://blog.csdn.net/kenden23/. Reprinted only with the consent of the author .
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/117152.html Link to the original text :https://javaforall.cn
边栏推荐
- js实现力扣71题简化路径
- 【pytorch】yolov5 训练自己的数据集
- 350. 两个数组的交集 II
- 深度剖析原理,看完这一篇就够了
- C # - realize serialization with Marshall class
- A5000 vGPU显示模式切换
- Tensorflow2.0 自定义训练的方式求解函数系数
- Druid database connection pool details
- The slave i/o thread stops because master and slave have equal MySQL serv
- C # use Marshall to manually create unmanaged memory in the heap and use
猜你喜欢

Low CPU load and high loadavg processing method

Mysql Information Schema 学习(一)--通用表

The "white paper on the panorama of the digital economy" has been released with great emphasis on the digitalization of insurance
深入分析,Android面试真题解析火爆全网

Transformer model (pytorch code explanation)

思维导图+源代码+笔记+项目,字节跳动+京东+360+网易面试题整理

Hudi vs Delta vs Iceberg

Leetcode 30. Concatenate substrings of all words
![[play with Linux] [docker] MySQL installation and configuration](/img/04/6253ef9fdf7d2242b42b4c7fb2c607.png)
[play with Linux] [docker] MySQL installation and configuration

It's enough to read this article to analyze the principle in depth
随机推荐
Carte de réflexion + code source + notes + projet, saut d'octets + jd + 360 + tri des questions d'entrevue Netease
A5000 vGPU显示模式切换
MySQL information schema learning (I) -- general table
Test Li hi
JDBC details
Vscode debug run fluent message: there is no extension for debugging yaml. Should we find yaml extensions in the market?
Tensorflow2.0 自定义训练的方式求解函数系数
深度剖析原理,看完这一篇就够了
PHP与EXCEL PHPExcel
学习探索-使用伪元素清除浮动元素造成的高度坍塌
POJ 3207 Ikki&#39;s Story IV – Panda&#39;s Trick (2-SAT)
usb host 驱动 - UVC 掉包
Documents to be used in IC design process
Blue Bridge Cup microbial proliferation C language
【翻译】数字内幕。KubeCon + CloudNativeCon在2022年欧洲的选择过程
Use of map (the data of the list is assigned to the form, and the JSON comma separated display assignment)
About image reading and processing, etc
蓝桥杯 微生物增殖 C语言
The "white paper on the panorama of the digital economy" has been released with great emphasis on the digitalization of insurance
Configuration and simple usage of the EXE backdoor generation tool quasar