bzoj:1675 [Usaco2005 Feb]Rigging the Bovine Election 竞选划区
Description
It's election time. The farm is partitioned into a 5x5 grid of cow locations, each of which holds either a Holstein ('H') or Jersey ('J') cow. The Jerseys want to create a voting district of 7 contiguous (vertically or horizontally) cow locations such that the Jerseys outnumber the Holsteins. How many ways can this be done for the supplied grid?
农场被划分为5x5的格子,每个格子中都有一头奶牛,并且只有荷斯坦(标记为H)和杰尔西(标记为J)两个品种.如果一头奶牛在另一头上下左右四个格子中的任一格里,我们说它们相连. 奶牛要大选了.现在有一只杰尔西奶牛们想选择7头相连的奶牛,划成一个竞选区,使得其中它们品种的奶牛比荷斯坦的多. 要求你编写一个程序求出方案总数.
Input
* Lines 1..5: Each of the five lines contains five characters per line, each 'H' or 'J'. No spaces are present.
Output
* Line 1: The number of distinct districts of 7 connected cows such that the Jerseys outnumber the Holsteins in the district.
Sample Input
JHJHJ
HHHHH
HJHHJ
HHHHH
Sample Output
HINT
.jpg)

usaco良心网站,直接暴力不会挂……233
暴力枚举点就行了,每次枚举就在已经选到的点周围选就行咯,随手带几个剪枝。
至于去重,直接hash就好。在累计答案处hash:64MS,每一层搜索都hash(去掉重复扩展的状态)20MS,玩火人工二分MOD:44MS、48MS……
运气#1
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int MOD=;
int ans=,xx,yy,k;
int hash[MOD];
char c[][];
void dfs(int x,int y,int hn,int jn,int p){
if (x<||y<||x>||y>) return;
if (x<xx||(x==xx&&y<yy)) return;
if (c[x][y]=='H') hn++;else jn++;
if (hn>) return;
p+=<<((x*+y));
k=p%MOD;
while(hash[k]!=-){
if (hash[k]==p) return;
k++;
if (k>=MOD) k-=MOD;
}
hash[k]=p;
if (hn+jn==){
ans++;
return;
}
char s=c[x][y];
c[x][y]=;
for (int i=;i<;i++)
for (int j=;j<;j++)
if (c[i][j]==){
if (c[i+][j]!=) dfs(i+,j,hn,jn,p);
if (c[i-][j]!=) dfs(i-,j,hn,jn,p);
if (c[i][j+]!=) dfs(i,j+,hn,jn,p);
if (c[i][j-]!=) dfs(i,j-,hn,jn,p);
}
c[x][y]=s;
}
int main(){
for (int i=;i<;i++) scanf("%s",c[i]);
memset(hash,-,sizeof(hash));
for (xx=;xx<;xx++)
for (yy=;yy<;yy++) dfs(xx,yy,,,);
printf("%d\n",ans);
}
bzoj:1675 [Usaco2005 Feb]Rigging the Bovine Election 竞选划区的更多相关文章
- 1675: [Usaco2005 Feb]Rigging the Bovine Election 竞选划区(题解第二弹)
1675: [Usaco2005 Feb]Rigging the Bovine Election 竞选划区 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: ...
- 1675: [Usaco2005 Feb]Rigging the Bovine Election 竞选划区(题解第一弹)
1675: [Usaco2005 Feb]Rigging the Bovine Election 竞选划区 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: ...
- 【BZOJ】1675: [Usaco2005 Feb]Rigging the Bovine Election 竞选划区(暴力dfs+set判重)
http://www.lydsy.com/JudgeOnline/problem.php?id=1675 一开始我写了个枚举7个点....... 但是貌似... 写挫了. 然后我就写dfs.. 判重好 ...
- bzoj1675 [Usaco2005 Feb]Rigging the Bovine Election 竞选划区
Description It's election time. The farm is partitioned into a 5x5 grid of cow locations, each of wh ...
- BZOJ 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛( 二分答案 )
最小最大...又是经典的二分答案做法.. -------------------------------------------------------------------------- #inc ...
- bzoj 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛
1734: [Usaco2005 feb]Aggressive cows 愤怒的牛 Description Farmer John has built a new long barn, with N ...
- [BZOJ 1733] [Usaco2005 feb] Secret Milking Machine 【二分 + 最大流】
题目链接:BZOJ - 1733 题目分析 直接二分这个最大边的边权,然后用最大流判断是否可以有 T 的流量. 代码 #include <iostream> #include <cs ...
- BZOJ 1733: [Usaco2005 feb]Secret Milking Machine 神秘的挤奶机
Description 约翰正在制造一台新型的挤奶机,但他不希望别人知道.他希望尽可能久地隐藏这个秘密.他把挤奶机藏在他的农场里,使它不被发现.在挤奶机制造的过程中,他需要去挤奶机所在的地方T(1≤T ...
- bzoj:3392: [Usaco2005 Feb]Part Acquisition 交易
Description 奶牛们接到了寻找一种新型挤奶机的任务,为此它们准备依次经过N(1≤N≤50000)颗行星,在行星上进行交易.为了方便,奶牛们已经给可能出现的K(1≤K≤1000)种货物 ...
随机推荐
- 【java】实现Interface java.lang.Comparable<T>接口的int compareTo(T o)方法实现对象数组或链表或集合的排序,和挽救式对象比较器Interface java.util.Comparator<T>
package 对象比较排序; import java.util.Arrays; class A implements Comparable<A>{ private String name ...
- canvas 从初级到XX 2# 让我们在之前的基础之上,再迈进一步吧 [中级向] (上)
还是老样子,先啰嗦一点前言. 最近各种事务缠身,所以也就隔了比较长的时间才开始码这篇文.希望不会这么快就过气. 好了,接下来就开始码代码.(写到中途,突然感觉到的.本篇设计大量初中物理知识,请怀念的往 ...
- iOS ShareSDK 三方分享/登录使用
原文 http://www.cnblogs.com/CoderAlex/p/4860352.html 一: 快速集成 1.前言 作为现在App里必不可少的用户分享需要,社交化分享显然是我们开发app里 ...
- Hybris 项目工程配置
1.控制台页面进入platform目录 cd F:\hybris640\hybris\bin\platform 并运行 setantenv.bat 生成对应的ant. 2.运行 ant moduleg ...
- lua 实现tableToString
function tableToString(studentNum) local str = "{ " str = str.."\n" for k, v in ...
- Missing number in array
Given an array of size n-1 and given that there are numbers from 1 to n with one missing, the missin ...
- Pashmak and Flowers
Pashmak decided to give Parmida a pair of flowers from the garden. There are nflowers in the garden ...
- python+selenium自动化测试_1
前言 回顾一下python+selenium基础,并整理相关知识点,分享给有需要,在前进道路上的朋友. print打印 #打印Hello World print("Hello World&q ...
- Elastic 技术栈之 Filebeat
Elastic 技术栈之 Filebeat 简介 Beats 是安装在服务器上的数据中转代理. Beats 可以将数据直接传输到 Elasticsearch 或传输到 Logstash . Beats ...
- tomcat中使用mysql连接池的配置
1.下载相应的jar包,添加到工程中 需要下载的包主要有commons-pool2-2.2 commons-dbcp2-2.0.1-src commons-dbcp2-2.0.1 commons-c ...