UVA 352 The Seasonal War

38 篇文章 0 订阅
12 篇文章 0 订阅

The inhabitants of Tigerville and Elephantville are engaged in a seasonal war. Last month, Elephantville successfully launched and orbited a spy telescope called the Bumble Scope. The purpose of the Bumble Scope was to count the number of War Eagles in Tigerville. The Bumble Scope, however, developed two problems because of poor quality control during its construction. Its primary lens was contaminated with bugs which block part of each image, and its focusing mechanism malfunctioned so that images vary in size and sharpness.
The computer programmers, who must rectify the Bumble Scope’s problems are being held hostage in a Programming Contest Hotel in Alaland by elephants dressed like tigers. The Bumble Scope’s awed images are stored by pixel in a le called Bumble.in. Each image is square and each pixel or cell contains either a 0 or a 1. The unique Bumble Scope Camera (BSC) records at each pixel location a 1 if part or all of a war eagle is present and a 0 if any other object, including a bug, is visible. The programmers must assume the following:
a) A war eagle is represented by at least a single binary one.
b) Cells with adjacent sides on common vertices, which contain binary ones, comprise one war eagle. A very large image of one war eagle might contain all ones.
c) Distinct war eagles do not touch one another. This assumption is probably awed, but the programmers are desperate.
d) There is no wrap-around. Pixels on the bottom are not adjacent to the top and the left is not adjacent to the right (unless, of course, there are only 2 rows or 2 columns)

Input and Output
Write a program that reads images of pixels from the input le (a text le), correctly counts the number of war eagles in the images and prints the image number and war eagle count for that image on a single line in the output le (also a text le).
Use the format in the sample output. Do this for each image in the input le. Each image will be preceded by a number indicating its square dimension. No dimension will exceed 25.

Sample input
6
100100
001010
000000
110000
111000
010100
8
01100101
01000001
00011000
00000010
11000011
10100010
10000001
01100000

Sample output
Image number 1 contains 3 war eagles.
Image number 2 contains 6 war eagles.

题意:
给出一个n*n的矩阵,输出有多少个1组成的不相连的块,每个位置和周围八个位置是相连的
思路:
dfs/bfs

#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
using namespace std;
int m;
int dx[]={1,1,1,0,0,-1,-1,-1};//声明8个方向
int dy[]={-1,0,1,-1,1,-1,0,1};
char Image[110][110];//记录数组
void initial()
{
    //string str;
    memset(Image,'0',sizeof(Image));//初始化
    for(int i=0;i<m;i++)
    {
        scanf("%s",Image[i]);
    }
}
void dfs(int x,int y)
{
    Image[x][y]='0';//访问过的位置标记为‘0’
    int nx,ny;
    for(int i=0;i<8;i++)
    {
        nx=x+dx[i];
        ny=y+dy[i];
        if(nx>=0&&nx<m&&ny>=0&&ny<m&&Image[nx][ny]=='1')
            dfs(nx,ny);
    }
}
int main()
{
    int i,k,sum,count=0;
    while(cin>>m)
    {
        count++;
        sum=0;
        initial();
        for(i=0;i<m;i++)
        {
            for(k=0;k<m;k++)
            {
                if(Image[i][k]=='1')//从数组中第一个为‘1’的位置开始搜索
                {
                    dfs(i,k);
                    sum++;//总数加1
                }
            }
        }
        cout<<"Image number "<<count<<" contains "<<sum<<" war eagles."<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值