日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

4kyu Path Finder #1: can you reach the exit?

發(fā)布時(shí)間:2025/3/21 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 4kyu Path Finder #1: can you reach the exit? 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

4kyu Path Finder #1: can you reach the exit?

題目背景:

Task

You are at position [0, 0] in maze NxN and you can only move in one of the four cardinal directions (i.e. North, East, South, West). Return true if you can reach position [N-1, N-1] or false otherwise.

Empty positions are marked ., Walls are marked W,Start and exit positions are empty in all test cases.

題目分析:

迷宮問(wèn)題一般就是深搜,廣搜,最短路徑等經(jīng)典的圖論問(wèn)題的應(yīng)用場(chǎng)景,這種題目在OJ中算是基本題目,刷了些題目的看到這種題目就很熟悉了。本道題我用廣搜去處理,其實(shí)廣搜的代碼很有套路,寫(xiě)了幾遍就知道如何寫(xiě)更舒服了,對(duì)于這種題型,其實(shí)應(yīng)用場(chǎng)景還是蠻好分析的,難點(diǎn)在于正確無(wú)誤地快速碼出經(jīng)典的搜索代碼。

AC代碼:

#include <iostream> #include <string> #include <cmath> #include <queue>using namespace std;int go[4][2] = {0, 1,0, -1,1, 0,-1, 0 };struct Node {int x, y; };queue<Node> Q;bool BFS(int length, string maze) {while( !Q.empty() ) {Node now = Q.front();Q.pop();for ( int i = 0; i < 4; i++ ) {int nx = now.x + go[i][0];int ny = now.y + go[i][1];if ( nx == length - 1 && ny == length - 1 ) return true;if ( nx < 0 || nx >= length || ny < 0 || ny >= length ) continue;if ( maze[nx * ( length + 1 ) + ny] == 'W' ) continue;maze[nx * ( length + 1 ) + ny] = 'W'; // make the tag, have goneNode tmp;tmp.x = nx;tmp.y = ny;Q.push(tmp);}}return false; }bool path_finder(string maze) {int length = std::floor( std::sqrt( (double) maze.size() ) );while(!Q.empty()) Q.pop();maze[0] = 'W';Node tmp;tmp.x = tmp.y = 0;Q.push(tmp);return BFS(length, maze); } 《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結(jié)

以上是生活随笔為你收集整理的4kyu Path Finder #1: can you reach the exit?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。