35 lines
921 B
TypeScript
35 lines
921 B
TypeScript
export {};
|
|
|
|
const input: string[] = require("fs").readFileSync(0).toString().trim().split('\n');
|
|
const [R, C]: number[] = input[0].split(' ').map(Number);
|
|
|
|
let board: string[][] = [];
|
|
for(let i=1; i<=R; i++) {
|
|
board.push(input[i].split(''));
|
|
}
|
|
|
|
let alphabet: boolean[] = new Array(26).fill(false);
|
|
alphabet[board[0][0].charCodeAt(0) - 65] = true;
|
|
let maxStep: number = 1;
|
|
|
|
const dx: number[] = [1,-1,0,0];
|
|
const dy: number[] = [0,0,1,-1];
|
|
|
|
const dfs = ([cx, cy, cs]: number[]) => {
|
|
maxStep = Math.max(cs, maxStep);
|
|
|
|
for(let i=0; i<4; i++) {
|
|
const nx = cx + dx[i];
|
|
const ny = cy + dy[i];
|
|
|
|
if(0<=nx && nx<C && 0<=ny && ny<R && !alphabet[board[ny][nx].charCodeAt(0) - 65]) {
|
|
alphabet[board[ny][nx].charCodeAt(0) - 65] = true;
|
|
dfs([nx,ny,cs+1]);
|
|
alphabet[board[ny][nx].charCodeAt(0) - 65] = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
dfs([0,0,1]);
|
|
|
|
console.log(maxStep); |