1. 程式人生 > >POJ 3984迷宮問題(BFS+列印)

POJ 3984迷宮問題(BFS+列印)

在這裡插入圖片描述

加了列印路徑,從後往前打。
AC程式碼:

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int vis[10][10], Map[10][10];
int head, tot;
int dx[4] = { 0, 1, -1, 0 };
int dx[4] = { 0, 1, -1, 0 };
int dy[4] = { 1, 0, 0, -1 };
struct node {
int x; int y; int pre; } num[1100]; void print(int n){ if (num[n].pre != -1) { print(num[n].pre); printf("(%d, %d)\n", num[n].x, num[n].y); } return; } void bfs(){ //注意區分head和tot,前一個表示每一步,後一個表示有多少種嘗試 head = 0; tot = 0; while (head < tot) { for
(int i = 0; i < 4; i++) { int m = num[head].x + dx[i]; int n = num[head].y + dy[i]; if (m >= 5 || n >= 5 || m < 0 || n < 0 || Map[m][n] == 1) continue; Map[m][n] = 1; num[tot].x = m; num[tot].y = n; num[
tot].pre = head; tot++; if (m == 4 && n == 4) print(head); } head++; } } int main() { for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) scanf("%d", &Map[i][j]); } printf("(0, 0)\n"); bfs(); printf("(4, 4)\n"); return 0; }

相關推薦

POJ 3984迷宮問題BFS+列印

加了列印路徑,從後往前打。 AC程式碼: #include <cstdio> #include <cstdlib> #include <iostream> #inc

POJ ~ 3414 ~ Pots BFS+列印路徑

題意:有兩個無刻度的容量分別為A,B升的杯子,通過一些操作使某一個杯子中有C升的水。 1. FILL(i) ,將i這個杯子中的水接滿 2. DROP(i),將i這個杯子中的水倒掉 3. POUR(i,j),將i這個杯子中的水倒入j這個杯子,能倒完就倒完,倒不完就留在杯子中。

POJ-3414 POTSBFS列印路徑

問題描述 You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: FILL(i

POJ-3984-迷宮問題-BFS(廣搜)-手寫隊列

org ast href || while div 要去 廣搜 trac 題目鏈接:http://poj.org/problem?id=3984 這個本來是個模板題,可是老師要去不能用STL裏的queue,得自己手寫解決。ORZ....看別人的博客學習。新技能get。。

POJ 3984 迷宮問題 (BFS + Stack)

while read 輸出 目標 ted ring 節點 fin mar 鏈接 : Here! 思路 : BFS一下, 然後記錄下每個孩子的父親用於找到一條路徑, 因為尋找這條路徑只能從後向前找, 這符合棧的特點, 因此在輸出路徑的時候先把目標節點壓入棧中, 然後不斷

POJ-3414-PotsBFS 模擬

H - Pots Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3414 App

POJ-3984-迷宮問題BFS

Description 定義一個二維陣列: int maze[5][5] = {0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0,}; 它表示一個迷宮,其中的1表示牆壁,0表示可以

poj 3984 迷宮問題BFS+回溯

迷宮問題 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11256 Accepted: 6733 Description 定義一個二維陣列: int maze[5][5] = {

poj 3984迷宮問題bfs求最短路徑 類似並查集儲存上個節點 儲存最短路徑

迷宮問題 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16343 Accepted: 9762 Description 定義一個二維陣列: int maze[5][5] = { 0,

POJ - 3984 - 迷宮問題 DFS

define class ac代碼 ng- art clu cstring fff table 迷宮問題 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10936 Accep

POJ 3984 迷宮問題搜尋

迷宮問題 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 33756   Acce

noip 01迷宮BFS+記憶化

一個 col using char rst clu tps str show 題目鏈接:https://www.luogu.org/problem/show?pid=1141 題意:給出一個僅由數字0與1組成的n×n格迷宮。放0的那一格可以4個方向走到放1那一

POJ 3984 迷宮問題【BFS/路徑記錄】

++ debug r++ scanf out ace AS ctype mis 迷宮問題 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31428 Accepted: 18000 Description

HPU暑期第五次積分賽 - G-迷宮BFS+最短路徑

題目 程式碼 #include <iostream> #include <cstdio> #include <queue> #include <cstring> using namespace std; int mp[110][11

POJ】1465MultipleBFS+同餘剪枝

Time Limit: 1000MS   Memory Limit: 32768K Total Submissions: 8365   Accepted:&nb

hdu1728 逃離迷宮BFS

題意:經典的迷宮問題的基礎上,條件是轉彎次數的限制,最多k次轉彎。 對於這種情況,使用BFS不再是每次添加當前位置周圍1步的點,而是將除了走過的以外三個方向上所有的點放入佇列。跳入的坑比較多,因為懶,所以沒有重新寫,總是在之前的上面修修補補。新增點的時候,同一方向上,遇到*

HDOJ 1728 逃離迷宮BFS,拐彎次數最少

開始拿到這道題,想到先前寫過的BFS,由出口到終點,最短路徑。後來一看題目不是這個意思,題目要求拐彎數最少到達終點而不是求最短路徑。 看了解題報告,有了一點點思路,就是先選定一條方向,然後把該方向

POJ 3083 Children of the Candy CornBFS + DFS

題意:給定w×h的矩陣,分別求從S到T:沿著左邊牆壁的走的路徑,沿著右邊牆壁走的路徑,最短路徑。 思路:最短路徑直接利用bfs搜尋可得。對於沿著牆壁走,記錄當前的狀態為(當前點所在的座標,當前前行的方向),例如沿著左邊牆壁走,若一下不會做,可以分情況考慮,

poj 3414 Pots bfs+路徑記錄

Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10071 Accepted: 4237 Special Judge Description You are given two

poj 3414 Potsbfs+輸出回溯路徑

給你兩個容器,分別能裝下A升水和B升水,並且可以進行以下操作 FILL(i) 將第i個容器從水龍頭裡裝滿(1 ≤ i ≤ 2); DROP(i) 將第i個容器抽乾 POUR(i,j) 將第i個容