UVa 469 - Wetlands of Florida

题目:给你一个矩阵和某些点,找到给的点所处连续的W区域的面积(八个方向)。

分析:搜索。floodfill算法,利用搜索直接求解就可以了。

说明:注意读入数据的格式。

#include <iostream>
#include <cstdlib>
#include <cstdio>

using namespace std;

char maps[111][111];
int  used[111][111];

int dxy[8][2] = {-1,-1,-1,0,-1,1,0,-1,0,1,1,-1,1,0,1,1};
int dfs(int x, int y, int n, int m)
{
	if (x < 0 || x >= n || y < 0 || y >= m) return 0;
	if (maps[x][y] != 'W' || used[x][y]) return 0;
	used[x][y] = 1;
	int sum = 1;
	for (int i = 0 ; i < 8 ; ++ i)
		sum += dfs(x+dxy[i][0], y+dxy[i][1], n, m);
	return sum;
}

int main()
{
	int T,x,y,temp;
	scanf("%d",&T);getchar();
	for (int t = 1 ; t <= T ; ++ t) {
		temp = getchar();
		while (temp != 'W' && temp != 'L')
			temp = getchar();
		int count = 0;
		while (temp == 'W' || temp == 'L' ) {
			ungetc(temp, stdin);
			gets(maps[count ++]);
			temp = getchar();
		}
		int length = strlen(maps[0]);
		while (temp >= '0' && temp <= '9') {
			ungetc(temp, stdin);
			scanf("%d%d",&x,&y);getchar();
			memset(used, 0, sizeof(used));
			printf("%d\n",dfs(x-1, y-1, count, length));
			temp = getchar();
		}
		if (t < T) printf("\n");
	}
	return 0;
}
时间: 2024-10-25 22:27:23

UVa 469 - Wetlands of Florida的相关文章

Wetlands of Florida UVA - 469

题很简单 意思就是 给出某点坐标,看看附近有几个W,附近指的是8个方向,上.下.左.右.左上.左下.右上.右下. 就是输入格式有点麻烦.再一次体会到 sscanf 函数的 强大. #include <cstdio> #include <cstring> using namespace std; char map[105][105],used[105][105]; int ans; void dfs(int x, int y) { if(x < 0 || y < 0 ||

UVA - 12001 UVa Panel Discussion

Description  UVa Panel Discussion  The UVa online judge team is arranging a panel discussion for the next ACM-ICPC World Finals event in Orlando, Florida. They want that three or four of the contestants take part in the panel and as they have about 3

UVa 568 Just the Facts

A过去后看了一下别人的解法,发现除了打表还有一种数论的方法. 分析一下阶乘后面的0是怎么出现的呢,当然是2乘5得到的. 我们将1~N先放在一个数组里面. 从数组第一个元素开始,先统计一下N!中因子为5的个数记为count,将其除去,然后再除去count个2.这样一来的话把所有元素乘起来后就不会出现10的倍数了. 当然并不是真正的乘起来,那样的话肯定是要溢出的,因为只关心最后一位数,所以每次乘完后求10的余数即可. 我的做法是打表,因为题目里给了N <= 10000的条件限制,所以可以把1~100

UVA 562 Dividing coins --01背包的变形

01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 int c[102],d

UVA 10341 Solve It

Problem F Solve It Input: standard input Output: standard output Time Limit: 1 second Memory Limit: 32 MB Solve the equation: p*e-x + q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0 where 0 <= x <= 1. Input Input consists of multiple test cases and te

UVA 11014 - Make a Crystal(容斥原理)

UVA 11014 - Make a Crystal 题目链接 题意:给定一个NxNxN的正方体,求出最多能选几个整数点.使得随意两点PQ不会使PQO共线. 思路:利用容斥原理,设f(k)为点(x, y, z)三点都为k的倍数的点的个数(要扣掉一个原点O).那么全部点就是f(1),之后要去除掉共线的,就是扣掉f(2), f(3), f(5)..f(n).n为素数.由于这些素数中包括了合数的情况,而且这些点必定与f(1)除去这些点以外的点共线,所以扣掉.可是扣掉后会扣掉一些反复的.比方f(6)在f

[UVa] Palindromes(401)

UVA - 401 Palindromes Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A regular palindrome is a string of numbers or letters that is the same forward as backward. For example, the string "ABCDED

uva 401.Palindromes

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=342 题目意思:给出一段字符串(大写字母+数字组成).判断是否为回文串 or 镜像串 or 回文镜像串 or 什么都不是.每个字母的镜像表格如下 Character Reverse Character Reverse Character Reverse A A M M Y Y B

[2016-02-19][UVA][129][Krypton Factor]

UVA - 129 Krypton Factor Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description You have been employed by the organisers of a Super Krypton Factor Contest in which contestants have very high mental and physica