Lights
InterviewTime limit1sMemory limit128 MB
You press switches that flip the rectangle from the origin to the switch and need the fewest presses to turn every bulb on.
- Level
Medium5 of 10
- Topics
- Greedy, Prefix sum
- Solved
- No attempts yet
Problem
A rectangular panel has rows with bulbs in each row. The rows are numbered from 0 to and the columns from 0 to . Some of the bulbs are on and the rest are off.
The panel is wired to a switch board. The switches sit in a rectangle of rows and columns, the same size as the panel.
Pressing one switch flips the state of some bulbs. Bulbs that were on turn off, and bulbs that were off turn on. More precisely, pressing the switch at changes the state of every bulb at with and . In other words, it flips every bulb inside the rectangle whose opposite corners are and .
You are given a matrix with the initial state of the bulbs. The character 1 is a bulb that is on, and the character 0 is a bulb that is off.
Find the minimum number of presses needed to have every bulb on at the same time.
Input
The first line contains , the number of test cases.
Each test case starts with a line holding two integers and . The next lines hold characters each, and this matrix is the initial state of the bulbs. and are between 1 and 500 inclusive.
Output
For each test case, print one line in the format Case #x: M. Here is the case number starting from 1, and is the minimum number of presses needed to have every bulb on at the same time.