-
#include<iostream> using namespace std; int r, c; int map[20][20]; bool d[20][20]; bool a[27]; int dx[] = { 0,0,1,-1 }; int dy[] = { 1,-1,0,0 }; int ans; void go(int x, int y, int cnt) { if (cnt > 26) return; if (x == r && y == c) { ans = cnt > ans ? cnt : ans; return; } else { if (a[map[x][y]] == true) { ans = cnt > ans ? cnt : ans; return; } a[map[x][y]] = true; d[x][y] = true; for (int i = 0; i < 4; i++) { int nx = x + dx[i]; int ny = y + dy[i]; if (nx >= 0 && ny >= 0 && nx < r && ny < c) { go(nx, ny, cnt + 1); } } a[map[x][y]] = false; d[x][y] = false; } } int main() { cin >> r >> c; for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { char c; cin >> c; if (c == 'A') map[i][j] = 1; if (c == 'B') map[i][j] = 2; if (c == 'C') { map[i][j] = 3; } if (c == 'D') map[i][j] = 4; if (c == 'E') map[i][j] = 5; if (c == 'F') map[i][j] = 6; if (c == 'G') map[i][j] = 7; if (c == 'H') map[i][j] = 8; if (c == 'I') map[i][j] = 9; if (c == 'J') map[i][j] = 10; if (c == 'K') map[i][j] = 11; if (c == 'L') map[i][j] = 12; if (c == 'M') map[i][j] = 13; if (c == 'N') map[i][j] = 14; if (c == 'O') map[i][j] = 15; if (c == 'P') map[i][j] = 16; if (c == 'Q') map[i][j] = 17; if (c == 'R') map[i][j] = 18; if (c == 'S') map[i][j] = 19; if (c == 'T') map[i][j] = 20; if (c == 'U') map[i][j] = 21; if (c == 'V') map[i][j] = 22; if (c == 'W') map[i][j] = 23; if (c == 'X') map[i][j] = 24; if (c == 'Y') map[i][j] = 25; if (c == 'Z') map[i][j] = 26; } } go(0, 0, 0); if (ans == 0) ans = 1; cout << ans << "\n"; return 0; }
알파벳으로 하기귀찮아서 그냥 숫자로 다바꿔서했다.