24h購物| | PChome| 登入
2012-06-21 14:53:01| 人氣853| 回應0 | 上一篇 | 下一篇

[UVA] 469 - Wetlands of Florida

推薦 0 收藏 0 轉貼0 訂閱站台


 Wetlands of Florida 

A construction company owns a large piece of real estate within the state of Florida. Recently, the company decided to develop this property. Upon inspection of the property, however, it was revealed that the land, at various locations, contained bodies of water. This came as a shock to the owners of the company, for they were from out of state and not familiar with wetlands of Florida. The situation was very grave and the owners not knowing that such bodies of water can be converted to beautiful lakes that will increase the value of the land around them, were about to abandon the construction project. Fortunately, this fact was brought to the owners' attention by a smart FIU graduate who worked for the company and consequently the construction project started.

The engineers divided the construction site by a grid into uniform square cells such that each square cell entirely contained either water or land. (How they did it, of course, is anybody's guess.) Now, the question that the engineers are to answer is the following: ``Given the row and column number of a grid cell that contains water, what is the area of the lake containing that cell.'' (an area is measured by number of grid cells it contains. Diagonal cells are considered to be adjacent.)

You are to write a program to answer this question!

Input

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.

Each input set consists of tex2html_wrap_inline40 lines each containing tex2html_wrap_inline42 character long sequence of ``L''s and ``W''s followed by k > 0 lines each containing a pair of integers i and j. The first n lines will represent the tex2html_wrap_inline48 grid covering the land where a ``W''/``L'' at the tex2html_wrap_inline50 character of the tex2html_wrap_inline52 line indicates water/land within the cell at row r and column c of the grid. The pairs of integers on the last k lines, each represent the row and column numbers of some grid cell that contains water.

Output

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.

The output for each pair of integers, i and j, on the last k lines of input, consists of an integer, on a separate line, indicating the area of the lake containing the grid cell, at row i and column j of the grid.

Sample Input

1

LLLLLLLLL
LLWWLLWLL
LWWLLLLLL
LWWWLWWLL
LLLWWWLLL
LLLLLLLLL
LLLWWLLWL
LLWLWLLLL
LLLLLLLLL
3 2
7 5

Sample Output

12
4


不理速度了, 隨便打打上, 這題的輸入並不容易, 挺複雜的


#include <stdio.h>
#include <string.h>

char map[105][105], used[105][105];
int ans;
void dfs(int x, int y) {
if(x < 0 || y < 0 || map[x][y] == 0)
return;
if(used[x][y] != 0 || map[x][y] != 'W')
return;
used[x][y] = 1;
ans++;
int i, j;
for(i = -1; i <= 1; i++)
for(j = -1; j <= 1; j++)
dfs(x+i, y+j);
}
int main() {
int t, i, j;
char str[105];
scanf("%d ", &t);
while(t--) {
memset(map, 0, sizeof(map));
int n = 0;
while(gets(str)) {
if(str[0] == '\0')
break;
if(str[0] != 'W' && str[0] != 'L') {
sscanf(str, "%d %d", &i, &j);
memset(used, 0, sizeof(used));
ans = 0;
dfs(i-1, j-1);
printf("%d\n", ans);
} else {
sscanf(str, "%s", map[n]);
n++;
}
}
if(t)
puts("");
}
return 0;
}
 

台長: Morris
人氣(853) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][Bitset技巧] 11466 - Largest Prime Divisor
此分類上一篇:[UVA][并查集] 459 - Graph Connectivity

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文