뻘짓 1번. continue 말고 return 써가지고 안됨
뻘짓 2번. x랑 y바꿔 써서 안됨
뻘짓 3번. const MAX = 100 햇어야 됬는데 10 으로 해놔가지고 test case중에 10 넘는거 한개있었는데 거기서 계속 털림. 그래도 우연으로 찾은건 아니고 갓디버거를 사용해서 됨. 디버거가 없었더라면..... 내가 찾아낼 수 있었을가?
c++을 써야될 이유가 생겨서 처음으로 c++로 알고리즘 문제 풀어봄. 후....
#include <iostream>
using namespace std;
const int MAX = 100;
int N, M;
int Map[MAX][MAX];
int Visit[MAX][MAX] = {};
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};
void DFS (int x, int y, int depth){
if (x == N-1 && y == M-1){
if (depth < Map[x][y]){
Map[x][y] = depth;
}
return;
}
for (int i =0 ; i<4; i++){
int next_x = x + dx[i];
int next_y = y + dy[i];
if ( next_x <0 || next_y <0 || next_x >= N || next_y >= M) continue;
if (Visit[next_x][next_y] == 0 && Map[next_x][next_y] != 0){
Visit[next_x][next_y] = 1;
DFS(next_x, next_y, depth +1);
Visit[next_x][next_y] = 0;
}
}
}
int main(){
cin >> N >> M;
string input;
for (int i = 0; i < N; i ++){
cin >> input;
for (int j = 0; j < M; j++){
Map[i][j] = input[j] -'0';
}
}
Map[N-1][M-1] = N * M;
DFS(0,0,1);
cout << Map[N-1][M-1];
return 0;
}