Under construction

Still being built. Things may change

Nash Equilibrium

Time limit1sMemory limit128 MB

Summary
Given two m by n payoff matrices for players A and B, list every action pair where each player's action is a best response to the other's.
Level

Easy3 of 10

Topics
Brute force, Matrix, Implementation
Solved
No attempts yet

Problem

A two-player normal-form game between players AA and BB is completely described by:

  • {a1,…,am}\{a_1, \dots, a_m\}, the set of actions available to player AA;
  • {b1,…,bn}\{b_1, \dots, b_n\}, the set of actions available to player BB;
  • PAP_A, an m×nm \times n payoff matrix for player AA;
  • PBP_B, an m×nm \times n payoff matrix for player BB.

Both players choose their actions simultaneously — say player AA plays aia_i and player BB plays bjb_j. The payoffs are then read from the matrices: player AA receives PA[i,j]P_A[i,j] and player BB receives PB[i,j]P_B[i,j]. Each player wants to maximize their own payoff.

Given a fixed action bjb_j of player BB, a best response of player AA is any action aia_i that maximizes AA's payoff, that is, any aia_i with PA[i,j]=max⁡i′PA[i′,j]P_A[i,j] = \max_{i'} P_A[i',j]. Symmetrically, given a fixed action aia_i of player AA, a best response of player BB is any action bjb_j with PB[i,j]=max⁡j′PB[i,j′]P_B[i,j] = \max_{j'} P_B[i,j'].

A pair of actions (ai,bj)(a_i, b_j) is a pure-strategy Nash equilibrium if aia_i is a best response to bjb_j and, at the same time, bjb_j is a best response to aia_i.

Given the two payoff matrices PAP_A and PBP_B, find and list all pure-strategy Nash equilibria of the game.

Input

The input contains multiple test cases.

Each test case begins with a line containing two integers mm and nn (1≤m,n≤201 \le m, n \le 20). The next mm lines give the rows of payoff matrix PAP_A, and the following mm lines give the rows of payoff matrix PBP_B; each row lists nn integers. Every payoff value is an integer in the range −100-100 to 100100, inclusive.

The input ends with a line 0 0, which must not be processed.

Output

For each test case, let NN be the number of pure-strategy Nash equilibria of the game. Output:

  1. a line containing the single integer NN;
  2. then NN lines, each containing two integers ii and jj (1-indexed), meaning that (ai,bj)(a_i, b_j) is a Nash equilibrium.

List the equilibria in lexicographic order: (ai1,bj1)(a_{i_1}, b_{j_1}) comes before (ai2,bj2)(a_{i_2}, b_{j_2}) if i1<i2i_1 < i_2, or if i1=i2i_1 = i_2 and j1<j2j_1 < j_2.

Hint

Consider a game in which players AA and BB each have two actions, with the payoff matrices shown below:

Payoff matrices for the example game

If player AA plays a1a_1, then player BB maximizes their payoff by playing b1b_1, since PB[1,1]=1>0=PB[1,2]P_B[1,1] = 1 > 0 = P_B[1,2]. Likewise, if player BB plays b1b_1, then player AA maximizes their payoff by playing a1a_1, since PA[1,1]=1>0=PA[2,1]P_A[1,1] = 1 > 0 = P_A[2,1]. So a1a_1 is a best response to b1b_1 and vice versa, which makes (a1,b1)(a_1, b_1) a pure-strategy Nash equilibrium.

By contrast, (a2,b2)(a_2, b_2) is not a Nash equilibrium: if player AA plays a2a_2, then BB's best response is b1b_1, because PB[2,1]=5>3=PB[2,2]P_B[2,1] = 5 > 3 = P_B[2,2].

Examples1

  1. Example 1

    Input
    3 4
    1 5 -3 2
    4 2 -1 -3
    3 -2 5 9
    0 -4 -1 -5
    -9 2 8 3
    -3 4 -2 3
    2 2
    1 10
    0 5
    1 0
    10 5
    2 2
    1 1
    1 1
    1 1
    1 1
    0 0
    
    Expected output
    0
    1
    1 1
    4
    1 1
    1 2
    2 1
    2 2