Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binary Matrix #91

Merged
merged 3 commits into from
Feb 9, 2025
Merged

Conversation

zafstojano
Copy link
Collaborator

What does this PR do?

This PR introduces a procedural environment for the 01 Matrix problem. Given an n x n binary matrix, the task is to return the distance of the nearest 0 for each cell. The distance between two cells sharing a common edge is 1.

In order to ensure there exists a solution, I always prepend the generated binary matrix with a 0.

The solution is a level-order BFS traversal starting from the destination (the 0's).

Example

import reasoning_gym 


dataset = reasoning_gym.create_dataset("binary_matrix", size=5, seed=42, max_n=10, p_zero=0.2)

for i, item in enumerate(dataset):
    print(f"Sample {i+1}:")
    print(f"Question: {item['question']}")
    print(f"Answer:\n{item['answer']}\n")
    print(f"Metadata: {item['metadata']}\n")
    print("--------------------------------------------------\n")
Sample 1:
Question: Given a square matrix, your job is to find the distance of the nearest 0 for each cell.

Example:

Input: Rotate the matrix below by 90 degrees clockwise:
0 0 0
0 1 0
1 1 1

Output:
0 0 0
0 1 0
1 2 1

Find the distance to the nearest 0 for each cell in the matrix below:
0 0
1 1

Answer:
0 0
1 1

Metadata: {'matrix': [[0, 0], [1, 1]], 'solution': [[0, 0], [1, 1]]}

--------------------------------------------------

Sample 2:
Question: Given a square matrix, your job is to find the distance of the nearest 0 for each cell.

Example:

Input: Rotate the matrix below by 90 degrees clockwise:
0 0 0
0 1 0
1 1 1

Output:
0 0 0
0 1 0
1 2 1

Find the distance to the nearest 0 for each cell in the matrix below:
0

Answer:
0

Metadata: {'matrix': [[0]], 'solution': [[0]]}

--------------------------------------------------

Sample 3:
Question: Given a square matrix, your job is to find the distance of the nearest 0 for each cell.

Example:

Input: Rotate the matrix below by 90 degrees clockwise:
0 0 0
0 1 0
1 1 1

Output:
0 0 0
0 1 0
1 2 1

Find the distance to the nearest 0 for each cell in the matrix below:
0 1 1 0 1 1 1
1 0 1 1 1 1 1
1 1 1 1 1 0 1
1 1 1 1 1 1 1
0 1 0 0 1 1 0
1 1 1 1 1 1 1
1 1 1 1 0 1 1

Answer:
0 1 1 0 1 2 3
1 0 1 1 2 1 2
2 1 2 2 1 0 1
1 2 1 1 2 1 1
0 1 0 0 1 1 0
1 2 1 1 1 2 1
2 3 2 1 0 1 2

Metadata: {'matrix': [[0, 1, 1, 0, 1, 1, 1], [1, 0, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 0, 1], [1, 1, 1, 1, 1, 1, 1], [0, 1, 0, 0, 1, 1, 0], [1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 0, 1, 1]], 'solution': [[0, 1, 1, 0, 1, 2, 3], [1, 0, 1, 1, 2, 1, 2], [2, 1, 2, 2, 1, 0, 1], [1, 2, 1, 1, 2, 1, 1], [0, 1, 0, 0, 1, 1, 0], [1, 2, 1, 1, 1, 2, 1], [2, 3, 2, 1, 0, 1, 2]]}

--------------------------------------------------

Sample 4:
Question: Given a square matrix, your job is to find the distance of the nearest 0 for each cell.

Example:

Input: Rotate the matrix below by 90 degrees clockwise:
0 0 0
0 1 0
1 1 1

Output:
0 0 0
0 1 0
1 2 1

Find the distance to the nearest 0 for each cell in the matrix below:
0 1 1 1 0
1 0 1 1 1
1 1 0 1 1
0 1 1 1 1
0 0 0 1 1

Answer:
0 1 2 1 0
1 0 1 2 1
1 1 0 1 2
0 1 1 2 3
0 0 0 1 2

Metadata: {'matrix': [[0, 1, 1, 1, 0], [1, 0, 1, 1, 1], [1, 1, 0, 1, 1], [0, 1, 1, 1, 1], [0, 0, 0, 1, 1]], 'solution': [[0, 1, 2, 1, 0], [1, 0, 1, 2, 1], [1, 1, 0, 1, 2], [0, 1, 1, 2, 3], [0, 0, 0, 1, 2]]}

--------------------------------------------------

Sample 5:
Question: Given a square matrix, your job is to find the distance of the nearest 0 for each cell.

Example:

Input: Rotate the matrix below by 90 degrees clockwise:
0 0 0
0 1 0
1 1 1

Output:
0 0 0
0 1 0
1 2 1

Find the distance to the nearest 0 for each cell in the matrix below:
0 1
1 1

Answer:
0 1
1 2

Metadata: {'matrix': [[0, 1], [1, 1]], 'solution': [[0, 1], [1, 2]]}

--------------------------------------------------

Copy link
Contributor

@andreaskoepf andreaskoepf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I will later add that distance here means "Manhattan or taxicab distance".

@andreaskoepf andreaskoepf merged commit 1472de0 into open-thought:main Feb 9, 2025
3 checks passed
@zafstojano zafstojano deleted the env/binary-matrix branch February 9, 2025 19:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants