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

1675: [Usaco2005 Feb]Rigging the Bovine Election 竞选划区

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 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 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

Source

Silver

题解:一开始这道题我看了半天硬是没敢做,首先想到的是状压DP,可是状压虽然25格应该能压得下,但是怎么判断连续?所以一时束手无策

直到看到了某位网上的神犇说——大力出奇迹!!!orzorzorz

然后我就按照那样子来了一发,结果居然能A!!!神奇QAQ

实际上就是\( O\left( {25}^{7} \right) \)的朴素暴力,实在没想到这都能AC,于是有种瞬间被彻底吓尿的感觉QAQ

但是很明显有很大优化的空间,于是本人打算明天再来一发强化版的么么哒

 1 /**************************************************************
 2     Problem: 1675
 3     User: HansBug
 4     Language: Pascal
 5     Result: Accepted
 6     Time:1860 ms
 7     Memory:228 kb
 8 ****************************************************************/
 9
10 const
11      mx:array[1..4] of longint=(0,1,0,-1);
12      my:array[1..4] of longint=(1,0,-1,0);
13 var
14    i,j,k,l,m,n,ans,i1,i2,i3,i4,i5,i6,i7:longint;
15    ch:char;
16    map:array[0..6,0..6] of longint;
17    s:array[0..8] of longint;
18 function check:boolean;
19          var
20             i,j,sx,sy,xx,yy,sum,t,w:longint;
21             nx,ny:array[0..8] of longint;
22             q:array[0..10] of longint;
23             d:array[0..6,0..6] of longint;
24          begin
25               sum:=0;t:=0;w:=1;
26               fillchar(nx,sizeof(nx),0);
27               fillchar(ny,sizeof(ny),0);
28               fillchar(q,sizeof(q),0);
29               fillchar(d,sizeof(d),0);
30               for i:=1 to 7 do
31                   begin
32                        ny[i]:=s[i] mod 5;
33                        nx[i]:=(s[i] div 5)+1;
34                        if ny[i]=0 then
35                           begin
36                                ny[i]:=5;
37                                dec(nx[i]);
38                           end;
39                        d[nx[i],ny[i]]:=i;
40                   end;
41               q[1]:=1;d[nx[1],ny[1]]:=0;sum:=map[nx[1],ny[1]];
42               while t<w do
43                     begin
44                          inc(t);
45                          xx:=nx[q[t]];
46                          yy:=ny[q[t]];
47                          for k:=1 to 4 do
48                              begin
49                                   sx:=xx+mx[k];
50                                   sy:=yy+my[k];
51                                   if (sx<1) or (sy<1) or (sy>5) or (sy>5) or (d[sx,sy]=0) then continue;
52                                   inc(w);
53                                   q[w]:=d[sx,sy];
54                                   d[sx,sy]:=0;
55                                   inc(sum,map[sx,sy]);
56                              end;
57                     end;
58               exit((w=7) and (sum>3));
59          end;
60 begin
61      fillchar(map,sizeof(map),0);
62      for i:=1 to 5 do
63          for j:=1 to 5 do
64              begin
65                   read(ch);
66                   if ch=‘J‘ then map[i,j]:=1 else map[i,j]:=0;
67                   if j=5 then readln;
68              end;
69      ans:=0;
70      for i1:=1 to 19 do
71          for i2:=i1+1 to 20 do
72              for i3:=i2+1 to 21 do
73                  for i4:=i3+1 to 22 do
74                      for i5:=i4+1 to 23 do
75                          for i6:=i5+1 to 24 do
76                              for i7:=i6+1 to 25 do
77                                  begin
78                                       s[1]:=i1;s[2]:=i2;s[3]:=i3;
79                                       s[4]:=i4;s[5]:=i5;s[6]:=i6;s[7]:=i7;
80                                       if check then inc(ans);
81                                  end;
82      writeln(ans);
83      readln;
84 end.        
时间: 2024-10-20 23:57:18

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: 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: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 l

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

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的标号.由于这些行星都不是十分发达.没有流通的货币,所以

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

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

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

1734: [Usaco2005 feb]Aggressive cows 愤怒的牛 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 217  Solved: 175[Submit][Status][Discuss] Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are located along a st

[bzoj1733][Usaco2005 feb]Secret Milking Machine 神秘的挤奶机_网络流

[Usaco2005 feb]Secret Milking Machine 神秘的挤奶机 题目大意:约翰正在制造一台新型的挤奶机,但他不希望别人知道.他希望尽可能久地隐藏这个秘密.他把挤奶机藏在他的农场里,使它不被发现.在挤奶机制造的过程中,他需要去挤奶机所在的地方T(1≤T≤200)次.他的农场里有秘密的地道,但约翰只在返回的时候用它.农场被划分成N(2≤N≤200)块区域,用1到200标号.这些区域被P(1≤P≤40000)条道路连接,每条路有一个小于10^6的长度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,