#include <iostream> using namespace std; | |
int map[20][20]; | |
bool visited[101]; | |
int dy[4]={1,1,-1,-1}; | |
int dx[4]={1,-1,-1,1}; | |
int N, mx; | |
int progress[4]; | |
void input(){ | |
cin >>N; | |
for(int i=0; i<N; i++) | |
for(int j=0; j<N; j++) | |
cin >> map[i][j]; | |
return; | |
} | |
void dfs(int sy, int sx, int y, int x, int dir, int depth){ | |
for(int i=dir; i<=dir+1; i++){ | |
if(i!=0 && progress[i-1]==0) continue; | |
int ny=y+dy[i]; | |
int nx=x+dx[i]; | |
if(ny>=0 && ny<N && nx>=0 && nx<N && !visited[map[ny][nx]] && i<4){ | |
visited[map[ny][nx]]=1; | |
progress[i]++; | |
dfs(sy, sx, ny, nx, i, depth+1); | |
progress[i]--; | |
visited[map[ny][nx]]=0; | |
} | |
if(i==3 && sy==ny && sx==nx){ | |
if(mx<depth) mx=depth; | |
return; | |
} | |
} | |
return; | |
} | |
int main(){ | |
int tc; | |
cin >>tc; | |
for(int T=1; T<=tc; T++){ | |
input(); | |
mx=0; | |
for(int i=0; i<N-2; i++){ | |
for(int j=1; j<N-1; j++){ | |
visited[map[i][j]]=1; | |
dfs(i, j, i, j, 0, 1); | |
visited[map[i][j]]=0; | |
} | |
} | |
if(mx==0) mx=-1; | |
cout << "#" <<T<< " " <<mx<<endl; | |
} | |
return 0; | |
} |
'알고리즘 문제 풀이 > 코딩 테스트 대비(~211220)' 카테고리의 다른 글
백준 1712 손익분기점 (0) | 2021.04.15 |
---|---|
180413_점심식사시간 (0) | 2018.04.13 |
활주로 건설 (0) | 2018.03.29 |
특이한 자석 (0) | 2018.03.28 |
숫자 만들기 (0) | 2018.03.26 |
#include <iostream> using namespace std; | |
int map[20][20]; | |
bool visited[101]; | |
int dy[4]={1,1,-1,-1}; | |
int dx[4]={1,-1,-1,1}; | |
int N, mx; | |
int progress[4]; | |
void input(){ | |
cin >>N; | |
for(int i=0; i<N; i++) | |
for(int j=0; j<N; j++) | |
cin >> map[i][j]; | |
return; | |
} | |
void dfs(int sy, int sx, int y, int x, int dir, int depth){ | |
for(int i=dir; i<=dir+1; i++){ | |
if(i!=0 && progress[i-1]==0) continue; | |
int ny=y+dy[i]; | |
int nx=x+dx[i]; | |
if(ny>=0 && ny<N && nx>=0 && nx<N && !visited[map[ny][nx]] && i<4){ | |
visited[map[ny][nx]]=1; | |
progress[i]++; | |
dfs(sy, sx, ny, nx, i, depth+1); | |
progress[i]--; | |
visited[map[ny][nx]]=0; | |
} | |
if(i==3 && sy==ny && sx==nx){ | |
if(mx<depth) mx=depth; | |
return; | |
} | |
} | |
return; | |
} | |
int main(){ | |
int tc; | |
cin >>tc; | |
for(int T=1; T<=tc; T++){ | |
input(); | |
mx=0; | |
for(int i=0; i<N-2; i++){ | |
for(int j=1; j<N-1; j++){ | |
visited[map[i][j]]=1; | |
dfs(i, j, i, j, 0, 1); | |
visited[map[i][j]]=0; | |
} | |
} | |
if(mx==0) mx=-1; | |
cout << "#" <<T<< " " <<mx<<endl; | |
} | |
return 0; | |
} |
'알고리즘 문제 풀이 > 코딩 테스트 대비(~211220)' 카테고리의 다른 글
백준 1712 손익분기점 (0) | 2021.04.15 |
---|---|
180413_점심식사시간 (0) | 2018.04.13 |
활주로 건설 (0) | 2018.03.29 |
특이한 자석 (0) | 2018.03.28 |
숫자 만들기 (0) | 2018.03.26 |