티스토리 뷰
https://www.acmicpc.net/problem/16988
16988번: Baaaaaaaaaduk2 (Easy)
서기 2116년, 인간은 더 이상 AI의 상대가 되지 못하게 되었다. 근력, 순발력, 창의력, 사고력, 문제해결능력, 심지어 인간미조차 AI가 인간을 앞선다. AI가 온 지구를 관리하며 이미 인류는 지구의
www.acmicpc.net
class Node {
constructor(item) {
this.item = item;
this.next = null;
}
}
class Queue {
constructor() {
this.head = null;
this.tail = null;
this.length = 0;
}
push(item) {
const node = new Node(item);
if (this.head == null) {
this.head = node;
} else {
this.tail.next = node;
}
this.tail = node;
this.length += 1;
}
pop() {
const popItem = this.head;
this.head = this.head.next;
this.length -= 1;
return popItem.item;
}
}
const input = require('fs').readFileSync('./dev/stdin').toString().split('\n').map(v => v.split(' ').map(Number));
const [N, M] = input.shift();
const dx = [0, 0, -1, 1];
const dy = [-1, 1, 0, 0];
let board = input;
const possible = [];
for (let i = 0; i < N; i++) {
for (let j = 0; j < M; j++) {
if (board[i][j] == 0) {
possible.push([i, j])
}
}
}
let max = 0;
for (let n = 0; n < possible.length; n++) {
for (let m = n + 1; m < possible.length; m++) {
const [x1, y1] = possible[n];
const [x2, y2] = possible[m];
board[x1][y1] = 1;
board[x2][y2] = 1;
let visited = Array.from(Array(N), () => Array(M).fill(false));
let total = 0;
for (let i = 0; i < N; i++) {
for (let j = 0; j < M; j++) {
if (board[i][j] == 2 && !visited[i][j]) {
const q = new Queue();
q.push([i, j]);
visited[i][j] = true;
let impossible = false;
let cnt = 1;
while (q.length > 0) {
const [x, y] = q.pop();
for (let k = 0; k < 4; k++) {
const nx = x + dx[k];
const ny = y + dy[k];
if (nx < 0 || nx >= N || ny < 0 || ny >= M || visited[nx][ny]) continue;
if (board[nx][ny] == 2) {
visited[nx][ny] = true;
q.push([nx, ny])
cnt++;
} else if (board[nx][ny] == 0) {
impossible = true;
}
}
}
if (!impossible) total += cnt;
}
}
}
if (total > max) {
max = total;
}
board[x1][y1] = 0;
board[x2][y2] = 0;
}
}
function check(x, y) {
for (let k = 0; k < 4; k++) {
const nx = x + dx[k];
const ny = y + dy[k];
if (nx < 0 || nx >= N || ny < 0 || ny >= M) continue;
if (board[nx][ny] == 0) return false;
}
return true;
}
console.log(max)
728x90
'자료구조 알고리즘 > 백준' 카테고리의 다른 글
Node.js) 백준 5585번: 거스름돈 (0) | 2023.07.14 |
---|---|
Node.js) 백준 7567번: 그릇 (0) | 2023.07.13 |
Node.js) 백준 10156번: 과자 (0) | 2023.07.11 |
Node.js) 백준 1722번: 순열의 순서 (0) | 2023.07.11 |
Node.js) 백준 17141번: 연구소 2 (0) | 2023.07.09 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 그래프
- 개발자면접
- 서버점검
- 최소공통조상
- node.js
- DB 생성
- BFS
- 서버개발
- 면접비
- 다이나믹프로그래밍
- 동적프로그래밍
- 투포인터 연습
- create databases;
- 다이나밍프로그래밍
- create db
- 은둔청년체험
- MySQL
- MOD
- 투포인터
- 로드나인
- 면접질문
- 롱베케이션
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
글 보관함