Under construction

Still being built. Things may change

Snake Game

Time limit1sMemory limit1024 MB

Summary
Find the maximum length a snake can reach on an arrow grid, where apples change its path and the game ends when the head hits its body or the wall.
Level

Hard8 of 10

Topics
Graph, Simulation, Implementation
Solved
No attempts yet

Problem

Sookkek took part in Myeongrang Hajae, the annual freshman game development event run by the KAIST game development club Hajae, and built an automated snake game. The goal is to make the snake as long as possible to raise the score. The map is a grid of HH rows and WW columns of rectangular tiles.

  1. Every tile has one of an up, right, down, or left arrow.
  2. Some tiles have an apple on them.

One snake is on the map. It consists of a head, a body, and a tail. Among the tiles the snake occupies, the one it occupied last is the head, and the one it occupied first is the tail. The rest are body tiles. At the start, the snake occupies two adjacent tiles, one head and one tail. The starting score is the number of tiles the snake occupies. The snake repeats the following steps.

  1. Choose the tile where the head grows. The new head is the cell one step from the current head in the direction of the arrow on the head's tile.
    • However, if the arrow on the tile points opposite to the snake's moving direction, ignore the arrow and take the cell one step in the moving direction instead.
    • At the start, the snake's moving direction is from the tail toward the head.
  2. If there is no apple at the new head position, or the position is outside the map, the snake leaves the tail tile and no longer occupies it. The first tile the snake occupied among the remaining tiles becomes the new tail.
  3. If there is an apple at the new head position, the snake eats it. An eaten apple disappears.
  4. The snake occupies the new head position. If this position is outside the map or already occupied by the snake, the game ends immediately.
  5. The score becomes the total number of tiles the snake occupies.

Sookkek wants to know the maximum score this game can reach. Find it for him while he is busy drawing art.

Input

The first line contains HH, WW, r1r_1, c1c_1, r2r_2, and c2c_2, separated by spaces. The map has HH rows and WW columns. The snake's starting head is at (r1,c1)(r_1, c_1), and its starting tail is at (r2,c2)(r_2, c_2). The top-left tile is (1,1)(1, 1), and the tile in row rr and column cc is (r,c)(r, c). (3≤H,W≤5003 \le H, W \le 500; 1≤r1,r2≤H1 \le r_1, r_2 \le H; 1≤c1,c2≤W1 \le c_1, c_2 \le W; ∣r1−r2∣+∣c1−c2∣=1|r_1-r_2| + |c_1-c_2| = 1)

The next HH lines each contain WW integers from 00 to 33, separated by spaces. The cc-th number on the rr-th line is the arrow on tile (r,c)(r, c). Up, right, down, and left arrows are 0,1,2,30, 1, 2, 3.

The next line contains the number of apples AA. (1≤A≤H×W−21 \le A \le H \times W - 2)

The next AA lines each contain two integers pip_i and qiq_i, separated by a space. The ii-th apple is at (pi,qi)(p_i, q_i). No tile has two or more apples, and no apple is on the tile where the snake starts. (1≤pi≤H1 \le p_i \le H; 1≤qi≤W1 \le q_i \le W)

Output

Print the maximum score this game can reach.

Hint

The game may never end depending on the map.

Examples3

  1. Example 1

    Input
    3 4 1 2 1 1
    1 1 1 2
    0 2 3 2
    0 3 0 3
    4
    1 4
    2 2
    2 3
    3 3
    
    Expected output
    6
    
  2. Example 2

    Input
    3 4 2 4 3 4
    1 1 1 2
    0 2 3 2
    0 3 0 3
    4
    1 4
    2 2
    2 3
    3 3
    
    Expected output
    3
    
  3. Example 3

    Input
    3 4 3 2 3 1
    1 1 1 2
    0 2 3 2
    0 3 0 3
    4
    1 4
    2 2
    2 3
    3 3
    
    Expected output
    5