Banner Home Page Web Contests Problems Ranklist Status Statistics

Bubble Shooter

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 56   Accepted Submission(s) : 22
Problem Description
Bubble shooter is a popular game. You can find a lot of versions from the Internet.<br><br><img src=/data/images/1547-1.jpg><br><br>The goal of this game is to clean the bubbles off the field. Every time you just point the cannon to where you want the next bubble to go, and if three or more of bubbles with the same color came together (including the newly shot bubble), they will detonate. After the first explode, if some bubbles are disconnected from the bubble(s) in the topmost row, they will explode too.<br><br>In this problem, you will be given an arranged situation of bubbles in the field and the newly shot bubble. Your program should output the total number of bubbles that will explode.<br>
 

Input
There are multiple test cases. Each test case begins with four integers H (the height of the field, 2 <= H <= 100), W (the width of the field, 2 <= W <= 100, in the picture above, W is 10), h (the vertical position of the newly shot bubble, count from top to bottom, and the topmost is counted as 1) and w (the horizontal position of the newly shot bubble, count from left to right, and the leftmost is counted as 1). <br>Then H lines follow, the odd lines will contain W characters while the even lines will contain W-1 characters (refer to the picture above). Each character will be either a lowercase from 'a' to 'z' indicating the color of the bubble in that position, or a capital letter 'E' indicating an empty position. You may assure the arranged situation is always valid (all the bubbles are directly or indirectly connected with at least one bubble in the topmost row, and the position of newly shot bubble is never empty).
 

Output
For each test case, output an integer indicating how many bubbles will explode.
 

Sample Input
2 2 2 1<br>aa<br>a<br>3 3 3 3<br>aaa<br>ba<br>bba<br>3 3 3 1<br>aaa<br>ba<br>bba<br>3 3 3 3<br>aaa<br>Ea<br>aab<br>
 

Sample Output
3<br>8<br>3<br>0<br>
 

Author
JIN, Tianpeng
 

Source
Zhejiang Provincial Programming Contest 2006
 

Statistic | Submit | Back