luogu#P2845. [USACO15DEC] Switching on the Lights S

    ID: 1810 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>模拟搜索2015USACO广度优先搜索 BFS深度优先搜索 DFS

[USACO15DEC] Switching on the Lights S

Background

Source: usaco-2015-dec.

Farmer John recently built a large set of barns arranged in an N×NN \times N rectangular grid (1<N1001 < N \leq 100).

However, Bessie is afraid of the dark and wants to know how many barns’ lights she can turn on.

Problem Description

There are N×NN \times N rooms forming an N×NN \times N grid. Bessie starts at the top-left corner (1,1)(1, 1) and can move only up, down, left, and right.

Initially, only the room (1,1)(1, 1) is lit, and Bessie may only move through rooms whose lights are on.

There are MM additional pieces of information. Each describes that a switch in room (a,b)(a, b) controls the light in room (c,d)(c, d).

Compute the maximum number of rooms whose lights can be turned on.

Input Format

The first line contains two integers N,MN, M (1<N1001 < N \leq 100, 1<M<2×1051 < M < 2 \times 10^5).

Lines 22 to M+1M+1 each contain four integers (x1,y1),(x2,y2)(x_1, y_1), (x_2, y_2), meaning the switch in room (x1,y1)(x_1, y_1) can turn on the light in room (x2,y2)(x_2, y_2).

Output Format

Output a single integer, the maximum number of rooms that can be lit.

3 6
1 1 1 2
2 1 2 2
1 1 1 3
2 3 3 1
1 3 1 2
1 3 2 1

5

Hint

Bessie can use the switch at (1,1)(1, 1) to turn on the lights in (1,2)(1, 2) and (1,3)(1, 3), then move to (1,3)(1, 3) and turn on (2,1)(2, 1), move to (2,1)(2, 1) and turn on (2,2)(2, 2). The switch at (2,3)(2, 3) is unreachable. Therefore, 55 rooms can be lit.

Translated by ChatGPT 5