码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-04-10 01:21:19      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:

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.        

 

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

标签:

原文地址:http://www.cnblogs.com/HansBug/p/4413031.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!