Re: [閒聊] 每日leetcode

看板Marginalman作者 (franchouchouISBEST)時間1周前 (2024/04/22 23:12), 1周前編輯推噓5(503)
留言8則, 6人參與, 1周前最新討論串149/183 (看更多)
752. Open the Lock 想老半天 覺得不會是BFS吧 然後受不了偷看安紗 BFS真可以 然後刻老半天 一堆WA 心態又崩一天 一生就這樣了 int openLock(vector<string>& deadends, string target) { vector<int> step_cnt(10000, INT_MAX); step_cnt[0] = 0; //0000 queue<int> q; int tar = stoi(target); //init dead for(auto s : deadends) step_cnt[stoi(s)] = -1; // if 0000 is dead if(step_cnt[0] != -1) q.push(0); //0000 int unit_arr[4] = {1000, 100, 10, 1}; int unit2_arr[4] = {10000, 1000, 100, 10}; while(!q.empty()) { int cur = q.front(); q.pop(); int step_cur = step_cnt[cur]; if(cur == tar) return step_cnt[cur]; for(int i=0; i<4; i++) { // four digits int unit = unit_arr[i]; int unit2 = unit2_arr[i]; int dig = (cur - (cur/unit2 * unit2))/unit; int a = ((dig+1)%10)*unit + (cur%unit) + (cur/unit2)*unit2; int b = ((dig+9)%10)*unit + (cur%unit) + (cur/unit2)*unit2; if(step_cnt[a] == INT_MAX) { step_cnt[a] = step_cur+1; q.push(a); } if(step_cnt[b] == INT_MAX) { step_cnt[b] = step_cur+1; q.push(b); } } } return -1; } -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.137.188.131 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1713798748.A.1AA.html ※ 編輯: DJYOSHITAKA (114.137.188.131 臺灣), 04/22/2024 23:13:49

04/22 23:14, 1周前 , 1F
大師 別卷了
04/22 23:14, 1F

04/22 23:14, 1周前 , 2F
我是偷看提示才想到BFS 結果想錯一步
04/22 23:14, 2F

04/22 23:14, 1周前 , 3F
還是要看解答 我就爛:(((
04/22 23:14, 3F

04/22 23:14, 1周前 , 4F
你們都是大師
04/22 23:14, 4F

04/22 23:16, 1周前 , 5F
唉unit2應該直接改成unit*10 懶改了==
04/22 23:16, 5F

04/22 23:16, 1周前 , 6F
大師
04/22 23:16, 6F

04/22 23:16, 1周前 , 7F
別卷了
04/22 23:16, 7F

04/22 23:28, 1周前 , 8F
大師
04/22 23:28, 8F
文章代碼(AID): #1c9dvS6g (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1c9dvS6g (Marginalman)