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)种货物 ...
随机推荐
- iOS 获取一个不变的UDID
原文:iOS7: 如何获取不变的UDID 如何使用KeyChain保存和获取UDID 本文是iOS7系列文章第一篇文章,主要介绍使用KeyChain保存和获取APP数据,解决iOS7上获取不变UDID ...
- 个人的MySql配置总结
lower_case_table_names参数是用来设置MySQL是否让Schema和数据表大小写敏感,我测试的是在查询界面和MySQL控制台界面无法改变它的值,要在配置文件中改变(先关闭服务),一 ...
- table常用的属性以及用法
<table><table/>先定义一个表格这个就没得讲了,<caption>表示这个表格的标题 <table border="6"> ...
- Who Will Win?
Gautam and Subhash are two brothers. They are similar to each other in all respects except one. They ...
- redis咋么实现分布式锁,redis分布式锁的实现方式,redis做分布式锁 积极正义的少年
前言 分布式锁一般有三种实现方式:1. 数据库乐观锁:2. 基于Redis的分布式锁:3. 基于ZooKeeper的分布式锁.本篇博客将介绍第二种方式,基于Redis实现分布式锁.虽然网上已经有各种介 ...
- Collection源码图
java基础是否扎实,在于多读源码,比如集合 IO Socket 多线程并发包等 最近将集合框架的源码读了以下,总结了一些,下图所示
- python logging一个通用的使用模板
import os import logbook from logbook.more import ColorizedStderrHandler from functools import wraps ...
- PCoA主坐标分析
来源:http://blog.sina.com.cn/s/blog_670445240101nlss.html 1 背景介绍 这是一种排序方法.假设我们对N个样方有了衡量它们之间差异即距离的数据, ...
- python 模块:csv
转自:http://www.cnblogs.com/sislcb/archive/2008/12/15/1355481.html (感谢整理) 举几个例子来介绍一下,Python 的 CSV模块的使用 ...
- python模块安装报错 :error: command 'gcc' failed with exit status 1
参考:http://blog.csdn.net/fenglifeng1987/article/details/38057193 解决方法 yum install gcc libffi-devel py ...