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

暴力出奇迹啊……

直接7个for枚举位置判可行性也能过啊……

我服了

#include<cstdio>
const int mx[4]={0,1,0,-1};
const int my[4]={1,0,-1,0};
bool map[6][6];
int d[6][6];
int s[8];
int nx[8],ny[8];
int q[10];
int ans;
inline bool mark()
{
int i,j,sx,sy,xx,yy,sum=0,t=0,w=1;
for (i=1;i<=5;i++)for(j=1;j<=5;j++)d[i][j]=0;
for (i=1;i<=7;i++)
{
ny[i]=s[i]%5;nx[i]=s[i]/5;
if (ny[i])nx[i]++;
if (!ny[i])ny[i]=5;
d[nx[i]][ny[i]]=i;
}
q[1]=1;d[nx[1]][ny[1]]=0;sum=map[nx[1]][ny[1]];
while (t<w)
{
xx=nx[q[++t]];
yy=ny[q[t]];
for (int k=0;k<4;k++)
{
sx=xx+mx[k];sy=yy+my[k];
if (sx>0&&sy>0&&sx<6&&sy<6&&d[sx][sy])
{
q[++w]=d[sx][sy];
d[sx][sy]=0;
sum+=map[sx][sy];
}
}
}
return w==7&&sum>3;
}
int main()
{
for (int i=1;i<=5;i++)
for (int j=1;j<=5;j++)
{
char ch=getchar();
while (ch!='H'&&ch!='J')ch=getchar();
if (ch=='J')map[i][j]=1;
}
for (s[1]=1;s[1]<=19;s[1]++)
for (s[2]=s[1]+1;s[2]<=20;s[2]++)
for (s[3]=s[2]+1;s[3]<=21;s[3]++)
for (s[4]=s[3]+1;s[4]<=22;s[4]++)
for (s[5]=s[4]+1;s[5]<=23;s[5]++)
for (s[6]=s[5]+1;s[6]<=24;s[6]++)
for (s[7]=s[6]+1;s[7]<=25;s[7]++)
if (mark())ans++;
printf("%d",ans);
}

  

bzoj1675 [Usaco2005 Feb]Rigging the Bovine Election 竞选划区的更多相关文章

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

    1675: [Usaco2005 Feb]Rigging the Bovine Election 竞选划区 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit:  ...

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

    1675: [Usaco2005 Feb]Rigging the Bovine Election 竞选划区 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit:  ...

  3. 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 wh ...

  4. 【BZOJ】1675: [Usaco2005 Feb]Rigging the Bovine Election 竞选划区(暴力dfs+set判重)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1675 一开始我写了个枚举7个点....... 但是貌似... 写挫了. 然后我就写dfs.. 判重好 ...

  5. 问题 L: 「Usaco2005 Feb」竞选划区O(∩_∩)O 纯属的暴力

    题目描述 农场被划分为5x5的格子,每个格子中都有一头奶牛,并且只有荷斯坦(标记为H)和杰尔西(标记为J)两个品种. 如果一头奶牛在另一头上下左右四个格子中的任一格里,我们说它们相连. 奶牛要大选了. ...

  6. BZOJ3392: [Usaco2005 Feb]Part Acquisition 交易

    3392: [Usaco2005 Feb]Part Acquisition 交易 Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 26  Solved:  ...

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

    最小最大...又是经典的二分答案做法.. -------------------------------------------------------------------------- #inc ...

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

    1734: [Usaco2005 feb]Aggressive cows 愤怒的牛 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 217  Solved: ...

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

    1734: [Usaco2005 feb]Aggressive cows 愤怒的牛 Description Farmer John has built a new long barn, with N ...

随机推荐

  1. 通过实现Comparable接口结合TreeSet来对对象自动排序

    经过会遇到这样的情况,对于某个对象数组或者链表要按照一定的规则进行排序,那么我们该怎么做呢? 如遇到这样的需求: 1.需求1 对于学生对象按照年龄进行排序,年龄小的排在前面. 单单看到这样的需求,实现 ...

  2. How to run OFBiz as a Service on linux

    Windows See this specific guide: How to Run OFBiz as Windows Service with Java Service Wrapper Linux ...

  3. 恢复Linux下被误删除的文件(笔记)

    恢复Linux下被误删除的文件 [root@xuegod63 ~]# mount /dev/cdrom /mnt/ 分一个区:sda4  查找:extundelete 分一个区:sda4  [root ...

  4. java时间格式转换

    package org.shineway.com; import java.text.ParseException; import java.text.SimpleDateFormat; import ...

  5. CREATE PROCEDURE

    1 CREATE PROCEDURE(创建) CREATE PROCEDURE存储过程名(參数列表) BEGIN SQL语句代码块 END 注意: 由括号包围的參数列必须总是存在.假设没有參数,也该使 ...

  6. poj 1523 SPF(tarjan求割点)

    本文出自   http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...

  7. UIImageView圆角,自适应图片宽高比例,图片拉伸,缩放比例和图片缩微图

    /*      设置圆角,通过layer中的cornerRadius和masksToBounds即可.            自适应图片宽高比例.通过UIViewContentModeScaleAsp ...

  8. Hacker(七)----黑客常用术语和DOS命令

    掌握基本的黑客术语和DOS命令是一名黑客最基本的技能,黑客术语能够实现自己和其他人之间的正常交流.DOS命令就是DOS操作系统的命令,它是一种面向磁盘的操作命令.黑客在入侵目标主机的过程中经常会使用这 ...

  9. css_day5

  10. Android--------Java接口回调

    >###回调说明 回调说明:         1.class A,class B         2.class A 实现接口callback         3.class B拥有一个参数为c ...