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.

5行,输入农场的情况.

Output

* Line 1: The number of distinct districts of 7 connected cows such that the Jerseys outnumber the Holsteins in the district.

输出划区方案总数.

Sample Input

HHHHH
JHJHJ
HHHHH
HJHHJ
HHHHH

Sample Output

2

HINT

usaco良心网站,直接暴力不会挂……233

暴力枚举点就行了,每次枚举就在已经选到的点周围选就行咯,随手带几个剪枝。

至于去重,直接hash就好。在累计答案处hash:64MS,每一层搜索都hash(去掉重复扩展的状态)20MS,玩火人工二分MOD:44MS、48MS……

运气#1

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int MOD=10001;
int ans=0,xx,yy,k;
int hash[MOD];
char c[5][5];
void dfs(int x,int y,int hn,int jn,int p){
    if (x<0||y<0||x>4||y>4) return;
    if (x<xx||(x==xx&&y<yy)) return;
    if (c[x][y]==‘H‘) hn++;else jn++;
    if (hn>3) return;
    p+=1<<((x*5+y));
    k=p%MOD;
    while(hash[k]!=-1){
        if (hash[k]==p) return;
        k++;
        if (k>=MOD) k-=MOD;
    }
    hash[k]=p;
    if (hn+jn==7){
        ans++;
        return;
    }
    char s=c[x][y];
    c[x][y]=0;
    for (int i=0;i<5;i++)
    for (int j=0;j<5;j++)
    if (c[i][j]==0){
        if (c[i+1][j]!=0) dfs(i+1,j,hn,jn,p);
        if (c[i-1][j]!=0) dfs(i-1,j,hn,jn,p);
        if (c[i][j+1]!=0) dfs(i,j+1,hn,jn,p);
        if (c[i][j-1]!=0) dfs(i,j-1,hn,jn,p);
    }
    c[x][y]=s;
}
int main(){
    for (int i=0;i<5;i++) scanf("%s",c[i]);
    memset(hash,-1,sizeof(hash));
    for (xx=0;xx<5;xx++)
    for (yy=0;yy<5;yy++) dfs(xx,yy,0,0,0);
    printf("%d\n",ans);
}
时间: 2024-10-13 00:05:41

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: 196  Solved: 116[Submit][Status][Discuss] Description It's election time. The farm is partitioned into a 5x5 grid of cow locations, each of which hold

1675: [Usaco2005 Feb]Rigging the Bovine Election 竞选划区(题解第二弹)

1675: [Usaco2005 Feb]Rigging the Bovine Election 竞选划区 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 198  Solved: 118[Submit][Status][Discuss] Description It's election time. The farm is partitioned into a 5x5 grid of cow locations, each of which hold

BZOJ 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛( 二分答案 )

最小最大...又是经典的二分答案做法.. -------------------------------------------------------------------------- #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #define rep( i , n ) for( int i = 0 ; i < n ; ++i ) #defin

bzoj 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛

1734: [Usaco2005 feb]Aggressive cows 愤怒的牛 Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are located along a straight line at positions x1,...,xN (0 <= xi <= 1,000,000,000). His C (2 <= C &l

[BZOJ 1733] [Usaco2005 feb] Secret Milking Machine 【二分 + 最大流】

题目链接:BZOJ - 1733 题目分析 直接二分这个最大边的边权,然后用最大流判断是否可以有 T 的流量. 代码 #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; const int MaxN = 200 + 5,

【差分】bzoj 1676 [Usaco2005 Feb]Feed Accounting 饲料计算

题目的建模意思是什么呢? 每个奶牛从a点开始吃,从b+1点就停止吃.这就是间接告诉你这两点组成一个区间,需要差分序列. 之后对差分的序列求前缀和(计算每天的粮草的消耗量), 之后对于消耗的粮草,我们倒着从第D天往第1天加和,如果刚好相符就输出. #include <cstdio> #include <algorithm> #include <cstring> int n,d,Start,End; int Sum[2333]; int x,y; int main(){ s

BZOJ 1733: [Usaco2005 feb]Secret Milking Machine 神秘的挤奶机

Description 约翰正在制造一台新型的挤奶机,但他不希望别人知道.他希望尽可能久地隐藏这个秘密.他把挤奶机藏在他的农场里,使它不被发现.在挤奶机制造的过程中,他需要去挤奶机所在的地方T(1≤T≤200)次.他的农场里有秘密的地道,但约翰只在返回的时候用它.农场被划分成N(2≤N≤200)块区域,用1到200标号.这些区域被P(1≤P≤40000)条道路连接,每条路有一个小于10^6的长度L.两块区域之间可能有多条道路连接.为了减少被发现的可能,约翰不会两次经过农场上的任何一条道路.当然了

BZOJ3392: [Usaco2005 Feb]Part Acquisition 交易

3392: [Usaco2005 Feb]Part Acquisition 交易 Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 26  Solved: 18[Submit][Status] Description 奶牛们接到了寻找一种新型挤奶机的任务,为此它们准备依次经过N(1≤N≤50000)颗行星,在行星上进行交易.为了方便,奶牛们已经给可能出现的K(1≤K≤1000)种货物进行了由1到K的标号.由于这些行星都不是十分发达.没有流通的货币,所以

bzoj1676[Usaco2005 Feb]Feed Accounting 饲料计算*

bzoj1676[Usaco2005 Feb]Feed Accounting 饲料计算 题意: 知道草料到来时F1kg,第D天F2kg.同时知道每头牛到来时间和离开时间,一牛一天吃1kg草料,问草料到来是第几天. 题解: 直接用区间左端点对应数组元素++,右端点+1对应数组元素--的方法,最后扫一下即可. 代码: 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #define inc(i