UVA352 LA5642 The Seasonal War【DFS】
作者:互联网
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 flawed images are stored by pixel in a file 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 flawed, 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 file (a text file), 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 file (also a text file).
Use the format in the sample output. Do this for each image in the input file. 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.
问题链接:UVA352 LA5642 The Seasonal War
问题简述:给定一个n*n的矩阵,矩阵由1和0构成,输出有多少个1组成的不相连的块,每个位置和周围八个位置是相连通的。
思路。
问题分析:
用DFS解决,简单题,不解释。
该程序在LA5642(UVALive5642)中出现WA,不可理解。
程序说明:(略)
参考链接:(略)
题记:(略)
AC的C++语言程序如下:
/* UVA352 LA5642 The Seasonal War */
#include <bits/stdc++.h>
using namespace std;
const int DN = 8;
const int drow[] = {1, 1, 1, 0, 0, -1, -1, -1};
const int dcol[] = {-1, 0, 1, -1, 1, -1, 0, 1};
const int N = 25;
char grid[N][N + 1];
int n, cnt;
void dfs(int row, int col)
{
grid[row][col] = '0';
for(int i = 0; i < DN; i++) {
int nr = row + drow[i];
int nc = col + dcol[i];
if(nr >= 0 && nr < n && nc >= 0 && nc < n && grid[nr][nc] == '1')
dfs(nr, nc);
}
}
int main()
{
int caseno = 0;
while(scanf("%d", &n) == 1) {
for(int i = 0; i < n; i++)
scanf("%s", grid[i]);
cnt = 0;
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
if(grid[i][j] == '1') {cnt++; dfs(i, j);}
printf("Image number %d contains %d war eagles.\n", ++caseno, cnt);
}
return 0;
}
海岛Blog
发布了2133 篇原创文章 · 获赞 2306 · 访问量 255万+
关注
标签:LA5642,Seasonal,Bumble,int,image,number,UVA352,Scope,war 来源: https://blog.csdn.net/tigerisland45/article/details/104616793