USACO6.4-Wisconsin Squares:搜索
2014-12-01 21:24
369 查看
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.
One more line with the total number of ways these arrangements can be created.
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 in lexicographic 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
这题估计是第6章里面最水的题了,可是我还是调了好久,因为没有注意到移进来的字母不能和原来的相同,一直TLE。。。T.T 然后直接暴搜,什么剪枝都不用加,测试数据竟然就是样例,而且只有一组。。。
Executing... Test 1: TEST OK [0.921 secs, 3372 KB] All tests OK. YOUR PROGRAM ('wissqu') WORKED FIRST TIME! That's fantastic -- and a rare thing. Please accept these special automated congratulations. Here are the test data inputs: ------- test 1 [length 20 bytes] ---- ABAC DCDE BEBC CADE
/* LANG:C++ TASK:wissqu */ #include <iostream> #include <memory.h> #include <stdio.h> using namespace std; class CC { public: int c; // 要移进来的字母 int x,y; }; int G[8][8]= {0}; // 'A'用1表示 int sum=0; int Left[10]= {-1,3,3,3,4,3}; bool vis[8][8]= {0}; CC goal[20],load[20]; const int dx[]= {-1,-1,-1, 1, 1, 1, 0, 0, 0}; const int dy[]= {-1, 0, 1,-1, 0, 1,-1, 1, 0}; // 判断(x,y)是否符合 bool check(int x,int y,int c) { for(int i=0; i<9; i++) { int newx=x+dx[i]; int newy=y+dy[i]; if(G[newx][newy]==c) return false; } return true; } void dfs(int cnt=0,int c=4) { if(cnt>=16) { sum++; // 记录方案 if(sum==1) { memcpy(goal,load,sizeof load); } return ; } for(int x=1; x<=4; x++) for(int y=1; y<=4; y++) { if(!vis[x][y] && check(x,y,c)) { int tmp=G[x][y]; G[x][y]=c; vis[x][y]=true; Left[c]--; load[cnt].x=x; load[cnt].y=y; load[cnt].c=c; if(cnt==15) dfs(cnt+1,0); else for(int i=1; i<=5; i++) { if(Left[i]) { dfs(cnt+1,i); } } G[x][y]=tmp; vis[x][y]=false; Left[c]++; } } } int main() { freopen("wissqu.in","r",stdin); freopen("wissqu.out","w",stdout); for(int i=1; i<=4; i++) { for(int j=1; j<=4; j++) { int x=getchar()-'A'+1; G[i][j]=x; } getchar(); } dfs(); for(int i=0; i<16; i++) printf("%c %d %d\n",goal[i].c-1+'A',goal[i].x,goal[i].y); printf("%d\n",sum); return 0; }
相关文章推荐
- USACO-Section1.3 Wormholes [搜索]
- 1751: [Usaco2005 qua]Lake Counting (搜索)
- USACO 4.2 cowcycle 搜索 DFS
- 【USACO】Mother's Milk(搜索)
- BZOJ 1603 [Usaco2008 Oct]打谷机 暴力搜索
- USACO 1.3 Wormholes - 搜索
- 【BZOJ】1770 [Usaco2009 Nov]lights 燈 折半搜索
- POJ - 3258/USACO - Dec06 Silver/NOIP 2015 - 提高组 River Hopscotch 贪心+二分搜索
- Fence Rails USACO 4.1(继续阵亡,DFSID+二分搜索+剩余记录)
- BZOJ 3391: [Usaco2004 Dec]Tree Cutting网络破坏(搜索)
- 【搜索】【USACO】Checker Challenge
- 【搜索】【USACO】Feed Ratios
- hdu 4277 USACO ORZ 搜索+set判重
- BZOJ 3891 USACO 2014 Dec Piggy Back 搜索 最短路
- USACO-Section2.1 Hamming Codes [搜索]
- 【搜索】【usaco 2.1.1】castle
- usaco 4.1.2 Fence Rails 搜索剪枝
- USACO / Checker Challenge(位运算的搜索)
- usaco 4.3 Letter Game 搜索
- 搜索【洛谷P2845】 [USACO15DEC]Switching on the Lights 开关灯