【BZOJ】1675: [Usaco2005 Feb]Rigging the Bovine Election 竞选划区(暴力dfs+set判重)
http://www.lydsy.com/JudgeOnline/problem.php?id=1675
一开始我写了个枚举7个点。。。。。。。
但是貌似。。。
写挫了。
然后我就写dfs。。
判重好难写啊。
。。。。
本来用hash的。。
但是对拍一直wa。。
所以干脆用set。。
然后将数值调大。。
然后就过了。。
然后bzoj数据弱。。
自己对拍还是hash有冲突的。。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) for1(aaa, 1, n) { for1(bbb, 1, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int md=1000000007;
int mp[7][7], vis[7][7], ans;
set<int> hs;
const int dx[]={-1, 1, 0, 0}, dy[]={0, 0, -1, 1};
int Hash() {
int ret=0, k=7;
for1(i, 1, 5) for1(j, 1, 5) if(vis[i][j]) {
ret=(ret+(((mp[i][j]*k)%md)*(i*5+j)%md))%md;
k=(k*7)%md;
}
return ret;
}
bool check(int x, int y) {
rep(i, 4) if(vis[x+dx[i]][y+dy[i]]) return 1;
return 0;
}
void dfs(int now, int a, int b) {
if(now==8) {
if(a>b) {
int h=Hash();
if(hs.count(h)==0) { hs.insert(h); ++ans; }
}
return;
}
for1(i, 1, 5) for1(j, 1, 5) if(!vis[i][j] && (check(i, j) || now==1)) {
vis[i][j]=1;
dfs(now+1, a+(mp[i][j]==31), b+(mp[i][j]==43));
vis[i][j]=0;
}
} int main() {
for1(i, 1, 5) for1(j, 1, 5) {
char ch; for(ch=getchar(); !(ch=='H'||ch=='J'); ch=getchar());
if(ch=='J') mp[i][j]=31;
else mp[i][j]=43;
}
dfs(1, 0, 0);
print(ans);
return 0;
}
后边写的枚举每个点
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) for1(aaa, 1, n) { for1(bbb, 1, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } int mp[7][7], ans, xy[10], x[10], y[10], vis[7][7], vv[7][7], cnt;
const int dx[]={-1, 1, 0, 0}, dy[]={0, 0, -1, 1};
void dfs(int x, int y) {
vv[x][y]=1;
++cnt;
rep(i, 4) {
int fx=dx[i]+x, fy=dy[i]+y;
if(vis[fx][fy] && !vv[fx][fy]) dfs(fx, fy);
}
}
bool check() {
for1(i, 1, 7) x[i]=(xy[i]+4)/5, y[i]=xy[i]-(x[i]-1)*5;
CC(vis, 0); CC(vv, 0);
for1(i, 1, 7) vis[x[i]][y[i]]=1;
cnt=0;
dfs(x[1], y[1]);
if(cnt!=7) return 0;
int sum=0;
for1(i, 1, 7) sum+=mp[x[i]][y[i]];
return sum>3;
}
int main() {
for1(i, 1, 5) for1(j, 1, 5) {
char ch; for(ch=getchar(); ch!='H'&&ch!='J'; ch=getchar());
mp[i][j]=ch=='J';
}
for(xy[1]=1; xy[1]<=19; ++xy[1])
for(xy[2]=xy[1]+1; xy[2]<=20; ++xy[2])
for(xy[3]=xy[2]+1; xy[3]<=21; ++xy[3])
for(xy[4]=xy[3]+1; xy[4]<=22; ++xy[4])
for(xy[5]=xy[4]+1; xy[5]<=23; ++xy[5])
for(xy[6]=xy[5]+1; xy[6]<=24; ++xy[6])
for(xy[7]=xy[6]+1; xy[7]<=25; ++xy[7])
if(check()) ++ans;
print(ans);
return 0;
}
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.
Output
* Line 1: The number of distinct districts of 7 connected cows such that the Jerseys outnumber the Holsteins in the district.
Sample Input
JHJHJ
HHHHH
HJHHJ
HHHHH
Sample Output
HINT
Source
【BZOJ】1675: [Usaco2005 Feb]Rigging the Bovine Election 竞选划区(暴力dfs+set判重)的更多相关文章
- 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 ...
- 1675: [Usaco2005 Feb]Rigging the Bovine Election 竞选划区(题解第二弹)
1675: [Usaco2005 Feb]Rigging the Bovine Election 竞选划区 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: ...
- 1675: [Usaco2005 Feb]Rigging the Bovine Election 竞选划区(题解第一弹)
1675: [Usaco2005 Feb]Rigging the Bovine Election 竞选划区 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: ...
- bzoj1675 [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 ...
- BZOJ 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛( 二分答案 )
最小最大...又是经典的二分答案做法.. -------------------------------------------------------------------------- #inc ...
- bzoj 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛
1734: [Usaco2005 feb]Aggressive cows 愤怒的牛 Description Farmer John has built a new long barn, with N ...
- [BZOJ 1733] [Usaco2005 feb] Secret Milking Machine 【二分 + 最大流】
题目链接:BZOJ - 1733 题目分析 直接二分这个最大边的边权,然后用最大流判断是否可以有 T 的流量. 代码 #include <iostream> #include <cs ...
- BZOJ 1733: [Usaco2005 feb]Secret Milking Machine 神秘的挤奶机
Description 约翰正在制造一台新型的挤奶机,但他不希望别人知道.他希望尽可能久地隐藏这个秘密.他把挤奶机藏在他的农场里,使它不被发现.在挤奶机制造的过程中,他需要去挤奶机所在的地方T(1≤T ...
- bzoj:3392: [Usaco2005 Feb]Part Acquisition 交易
Description 奶牛们接到了寻找一种新型挤奶机的任务,为此它们准备依次经过N(1≤N≤50000)颗行星,在行星上进行交易.为了方便,奶牛们已经给可能出现的K(1≤K≤1000)种货物 ...
随机推荐
- 专业版Unity技巧分享:使用定制资源配置文件 ScriptableObject
http://unity3d.9tech.cn/news/2014/0116/39639.html 通常,在游戏的开发过程中,最终会建立起一些组件,通过某种形式的配置文件接收一些数据.这些可能是程序级 ...
- 【Espruino】NO.17 使用平板电脑调试Espruino(OTG方式)
http://blog.csdn.net/qwert1213131/article/details/38068379 本文属于个人理解,能力有限,纰漏在所难免,还望指正! [小鱼有点电] [Espru ...
- POJ 3468 A Simple Problem with Integers 【线段树,区间更新】
题意:你有N个整数,A1,A2,-,一个.你须要处理两种类型的操作.一种类型的操作是加入了一些给定的数字,每一个数字在一个给定的时间间隔. 还有一种是在给定的时间间隔要求数量的总和. 难点:主要是la ...
- 多线程之 ThreadStart 和 ParameterizedThreadStart 委托
先看微软如何给出的方法使用,如下查看,我们发现,如下两个委托,分别对应带参数创建线程 和 不带参数创建线程. 下列 委托 方法不带参数 ThreadStart namespace System.Thr ...
- Linux命令-目录处理命令:mkdir
mkdir /tmp/beijing mkdir -p /tmp/shijiazhuang/yuhuaqu 一条命令可以同时创建父目录和子目录 mkdir /tmp/beijing/chaoyangq ...
- SEH, SAFESEH相关
SEH, SAFESEH相关 1,触发seh异常让目标程序Read/Write无效地址,如果和栈底相邻的内存只读,尝试覆盖超出栈底 2,如何找到(显示)要覆盖的SEHod语法:dd fs:[0]sof ...
- js方式实现页面加遮罩效果
有时候在页面上执行查询的时候由于数据量很大,需要较长时间,所以就需要在等待结果期间不可以操作页面,那么可以使用如下代码给页面添加遮罩效果: $.messager.progress({ title: ' ...
- obj 格式注意事项
用Adreno Profiler分析图形效果的实现过程时,需要将特效涉及到的模型导出,以便进行多角度的详细查看,结果发现Adreno Profiler导出模型的功能有bug,总是报错并生成一个残缺的. ...
- IIS攻击与安全加固实例分析
IIS作为一款流行的Web服务器,在当今互联网环境中占有很大的比重,绝大多数的asp.asp.net网站都运行在它上面.因此,也引来了无数 黑客们关注的目光.目前针对IIS的攻击技术已经非常成熟,而且 ...
- QSettings 使用实例 当需要在程序关闭时保存”状态“信息
用户对应用程序经常有这样的要求:要求它能记住它的settings,比如窗口大小,位置,一些别的设置,还有一个经常用的,就是recent files,等等这些都可以通过Qsettings来实现. 我们知 ...