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] ----
ABAC
DCDE
BEBC
CADE

  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
",goal[i].c-1+'A',goal[i].x,goal[i].y);
106 
107     printf("%d
",sum);
108 
109     return 0;
110 }
原文地址:https://www.cnblogs.com/oneshot/p/4136019.html