Grid Game

시간 제한2초메모리 제한1024 MB

요약
두 플레이어가 번갈아 양수 칸을 골라 값을 더 작은 XOR 결과로 바꾸고 오른쪽이나 아래로 이동하며, 최적 플레이에서 승자를 가린다.
난이도

보통10점 중 5점

유형
게임 이론, 동적 계획법, 수학, 구현
정답자
아직 제출이 없습니다

문제

Given a grid AA of size N×MN \times M. Each row is numbered from 11 to NN, and each column is numbered from 11 to MM. The cell at row rr and column cc is denoted as (r,c)(r, c).

Cell (r,c)(r, c) contains an integer A_r,cA\_{r,c}, which can be either −1-1 or a non-negative integer. If A_r,c=−1A\_{r,c} = -1, that means cell (r,c)(r, c) is impassable. Otherwise, cell (r,c)(r, c) is passable.

Two players will alternately take turns playing on this grid. In one turn, a player will do the following.

  1. Choose a cell with positive integer on it, and the player starts standing on that cell. Let xx be the integer at this starting cell.
  2. Choose a non-negative integer yy such that y<xy < x.
  3. Suppose that the player is standing on cell (r,c)(r, c). Update the value of A_r,cA\_{r,c} to A_r,c⊕x⊕yA\_{r,c} \oplus x \oplus y, where ⊕\oplus is the bitwise operator XOR.
  4. If either cell (r+1,c)(r + 1, c) or cell (r,c+1)(r, c + 1) is passable, the player must move to either passable cell of the player’s choosing. Then, repeat from step 3.
  5. If the player is no longer able to move, the player will step outside of the grid and end his turn.

A player who is unable to play on his turn (i.e. no positive integer on his turn) loses the game, and the opposing player wins the game.

If both players play optimally, determine who will win the game.

입력

Input begins with two integers NN MM (1≤N,M≤5001 ≤ N, M ≤ 500) representing the size of grid AA. Each of the next NN lines contains MM integers A_r,cA\_{r,c} (0≤A_r,c≤1090 ≤ A\_{r,c} ≤ 10^9 or A_r,c=−1A\_{r,c} = -1) representing the integer contained in cell (r,c)(r, c).

출력

If the first player win the game, output first in a single line. Otherwise, output second in a single line.

예제3

  1. 예제 1

    입력
    5 6
    0 0 0 0 0 0
    0 3 3 0 0 0
    0 0 3 -1 0 0
    0 0 3 3 3 3
    0 0 -1 -1 -1 -1
    
    예상 출력
    first
    
  2. 예제 2

    입력
    2 2
    1 1
    2 -1
    
    예상 출력
    first
    
  3. 예제 3

    입력
    1 1
    -1
    
    예상 출력
    second