Algorithm
sw 1227. [S/W 문제해결 기본] 7일차 - 미로2
이무쿤
2020. 3. 28. 10:30
반응형
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
입력이 개행없이 주어지므로 string으로 받아야 함.
그거 말고는 간단.
#include<iostream>
#include<string.h>
using namespace std;
char arr[100][100];
bool visit[100][100];
bool flag;
int posy, posx ,starty,startx;
void move(int y, int x)
{
if (y == posy && x == posx)
{
flag = true;
return;
}
if (y + 1 < 100)
{
if (!visit[y + 1][x] && (arr[y+1][x] == '0' || arr[y + 1][x] == '3'))
{
visit[y + 1][x] = true;
move(y + 1, x);
visit[y + 1][x] = false;
}
}
if (y - 1 >= 0)
{
if (!visit[y - 1][x] && (arr[y - 1][x] == '0' || arr[y - 1][x] == '3'))
{
visit[y - 1][x] = true;
move(y - 1, x);
visit[y - 1][x] = false;
}
}
if (x + 1 < 100)
{
if (!visit[y][x + 1] && (arr[y][x+1] == '0' || arr[y][x+1] == '3'))
{
visit[y][x+1] = true;
move(y ,x+1);
visit[y][x+1] = false;
}
}
if (x - 1 >= 0)
{
if (!visit[y][x - 1] && (arr[y][x-1] == '0' || arr[y][x-1] == '3'))
{
visit[y][x - 1] = true;
move(y , x - 1);
visit[y][x - 1] = false;
}
}
}
int main(int argc, char** argv)
{
int test_case;
for (test_case = 1; test_case <= 10; ++test_case)
{
int tc;
cin >> tc;
for (int i = 0; i < 100; i++)
{
for (int j = 0; j < 100; j++)
{
cin >> arr[i][j];
if (arr[i][j] == '3')
{
posy = i;
posx = j;
}
else if (arr[i][j] == '2')
{
starty = i;
startx = j;
}
}
}
memset(visit, 0, sizeof(visit));
flag = false;
move(starty, startx);
if (flag)
{
cout << "#" << test_case << " " << 1 << endl;
}
else
{
cout << "#" << test_case << " " << 0 << endl;
}
}
return 0;//정상종료시 반드시 0을 리턴해야합니다.
}반응형