HDU 4414 Finding crosses(dfs)

Problem Description

The Nazca Lines are a series of ancient geoglyphs located in the Nazca Desert in southern Peru. They were designated as a UNESCO World Heritage Site in 1994. The high, arid plateau stretches more than 80 kilometres (50 mi) between
the towns of Nazca and Palpa on the Pampas de Jumana about 400 km south of Lima. Although some local geoglyphs resemble Paracas motifs, scholars believe the Nazca Lines were created by the Nazca culture between 400 and 650 AD.[1] The hundreds of individual
figures range in complexity from simple lines to stylized hummingbirds, spiders, monkeys, fish, sharks, orcas, llamas, and lizards.

Above is the description of Nazca Lines from Wikipedia. Recently scientists found out that those lines form many crosses. Do those crosses have something to do with the Christian religion? Scientists are curious about this. But at first, they want to figure
out how many crosses are there. So they took a huge picture of Nazca area from the satellite, and they need you to write a program to count the crosses in the picture.

To simplify the problem, we assume that the picture is an N*N matrix made up of ‘o‘ and ‘#‘, and some ‘#‘ can form a cross. Here we call three or more consecutive ‘#‘ (horizontal or vertical) as a "segment".

The definition of a cross of width M is like this:

1) It‘s made up of a horizontal segment of length M and a vertical segment of length M.

2) The horizontal segment and the vertical segment overlap at their centers.

3) A cross must not have any adjacent ‘#‘.

4) A cross‘s width is definitely odd and at least 3, so the above mentioned "centers" can‘t be ambiguous.

For example, there is a cross of width 3 in figure 1 and there are no cross in figure 2 ,3 and 4.

You may think you find a cross in the top 3 lines in figure 2.But it‘s not true because the cross you find has a adjacent ‘#‘ in the 4th line, so it can‘t be called a "cross". There is no cross in figure 3 and figure 4 because of the same reason.

Input

There are several test cases.

In each test case:

The First line is a integer N, meaning that the picture is a N * N matrix ( 3<=N<=50) .

Next N line is the matrix.

The input end with N = 0

Output

For each test case, output the number of crosses you find in a line.

Sample Input

4
oo#o
o###
oo#o
ooo#
4
oo#o
o###
oo#o
oo#o
5
oo#oo
oo#oo
#####
oo#oo
oo##o
6
ooo#oo
ooo##o
o#####
ooo#oo
ooo#oo
oooooo
0

Sample Output

1
0
0
0

Source

2012 ACM/ICPC Asia Regional Hangzhou Online

题意:找出十字架个数

思路:列举十字架的中点。dfs时传进去方向,方便推断十字架周围是不是有#(这是不合格的)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)
using namespace std;
#define N 55

int ans,le,ri,up,down,temp;
int n,flag;
int step[4][2]={-1,0,1,0,0,-1,0,1};//上,下,左,右

char a[N][N];

int judge(int x,int y)
{
	if(x>=0&&x<n&&y>=0&&y<n)
		return 1;
	return 0;
}

void dfs(int x,int y,int i)
{
	temp++;
	int j;
	if(i<2) j=2;
	else
		j=0;

	int xx=x+step[i][0];
	int yy=y+step[i][1];

	if(!judge(xx,yy)) return ;

	if(a[xx][yy]=='o') return ;

	int xup=xx+step[j][0];
	int ydn=yy+step[j][1];

	if(judge(xup,ydn)&&a[xup][ydn]=='#')
		{
		   flag=1;
	       return ;
		}
	 xup=xx+step[j+1][0];
	 ydn=yy+step[j+1][1];
	if(judge(xup,ydn)&&a[xup][ydn]=='#')
		{
			flag=1;
	       return ;
		}
	dfs(xx,yy,i);
}

