HDU ACM 4414 Finding crosses 暴力枚举

分析:一个图,求图中‘#’组成了多少个十字架,注意十字架的宽度是奇数。对每个‘#’判断,上下左右 ,步长为1 ,2,。。。25是不是都符合条件,符合的话判断个数为奇数即可。

#include<iostream>
using namespace std;

#define N 50
char map[N][N];
int dir[4][2]={
	{-1,0},
	{0,-1},
	{1,0},
	{0,1}};
int n,ans;

bool valid(int x,int y)
{
	return x>=0 && x<n && y>=0 && y<n;
}

bool judge(int x,int y)
{
	int cnt=0,tmpc,i,j,x2,y2;

	for(i=1;i<=25;i++)            //枚举步长
	{
		tmpc=0;
		for(j=0;j<4;j++)          //四个方向
		{
			x2=x+dir[j][0]*i;
			y2=y+dir[j][1]*i;
			if(valid(x2,y2) && map[x2][y2]=='#')
			{
				tmpc++;
				if(j%2==0)  //左右
				{
					if(y2>0&&map[x2][y2-1]=='#' || y2<n-1 && map[x2][y2+1]=='#')   //是否存在相邻的
						return false;
				}
				else       //上下
				{
					if(x2>0&&map[x2-1][y2]=='#' || x2<n-1 && map[x2+1][y2]=='#')   //是否存在相邻的
						return false;
				}
				cnt++;
			}
		}
		if(tmpc==0) break;           //一个都没有
		if(tmpc!=4) return false;
	}
	if(cnt%2==0 && cnt>0) return true;
	else return false;
}

int solve()
{
	int i,j;

	ans=0;
	for(i=0;i<n;i++)
		for(j=0;j<n;j++)
			if(map[i][j]=='#' && judge(i,j)) ans++;
	return ans;
}

int main()
{
	int i,j;

	while(cin>>n && n)
	{
		getchar();
		for(i=0;i<n;i++)
			for(j=0;j<n;j++)
				cin>>map[i][j];
		cout<<solve()<<endl;
	}
    return 0;
}
时间: 2024-10-09 23:57:31

HDU ACM 4414 Finding crosses 暴力枚举的相关文章

HDU ACM 5254 棋盘占领-&gt;暴力枚举

分析:暴力,注意有公共点是指两个城池是否相邻. #include<iostream> #include<vector> using namespace std; bool map[505][505]; vector<int> vx; vector<int> vy; int n,m; bool judge(int x,int y) { bool t1,t2,t3,t4; t1=t2=t3=t4=0; if(x>1 && map[x-1][

hdu 4932 Miaomiao&#39;s Geometry 暴力枚举

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4932 Miaomiao's Geometry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 694    Accepted Submission(s): 180 Problem Description There are N point

HDU 4930 Fighting the Landlords(暴力枚举+模拟)

HDU 4930 Fighting the Landlords 题目链接 题意:就是题中那几种牌型,如果先手能一步走完,或者一步让后手无法管上,就赢 思路:先枚举出两个人所有可能的牌型的最大值,然后再去判断即可 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; struct Player { int rank[15]; } p1, p2; int t, h

HDU 4770 Lights Against Dudely 暴力枚举+dfs

又一发吐血ac,,,再次明白了用函数(代码重用)和思路清晰的重要性. 11779687 2014-10-02 20:57:53 Accepted 4770 0MS 496K 2976 B G++ czy Lights Against Dudely Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1360    Accepted Subm

HDU 2601 An easy problem(暴力枚举/质因子分解)

An easy problem Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7963    Accepted Submission(s): 1920 Problem Description When Teddy was a child , he was always thinking about some simple math p

hdu 4445 Crazy Tank (暴力枚举)

Crazy Tank Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4562    Accepted Submission(s): 902 Problem Description Crazy Tank was a famous game about ten years ago. Every child liked it. Time f

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

HDU 1431 素数回文-暴力枚举

题目链接:点击打开链接http://acm.hdu.edu.cn/showproblem.php?pid=1431 这道题搞了几天一直Memory Limit Exceeded,始终找不到错误,求指教,不胜感激! 后附有枚举代码! #include <stdio.h> #include <string.h> #include <string> #include <math.h> #include <stdlib.h> #include <i

HDU 5288 OO’s Sequence (暴力枚举因子)

题目链接:HDU 5288 OO's Sequence 题意:给出一个n,表示n个数的序列,函数f(l,r)定义,在l,r区间中存在多少个数,不能被其他数整除.求累加所有子区间的函数值 思路:从ai的小范围入手 1.a不能被b整除,即a的所有因子中不存在b,所以打表枚举所有的数的因子. 2.找到一个数(位置为i)满足条件时最左端l和最右端r,(i-l)*(r-i)就是对答案的贡献. AC代码: #include <stdio.h> #include <algorithm> #inc