백준 미로찾기 

https://www.acmicpc.net/problem/2178

 

2178번: 미로 탐색

첫째 줄에 두 정수 N, M(2 ≤ N, M ≤ 100)이 주어진다. 다음 N개의 줄에는 M개의 정수로 미로가 주어진다. 각각의 수들은 붙어서 입력으로 주어진다.

www.acmicpc.net

아 입력값 string인줄 모르고 int로 넣는라 삽질햇네요 ... ㅠ 

#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
#include<string>
using namespace std;
void Print(int n, int m, vector<vector<int>>maps) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
cout << maps[i][j];
}
cout << endl;
}
}

struct Data
{
int m;
int n;

int count;
};
int main()
{
int n;
int m;
cin >> m;//4
cin >> n;//6
int tmp;
vector<vector<int>>maps;

for (int i = 0; i < m; i++) {//4
vector<int> tmpMaps;
string tmpstr;
cin >> tmpstr;
for (int j = 0; j < n; j++) {//6
//cin >> tmp;

tmpMaps.push_back(tmpstr[j]-'0');
}
maps.push_back(tmpMaps);
}


if (maps.size()==0) {
cout << 0;
return 0;
}
if (maps.size() == 1) {
cout << 1;
return 0;
}

//Print(n,m,maps);
queue<Data> q;
Data tmpData;
tmpData.m = 0;
tmpData.n = 0;

if (maps[0][0]==0) {
 cout<< 0;
return 0;
}

maps[0][0] = 0;

tmpData.count = 1;
q.push(tmpData);

int arrayM[] = { 0,0,1,-1};
int arrayN[] = { 1,-1,0,0 };
vector<int>gCount;
bool bCheck = false;
while (!q.empty()) {
if (bCheck == true) {
break;
}
Data start = q.front();
q.pop();
for (int i = 0; i < 4; i++) {

if (start.m+arrayM[i]==-1|| start.n + arrayN[i] == -1
  ||start.n + arrayN[i]>= n|| start.m + arrayM[i] >= m)
{
continue;
}

if (maps[ start.m + arrayM[i] ] [ start.n + arrayN[i] ] == 1) {
start.count++;

if (start.n + arrayN[i]==n-1&& start.m + arrayM[i]==m-1) {

gCount.push_back(start.count);
bCheck = true;

i = 5;
continue;
}
maps[start.m + arrayM[i]][start.n + arrayN[i]] = 0;
Data tmpData2;
tmpData2.m = start.m + arrayM[i];
tmpData2.n = start.n + arrayN[i];

tmpData2.count = start.count;
q.push(tmpData2);

    start.count--;
}

}
}

sort(gCount.begin(), gCount.end());
cout << gCount[0];


return 0;
}

'알고리즘 공부' 카테고리의 다른 글

백준 토마토  (0) 2023.07.04
백준- 말이되고픈원숭이  (0) 2023.06.28
프로그래머스 카펫  (0) 2023.05.11
프로그래머스 소수 찾기  (0) 2023.05.10
프로그래머스 게임 맵 최단거리  (0) 2023.03.30

+ Recent posts