usaco 5.2 Wisconsin Squares(DFS)
2012-09-24 15:43
786 查看
Wisconsin Squares
It's spring in Wisconsin and time to move the yearling calves to the yearling pasture and last year's yearlings to the greener pastures of the north 40.
Farmer John has five kinds of cows on his farm (abbreviations are shown in parentheses): Guernseys (A), Jerseys (B), Herefords (C), Black Angus (D), and Longhorns (E). These herds are arranged on the 16 acre pasture,
one acre for each small herd, on a 4 x 4 grid (labeled with rows and columns) like this:
In the initial pasture layout, the herds total 3 A's, 3 B's, 4 C's, 3 D's, and 3 E's. This year's calves have one more D herd and one fewer C herd, for a total of 3 A's, 3 B's, 3 C's, 4 D's, and 3 E's.
FJ is extremely careful in his placement of herds onto his pasture grid. This is because when herds of the same types of cows are too close together, they misbehave: they gather near the fence and smoke cigarettes
and drink milk. Herds are too close together when they are on the same square or in any of the eight adjacent squares.
Farmer John must move his old herd out of the field and his new herd into the field using his old brown Ford pickup truck, which holds one small herd at a time. He picks up a new herd, drives to a square in the
yearling pasture, unloads the new herd, loads up the old herd, and drives the old herd to the north 40 where he unloads it. He repeats this operation 16 times and then drives to Zack's for low-fat yogurt treats and familiar wall decor.
Help Farmer John. He must choose just exactly the correct order to replace the herds so that he never puts a new herd in a square currently occupied by the same type of herd or adjacent to a square occupied by the
same type of herd. Of course, once the old cows are gone and the new cows are in place, he must be careful in the future to separate herds based on the new arrangement.
Very important hint: Farmer John knows from past experience that he must move a herd of D cows first.
Find a way for Farmer John to move the yearlings to their new pasture. Print the 16 sequential herd-type/row/column movements that lead to a safe moving experience for the cows.
Calculate the total number of possible final arrangements for the 4x4 pasture and calculate the total number of ways those arrangements can be created.
lexicographic order.
One more line with the total number of ways these arrangements can be created.
题意:给你一个4*4的矩阵,里面放了一些字母A~E,现在有新的不同数量的字母要替换旧的,总数也是16,有一个规则就是任何时候相同字母都不能相邻,包括被替换的字母
分析:随手写了DFS,没有任何优化,没想到居然可以过,不过这题就一组数据,也就是样例,0.7s 应该算还行吧。。。
代码:
It's spring in Wisconsin and time to move the yearling calves to the yearling pasture and last year's yearlings to the greener pastures of the north 40.
Farmer John has five kinds of cows on his farm (abbreviations are shown in parentheses): Guernseys (A), Jerseys (B), Herefords (C), Black Angus (D), and Longhorns (E). These herds are arranged on the 16 acre pasture,
one acre for each small herd, on a 4 x 4 grid (labeled with rows and columns) like this:
1 2 3 4 +------- 1|A B A C 2|D C D E 3|B E B C 4|C A D E
In the initial pasture layout, the herds total 3 A's, 3 B's, 4 C's, 3 D's, and 3 E's. This year's calves have one more D herd and one fewer C herd, for a total of 3 A's, 3 B's, 3 C's, 4 D's, and 3 E's.
FJ is extremely careful in his placement of herds onto his pasture grid. This is because when herds of the same types of cows are too close together, they misbehave: they gather near the fence and smoke cigarettes
and drink milk. Herds are too close together when they are on the same square or in any of the eight adjacent squares.
Farmer John must move his old herd out of the field and his new herd into the field using his old brown Ford pickup truck, which holds one small herd at a time. He picks up a new herd, drives to a square in the
yearling pasture, unloads the new herd, loads up the old herd, and drives the old herd to the north 40 where he unloads it. He repeats this operation 16 times and then drives to Zack's for low-fat yogurt treats and familiar wall decor.
Help Farmer John. He must choose just exactly the correct order to replace the herds so that he never puts a new herd in a square currently occupied by the same type of herd or adjacent to a square occupied by the
same type of herd. Of course, once the old cows are gone and the new cows are in place, he must be careful in the future to separate herds based on the new arrangement.
Very important hint: Farmer John knows from past experience that he must move a herd of D cows first.
Find a way for Farmer John to move the yearlings to their new pasture. Print the 16 sequential herd-type/row/column movements that lead to a safe moving experience for the cows.
Calculate the total number of possible final arrangements for the 4x4 pasture and calculate the total number of ways those arrangements can be created.
PROGRAM NAME: wissqu
TIME LIMIT: 5 seconds
INPUT FORMAT
Four lines, each with four letters that denote herds.SAMPLE INPUT (file wissqu.in)
ABAC DCDE BEBC CADE
OUTPUT FORMAT
16 lines, each with a herd-type, row and column. If there are multiple solutions (and there are), you should output the solution for which the concatenated string ("D41C42A31 ... D34") of the answers is first inlexicographic order.
One more line with the total number of ways these arrangements can be created.
SAMPLE OUTPUT (file wissqu.out)
D 4 1 C 4 2 A 3 1 A 3 3 B 2 4 B 3 2 B 4 4 E 2 1 E 2 3 D 1 4 D 2 2 C 1 1 C 1 3 A 1 2 E 4 3 D 3 4 14925
题意:给你一个4*4的矩阵,里面放了一些字母A~E,现在有新的不同数量的字母要替换旧的,总数也是16,有一个规则就是任何时候相同字母都不能相邻,包括被替换的字母
分析:随手写了DFS,没有任何优化,没想到居然可以过,不过这题就一组数据,也就是样例,0.7s 应该算还行吧。。。
代码:
/* ID: 15114582 PROG: wissqu LANG: C++ */ #include<cstdio> #include<cstring> #include<iostream> using namespace std; int dx[]={0,0,0,-1,1,1,1,-1,-1}; int dy[]={0,-1,1,0,0,-1,1,-1,1}; char map[9][9],q[55],out[55]; int s[9][9][5],v[9][9],num[5],ans; void CowMov(int x,int y,int c,int v) { for(int i=0;i<9;++i) s[x+dx[i]][y+dy[i]][c]+=v; } void CowSwp(int x,int y,int c1,int c2) { CowMov(x,y,c1,-1); CowMov(x,y,c2,1); } void dfs(int t) { if(t>=16) { q[t*3]='\0'; if(!ans||strcmp(q,out)<0)strcpy(out,q); ++ans; return; } for(int k=0,i,j;k<5;++k) if(num[k]) { --num[k]; for(i=1;i<5;++i) for(j=1;j<5;++j) if(!v[i][j]&&!s[i][j][k]) { CowSwp(i,j,map[i][j-1]-'A',k); q[t*3]='A'+k,q[t*3+1]=i,q[t*3+2]=j; v[i][j]=1; dfs(t+1); v[i][j]=0; CowSwp(i,j,k,map[i][j-1]-'A'); } ++num[k]; } } int main() { freopen("wissqu.in","r",stdin); freopen("wissqu.out","w",stdout); int i,j; memset(s,0,sizeof(s)); memset(v,0,sizeof(v)); for(i=1;i<5;++i) { scanf("%s",map[i]); for(j=0;j<4;++j)CowMov(i,j+1,map[i][j]-'A',1); } for(ans=i=0;i<5;++i)num[i]=3; for(i=1;i<5;++i) for(j=1;j<5;++j) if(!s[i][j][3]) { CowSwp(i,j,map[i][j-1]-'A',3); q[0]='D',q[1]=i,q[2]=j; v[i][j]=1; dfs(1); v[i][j]=0; CowSwp(i,j,3,map[i][j-1]-'A'); } for(i=0;i<16;++i) printf("%c %d %d\n",out[i*3],out[i*3+1],out[i*3+2]); printf("%d\n",ans); return 0; }
相关文章推荐
- USACO Section 5.2 Wisconsin Squares - 按要求DFS就行了..
- USACO 5.2 Snail Trails(DFS)
- Snail Trail_usaco 5.2_dfs
- usaco 5.2 Snail Trails(DFS)
- USACO 5.2 Wisconsin Squares(DFS)
- USACO Superprime Rib(dfs)
- usaco顺序的分数(随便搞,排序可以)和健康的赫斯坦奶牛(dfs)(水)
- Usaco 2.3 Zero Sums(回溯DFS)--暴搜
- Riding the Fences USACO 3.3 欧拉路径(dfs+floodfill)
- Fence Rails USACO 4.1(继续阵亡,DFSID+二分搜索+剩余记录)
- bzoj 1673: [Usaco2005 Dec]Scales 天平【dfs】
- bzoj 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐(暴力DFS)
- USACO section 2.4 Overfencing(dfs+bfs)
- [USACO1.4]母亲的牛奶Mother's Milk dfs
- USACO-Section 2.1 The Castle(DFS)
- usaco snail trails(dfs)
- bzoj 1621: [Usaco2008 Open]Roads Around The Farm分岔路口【dfs】
- USACO Healthy Holsteins DFS
- USACO section 2.4 Overfencing(dfs+bfs)
- USACO 1.5 Checker Challenge(DFS)