24h購物| | PChome| 登入
2012-05-27 21:24:53| 人氣178| 回應0 | 上一篇 | 下一篇

[UVA] 11244 - Counting Stars

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

A

Counting Stars

Input: Standard Input

Output: Standard Output

 

 

People generally don’t care to give attention to stars in a moonlit night. In most cases the attention goes towards the moon. Sadly, you have to write a program now that can count the stars in the sky. For this problem a sky is a two dimensional grid. Empty pixel is denoted by a ‘.’ (ASCII value 46) and a non-empty pixel is denoted by a ‘*’ (ASCII value 42). As a star is a very small object so it cannot occupy more than one pixel and in our sky two stars are never adjacent. So two or more adjacent non-empty pixels can denote some larger objects like moon, comet, sun or UFOs but they never represent a star. All the eight possible pixels around a pixel are adjacent to it. In the figure below the black pixel at the center have eight adjacent pixels. Of them three pixels are non-empty. 

*..

.**

..*

Input

The input file contains at most 1000 sets of inputs. The description of each set is given below:

 

Each set starts with two integer number r and c (0< r, c<101), which indicates the row and column number of the image to follow. Next r rows describe the sky as mentioned in the problem statement.

 

Input is terminated by a line containing two zeroes.

 

Output

For each set of input produce one line of output. This line contains a decimal integer which denotes the number of stars in the given sky.

 

Sample Input                                Output for Sample Input

5 5

.....

....*

....*

...*.

*....

4 3

...

.*.

...

*.*

0 0

1

3

 


Problemsetter: Shahriar Manzoor

Special Thanks: Syed Monowar Hossain


#include <stdio.h>

int main() {
    int n, m;
    while(scanf("%d %d", &n, &m) == 2) {
        if(n == 0 && m == 0)
            break;
        char map[102][102];
        int i, j, a, b;
        for(i = 0; i < n; i++)
            scanf("%s", map[i]);
        int ans = 0, x, y;
        for(i = 0; i < n; i++) {
            for(j = 0; j < m; j++) {
                if(map[i][j] == '*') {
                    int flag = 0;
                    for(a = -1; a <= 1; a++) {
                        for(b = -1; b <= 1; b++) {
                            if(a == 0 && b == 0)
                                continue;
                            x = i+a, y = j+b;
                            if(x < 0 || y < 0 || x >= n || y >= m)
                                continue;
                            if(map[x][y] == '*')
                                flag = 1;
                        }
                    }
                    if(flag == 0)
                        ans++;
                }
            }
        }

        printf("%d\n", ans);
    }
    return 0;
}

台長: Morris
人氣(178) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][BFS] 10946 - You want what filled?
此分類上一篇:[UVA][MST] 1235 - Anti Brute Force Lock

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