Posts

Showing posts with the label BFS

AM 789. The Maze III

需要多一个状态矩阵表示 方向 Code ( Language :C++) ( Judger :cloudjudge-cluster-9) Edit class Solution { public : /** * @param maze: the maze * @param ball: the ball position * @param hole: the hole position * @return: the lexicographically smallest way */ struct node { int x, y; node( int x, int y){ this ->x = x; this ->y = y; } }; const vector < int > dx = { -1 , 1 , 0 , 0 }; const vector < int > dy = { 0 , 0 , -1 , 1 }; const string movs = "udlr" ; //错误 把这里写错了,debug了半小时,简直不能忍!!!! const int dir = 4 ; bool canStop ( int x, int y, vector < vector < int >> &maze) { if (x < 0 || y < 0 || x >= maze.size() || y >= maze[ 0 ].size() || maze[x][y] == 1 ){ return true ; } return false ; } string findShortestWay ( vector < vector < int >...

AM 788. The Maze II

Code ( Language :C++) ( Judger :cloudjudge-cluster-9) Edit class Solution { public : /** * @param maze: the maze * @param start: the start * @param destination: the destination * @return: the shortest distance for the ball to stop at the destination */ struct node { int x, y; node( int x, int y){ this ->x = x; this ->y = y; } }; const vector < int > dx = { -1 , 1 , 0 , 0 }; const vector < int > dy = { 0 , 0 , -1 , 1 }; const int dir = 4 ; bool canStop ( int x, int y, vector < vector < int >> &maze) { if (x < 0 || y < 0 || x >= maze.size() || y >= maze[ 0 ].size() || maze[x][y] == 1 ){ return true ; } return false ; } int shortestDistance ( vector < vector < int >> &maze, vector < int > &start, vector < int > &d...

AM 787. The Maze

Code ( Language :C++) ( Judger :ip-172-31-12-4) Edit class Solution { public : /** * @param maze: the maze * @param start: the start * @param destination: the destination * @return: whether the ball could stop at the destination */ struct node { int x, y; node( int x, int y){ this ->x = x; this ->y = y; } }; const vector < int > dx = { -1 , 1 , 0 , 0 }; const vector < int > dy = { 0 , 0 , -1 , 1 }; const int dir = 4 ; bool canStop ( int x, int y, vector < vector < int >> &maze) { if (x < 0 || x >= maze.size() || y < 0 || y >= maze[ 0 ].size() || maze[x][y] == 1 ){ return true ; } return false ; } bool hasPath ( vector < vector < int >> &maze, vector < int > &start, vector < int > &destination) { // write yo...

796. Open the Lock

找最短路径嘛,BFS搞定 Description 中文 English You have a lock in front of you with 4 circular wheels. Each wheel has 10 slots:  '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' . The wheels can rotate freely and wrap around: for example we can turn  '9'  to be  '0' , or  '0'  to be  '9' . Each move consists of turning one wheel one slot. The lock initially starts at  '0000' , a string representing the state of the 4 wheels. You are given a list of  deadends  dead ends, meaning if the lock displays any of these codes, the wheels of the lock will stop turning and you will be unable to open it. Given a  target  representing the value of the wheels that will unlock the lock, return the minimum total number of turns required to open the lock, or -1 if it is impossible. 1.The length of deadends will be in the range  [1, 500] . 2. target  will not be in th...

AM 854. Closest Leaf in a Binary Tree

class Solution { public : /** * @param root: the root * @param k: an integer * @return: the value of the nearest leaf node to target k in the tree */ unordered_map <TreeNode*, vector <TreeNode*>> mp; int findClosestLeaf (TreeNode * root, int k) { // Write your code here TreeNode *target; buildGraph(root, NULL , target, k); std :: queue <TreeNode *> q; unordered_set <TreeNode *> set ; q.push(target); set .insert(target); while (!q.empty()){ TreeNode *tmp = q.front(); q.pop(); if (tmp->left == NULL && tmp->right == NULL ){ return tmp->val; } for ( auto i : mp[tmp]){ if (i == NULL || set .count(i)){ continue ; } set .insert(i); q.push(i); } } ...

AM 121. Word Ladder II

Code ( Language :C++) Edit class Solution { public : /* * @param start: a string * @param end: a string * @param dict: a set of string * @return: a list of lists of string */ vector < vector < string >> findLadders( string &start, string &end, unordered_set < string > &dict) { // write your code here // BFS找最短路径,然后依据最短路径DFS找所有可能。 //剩下的就看实现了 // Let's go!!! unordered_map < string , vector < string >> next; //建立graph,一个点指向哪些点(有connection) unordered_map < string , int > steps; // 每个点在path上距离起始点的距离。 vector < vector < string >> res; dict.insert(end); if (dict.size() == 0 ){ return res; } const int sLen = start.size(); int dist = 1 ; std :: queue < string > q; q.push(start); //dict.erase(start); steps[start] = 1 ; whi...