[A - 深さ優先探索] https://atcoder.jp/contests/atc001/tasks/dfs_a
この問題を解きたいと思い、下記のコードを書きました。
現在位置の上には進めるのですが、それ以外の方向に思ったように進みません。
配列の範囲についてエラーが出ているので、それと関係あると思うのですが、根本原因が分かりません。
デバッグをして、if文のあたりが怪しいような気がしています。
深さ優先探索の仕組みについては、概ね理解しているつもりです。
よろしくお願いします。
c++
1#include <bits/stdc++.h> 2using namespace std; 3 4bool find (int H, int W, vector<vector<char>> map, vector<vector<int>> stack, int a, int b) 5{ 6//現在地がgになったらtrue、終了 7 if(map.at(a).at(b) == 'g') 8 { 9 cout<<"goal"<<endl; 10 return true; 11 } 12 13 stack.push_back({a,b}); 14 map.at(a).at(b)='o'; 15 cout<<endl; 16 cout<<"------------------"<<endl; 17 cout << a<<","<<b<<endl; 18 for (int i = 0; i < H; ++i) 19 { 20 for (int j = 0; j < W; ++j) 21 { 22 cout<< map.at(i).at(j); 23 } 24 cout << endl; 25 } 26 27 28//進めるなら進む 29 if((map.at(a-1).at(b)=='.' || map.at(a-1).at(b)=='g') && 0 <= a-1 && a-1 < H && 0 <= b && b < W) 30 { 31 cout << "上"<< endl; 32 a-=1; 33 return find (H, W, map, stack, a, b); 34 } 35 else if ((map.at(a+1).at(b)=='.' || map.at(a+1).at(b)=='g') && 0 <= a+1 && a+1 < H && 0 <= b && b < W) 36 { 37 cout << map.at(a+1).at(b) <<endl; 38 cout << "下"<< endl; 39 a+=1; 40 find (H, W, map, stack, a, b); 41 } 42 else if ((map.at(a).at(b-1)=='.' || map.at(a).at(b-1)=='g') && 0 <= a && a < H && 0 <= b-1 && b-1 < W) 43 { 44 cout << "左"<<endl; 45 b-=1; 46 find (H, W, map, stack, a, b); 47 } 48 else if ((map.at(a).at(b+1)=='.' || map.at(a).at(b+1)=='g') && 0 <= a && a < H && 0 <= b+1 && b+1 < W) 49 { 50 cout << "右"<<endl; 51 b+=1; 52 find (H, W, map, stack, a, b); 53 } 54//四方行けなかったら、道が出てくるまで1マスずつ戻る 55 else 56 { 57 cout << "行き止まり"<<endl; 58 while (map.at(a-1).at(b)=='.' || map.at(a+1).at(b)=='.' || map.at(a).at(b-1)=='.' || map.at(a).at(b+1)=='.' || map.at(a-1).at(b)=='g' || map.at(a+1).at(b)=='g' || map.at(a).at(b-1)=='g' || map.at(a).at(b+1)=='g') 59 { 60 int i = 2; 61 a=stack.at(stack.size()-i).at(0); 62 b=stack.at(stack.size()-i).at(1); 63 i++; 64 //スタートを通過しても行ける場所が無かったら終了 65 if (i>stack.size()+10) 66 { 67 return false; 68 } 69 find (H, W, map, stack, a, b); 70 } 71 } 72} 73 74 75 76int main() 77{ 78 79 int H, W; 80 cin >> H >> W; 81 82//迷路入力 83 vector<vector<char>> map(H, vector<char>(W)); 84 for (int i = 0; i < H; ++i) 85 { 86 for (int j = 0; j < W; ++j) 87 { 88 cin >> map.at(i).at(j); 89 } 90 } 91 92//スタート地点確認 93 int a, b; 94 for (int i = 0; i < H; ++i) 95 { 96 for (int j = 0; j < W; ++j) 97 { 98 if (map.at(i).at(j) == 's') 99 { 100 a=i; 101 b=j; 102 } 103 } 104 } 105 106 vector<vector<int>> stack(0, vector<int>(0)); 107 108 bool result = find(H,W,map,stack,a,b); 109 cout << result << endl; 110} 111
input
14 3 2s.. 3.## 4... 5g.. 6
エラーメッセージ
[Wandbox] Start
prog.cc: In function 'bool find(int, int, std::__debug::vector<std::__debug::vector<char> >, std::__debug::vector<std::__debug::vector<int> >, int, int)':
prog.cc:72:1: warning: control reaches end of non-void function [-Wreturn-type]
72 | }
| ^
引用テキスト
terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check: __n (which is 4) >= this->size() (which is 4)
[Signal] Aborted
[Wandbox] Finish
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/01/27 11:05
2022/01/27 11:15
2022/01/28 03:02
2022/01/28 03:07