USACO6.4-Wisconsin Squares:搜索

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:

              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] ----ABACDCDEBEBCCADE

  1 /*
  2 LANG:C++
  3 TASK:wissqu
  4 */
  5 #include <iostream>
  6 #include <memory.h>
  7 #include <stdio.h>
  8 using namespace std;
  9
 10 class CC
 11 {
 12 public:
 13     int c; // 要移进来的字母
 14     int x,y;
 15 };
 16
 17 int G[8][8]= {0}; // ‘A‘用1表示
 18 int sum=0;
 19 int Left[10]= {-1,3,3,3,4,3};
 20 bool vis[8][8]= {0};
 21
 22 CC goal[20],load[20];
 23
 24
 25 const int dx[]= {-1,-1,-1, 1, 1, 1, 0, 0, 0};
 26 const int dy[]= {-1, 0, 1,-1, 0, 1,-1, 1, 0};
 27 // 判断(x,y)是否符合
 28 bool check(int x,int y,int c)
 29 {
 30     for(int i=0; i<9; i++)
 31     {
 32         int newx=x+dx[i];
 33         int newy=y+dy[i];
 34         if(G[newx][newy]==c)
 35             return false;
 36     }
 37     return true;
 38 }
 39
 40
 41 void dfs(int cnt=0,int c=4)
 42 {
 43     if(cnt>=16)
 44     {
 45         sum++;
 46         // 记录方案
 47         if(sum==1)
 48         {
 49             memcpy(goal,load,sizeof load);
 50         }
 51         return ;
 52     }
 53
 54
 55
 56     for(int x=1; x<=4; x++)
 57         for(int y=1; y<=4; y++)
 58         {
 59             if(!vis[x][y] && check(x,y,c))
 60             {
 61                 int tmp=G[x][y];
 62                 G[x][y]=c;
 63                 vis[x][y]=true;
 64                 Left[c]--;
 65                 load[cnt].x=x;
 66                 load[cnt].y=y;
 67                 load[cnt].c=c;
 68
 69                 if(cnt==15)
 70                     dfs(cnt+1,0);
 71                 else
 72                     for(int i=1; i<=5; i++)
 73                     {
 74                         if(Left[i])
 75                         {
 76                             dfs(cnt+1,i);
 77                         }
 78                     }
 79
 80                 G[x][y]=tmp;
 81                 vis[x][y]=false;
 82                 Left[c]++;
 83             }
 84         }
 85 }
 86
 87 int main()
 88 {
 89     freopen("wissqu.in","r",stdin);
 90     freopen("wissqu.out","w",stdout);
 91
 92     for(int i=1; i<=4; i++)
 93     {
 94         for(int j=1; j<=4; j++)
 95         {
 96             int x=getchar()-‘A‘+1;
 97             G[i][j]=x;
 98         }
 99         getchar();
100     }
101
102     dfs();
103
104     for(int i=0; i<16; i++)
105         printf("%c %d %d\n",goal[i].c-1+‘A‘,goal[i].x,goal[i].y);
106
107     printf("%d\n",sum);
108
109     return 0;
110 }
时间: 2025-01-01 15:55:40

USACO6.4-Wisconsin Squares:搜索的相关文章

USACO 6.4 Wisconsin Squares

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 pare

[USACO 6.4.3]Wisconsin Squares

题解 暴搜即可. 代码 /* TASK:wissqu LANG:C++ */ #include <cstdio> #include <cstring> using namespace std; char matrix[8][8], path[16][10]; bool v[4][4], flag; int ans, num[5] = {3, 3, 3, 4, 3}; bool judge(int x0, int y0, int x1, int y1, int k) { for (i

USACO3.2--Magic Squares+经典搜索

类似于八数码的一道经典搜索题,思路基本也一样.我是用康拓展开进行的判重. 代码如下: /* ID: 15674811 LANG: C++ TASK: msquare */ #include<iostream> #include<cstdio> #include<cstring> #include<queue> #include<algorithm> using namespace std; #define maxn 100000 typedef

USACO 6.5 All Latin Squares

All Latin Squares A square arrangement of numbers 1 2 3 4 5 2 1 4 5 3 3 4 5 1 2 4 5 2 3 1 5 3 1 2 4 is a 5 x 5 Latin Square because each whole number from 1 to 5 appears once and only once in each row and column. Write a program that will compute the

poj2676--Sudoku(搜索练习5-数独游戏)

Sudoku Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Appoint description:  System Crawler  (2013-01-21) Description Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 sm

hdu 1198 Farm Irrigation (搜索或并查集)

Farm Irrigation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5818    Accepted Submission(s): 2521 Problem Description Benny has a spacious farm land to irrigate. The farm land is a rectangle

pku 2488 A Knight&#39;s Journey (搜索 DFS)

A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28697   Accepted: 9822 Description Background The knight is getting bored of seeing the same black and white squares again and again and has decided to make a journey aro

C - 搜索

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Description BackgroundThe knight is getting bored of seeing the same black and white squares again and again and has decided to make a journey around the world

poj3009——迭代加深搜索

POJ 3009  迭代加深搜索 Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12986   Accepted: 5460 Description On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules are somewhat different from o