bzoj:1675: [Usaco2005 feb]rigging the bovine election campaign zoning

Source: Internet
Author: User

Description

It ' s election time. The farm is partitioned to 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 Jer Seys outnumber the Holsteins. How many ways can this is done for the supplied grid?

The farms are divided into 5x5 lattices, each with a cow in it, and only Holstein (labeled H) and Gercy (labeled J) two varieties. If a cow is in any of the four squares up and down the other side, we say they are connected. the cows are going to the election. Now there is a Gercy cow who wants to choose 7 cows connected to each other and make a campaign area where they breed more cows than Stan. ask you to write a program to find out the total number of scenarios.

Input

* Lines 1..5:each of the five Lines contains five characters per line, each ' H ' or ' J '. No spaces is present.

5 lines, enter the farm situation. Output

* Line 1:the number of distinct districts of 7 connected cows such that the jerseys outnumber the Holsteins in the Distri Ct.

Total output zoning scheme.

Sample Inputhhhhh
Jhjhj
Hhhhh
Hjhhj
Hhhhh
Sample Output2
HINT

Usaco Conscience website, direct violence will not hang ... 233

The brute force enumeration point is OK, each enumeration is selected at the selected point around the line, with a few pruning.

As for the weight, direct hash is good. In the cumulative answer hash:64ms, each layer of search is hash (remove the state of repeated expansion) 20MS, playing with fire artificial two points mod:44ms, 48MS ...

Lucky #

#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;Const intMod=10001;intans=0, xx,yy,k;intHash[mod];Charc[5][5];voidDfsintXintYintHnintJnintp) {    if(x<0|| y<0|| X>4|| Y>4)return; if(x<xx| | (X==XX&AMP;&AMP;Y&LT;YY))return; if(c[x][y]=='H') hn++;Elsejn++; 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; }    Chars=C[x][y]; C[x][y]=0;  for(intI=0;i<5; i++)     for(intj=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;}intMain () { for(intI=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);}

bzoj:1675: [Usaco2005 feb]rigging the bovine election campaign zoning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.