반응형
#include < iostream>
#include < algorithm>
using namespace std;
int n,m;
bool alpha[256];
char board[20][20];
int dy[4] = {-1,0,1,0};
int dx[4] = {0,-1,0,1};
bool check(int y,int x)
{
for(int i=0;i<256;i++){
if(alpha[i] == true)
return false;
}
return true;
}
int cnt;
int maxNum=0;
void go(int y,int x)
{
if(check(y,x)){
return ;
}
for(int dir=0; dir<4;dir++){
int ny = y + dy[dir];
int nx = x + dx[dir];
if(ny<0 || nx<0 || ny>=n || nx>=m){
continue;
}
if(alpha[board[ny][nx]] == true){
alpha[board[ny][nx]] = false;
cnt++;
go(ny,nx);
maxNum = max(maxNum,cnt);
alpha[board[ny][nx]] = true;
cnt--;
}
}
return ;
}
int main()
{
cin>>n>>m;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin>>board[i][j];
alpha[board[i][j]] = true;
}
}
alpha[board[0][0]] = false;
go(0,0);
cout<<maxNum+1;
return 0;
}
https://www.acmicpc.net/problem/1987
1987번: 알파벳
문제 세로 R칸, 가로 C칸으로 된 표 모양의 보드가 있다. 보드의 각 칸에는 대문자 알파벳이 하나씩 적혀 있고, 좌측 상단 칸 (1행 1열) 에는 말이 놓여 있다. 말은 상하좌우로 인접한 네 칸 중의 한 칸으로 이동할 수 있는데, 새로 이동한 칸에 적혀 있는 알파벳은 지금까지 지나온 모든 칸에 적혀 있는 알파벳과는 달라야 한다. 즉, 같은 알파벳이 적힌 칸을 두 번 지날 수 없다. 좌측 상단에서 시작해서, 말이 최대한 몇 칸을 지날 수 있는지를 구하는
www.acmicpc.net
반응형
'개발자 > 알고리즘' 카테고리의 다른 글
[BOJ] 2580번 스도쿠 (0) | 2019.10.15 |
---|---|
[BOJ] 14889번 스타트와 링크 (0) | 2019.10.13 |
[BOJ] 1339번 단어수학 (0) | 2019.10.13 |
[BOJ] 1748번 수 이어 쓰기1 (0) | 2019.10.13 |
[BOJ] 1261번 알고스팟 -deque (0) | 2019.10.12 |