int main()
{
	int i,j;
	while(scanf("%d",&n),n)
	{
		for(i=0;i<n;i++)
			scanf("%s",a[i]);

		ans=0;
		for(i=0;i<n;i++)
			for(j=0;j<n;j++)
			if(a[i][j]=='#')
		{
			temp=0;
			flag=0;
			dfs(i,j,0);
			up=temp;
			temp=0;
           if(flag) continue;
			dfs(i,j,1);
			down=temp;
			temp=0;

          if(flag) continue;
			dfs(i,j,2);
			le=temp;
			temp=0;

            if(flag) continue;
			dfs(i,j,3);

			ri=temp;
			temp=0;

			if(flag) continue;

			if(le==ri&&down==up&&le!=1&&up!=1) //左右相等。上下相等,不能是一条线
				ans++;
		}

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

版权声明:本文博客原创文章。博客,未经同意,不得转载。

时间: 2024-11-02 23:25:43

HDU 4414 Finding crosses(dfs)的相关文章

hdu 1716 排序2(dfs)

排列2 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5817    Accepted Submission(s): 2229 Problem Description Ray又对数字的列产生了兴趣:现有四张卡片,用这四张卡片能排列出很多不同的4位数,要求按从小到大的顺序输出这些4位数. Input 每组数据占一行,代表四张卡片上的数字(

HDU 5952 Counting Cliques(dfs)

Counting Cliques Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1855    Accepted Submission(s): 735 Problem Description A clique is a complete graph, in which there is an edge between every pai

HDU 6351 Beautiful Now(DFS)多校题解

思路:一开始对k没有理解好,k是指最多k次,不需要达到.这道题dfs暴力就行,我们按照全排列最大最小去找每一位应该和后面哪一位交换.k = 0没判断好WA了2发... 如果k >= len - 1,那么最大最小就是直接sort非前导零的答案.如果k < len - 1,那么我们交换肯定从最大位数交换,比如现在求最大值,那么我们从第一位依次判断,如果该位不是他后面最大的,那么就和后面最大的交换(如果最大的有多个,那么就每个都搜索一下),否则不消耗交换次数判断下一位.最小同理 注意前导零. 代码:

HDU 2553(N皇后)(DFS)

http://acm.hdu.edu.cn/showproblem.php?pid=2553 i表示行,map[i]表示列,然后用DFS遍历回溯 可以参考这篇文章: http://blog.csdn.net/cambridgeacm/article/details/7703739 1 #include <iostream> 2 #include <string> 3 #include <cstring> 4 #include <cstdlib> 5 #inc

HDU 5012 骰子旋转(DFS)

http://acm.hdu.edu.cn/showproblem.php?pid=5012 保存骰子的状态,然后用dfs或者bfs搜索 还是再讲一下dfs 我们的目标是找一个与b相同,且转次数最少的状态 dfs就是树状图,要明确每个状态下的分支,以及边界条件 有4种变换,所以每个状态下面有四种分支,又因为骰子转4次以上的状态没有意义,所以边界条件可以是4 每个状态起始时与b判断,如果相同,则更新结果 #include <iostream> #include <string> #i

HDU 2660 Accepted Necklace (DFS)

Accepted Necklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2474    Accepted Submission(s): 973 Problem Description I have N precious stones, and plan to use K of them to make a necklace f

hdu 1181 变形课 (dfs)简单搜索

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1181 变形课 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 17876    Accepted Submission(s): 6435 Problem Description 呃......变形课上Harry碰到了一点小麻烦,因为他并不

hdu 2160 Sequence one(DFS)

1 #include <iostream> 2 #include <vector> 3 #include <algorithm> 4 #include <cstdio> 5 #include <memory.h> 6 using namespace std; 7 #define MAXN 20002 8 9 10 //适用于正整数 11 template <class T> 12 inline void scan_d(T &r

HDU 1016-Prime Ring Problem(DFS)

Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 27595    Accepted Submission(s): 12271 Problem Description A ring is compose of n circles as shown in diagram. Put natural num