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. 1 2 3 4
  2. +-------
  3. 1|A B A C
  4. 2|D C D E
  5. 3|B E B C
  6. 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)

  1. ABAC
  2. DCDE
  3. BEBC
  4. 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)

  1. D 4 1
  2. C 4 2
  3. A 3 1
  4. A 3 3
  5. B 2 4
  6. B 3 2
  7. B 4 4
  8. E 2 1
  9. E 2 3
  10. D 1 4
  11. D 2 2
  12. C 1 1
  13. C 1 3
  14. A 1 2
  15. E 4 3
  16. D 3 4
  17. 14925
  18.  
  19. ——————————————————————————————————————————————————————————题解
    这是一道不需要任何优化的题
    然而我不断的T
    是因为我没读题……
    题目中不止说了八连块,还说了当前要放置小奶牛的块不能有同种类的大奶牛
    所以要不要放是九个块共同决定的……
    【只有一组数据点还是样例!不走心!】
  1. /*
  2. ID: ivorysi
  3. LANG: C++
  4. PROG: wissqu
  5. */
  6. #include <iostream>
  7. #include <cstdio>
  8. #include <cstring>
  9. #include <queue>
  10. #include <cmath>
  11. #include <set>
  12. #include <vector>
  13. #include <algorithm>
  14. #define siji(i,x,y) for(int i=(x);i<=(y);++i)
  15. #define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
  16. #define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
  17. #define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
  18. #define inf 0x5f5f5f5f
  19. #define ivorysi
  20. #define mo 97797977
  21. #define hash 974711
  22. #define base 47
  23. #define fi first
  24. #define se second
  25. #define pii pair<int,int>
  26. #define esp 1e-10
  27. typedef long long ll;
  28. using namespace std;
  29. char c[][];
  30. int calc[][][];
  31. int dirx[]={-,,,,,-,-,,};
  32. int diry[]={,,-,,,-,,-,};
  33. int num[];
  34.  
  35. bool used[][];
  36. char tempchange[];
  37. int row[],col[];
  38. int ans;
  39. bool flag;
  40. void init() {
  41. siji(i,,) {scanf("%s",c[i]+);}
  42. siji(i,,) {
  43. siji(j,,) {
  44. siji(z,,) {
  45. int xx=i+dirx[z],yy=j+diry[z];
  46. if(xx>= && xx<= && yy>= && yy<=) {
  47. ++calc[xx][yy][c[i][j]-'A'+];
  48. }
  49. }
  50. }
  51. }
  52. siji(i,,) num[i]=;
  53. ++num[];
  54. }
  55. void PRINT() {
  56. siji(i,,) {
  57. printf("%c %d %d\n",tempchange[i],row[i],col[i]);
  58. }
  59. }
  60. void dfs(int dep) {
  61. if(dep>) {
  62. ++ans;
  63. if(!flag) {PRINT();flag=;}
  64. return;
  65. }
  66. siji(z,,) {
  67. if(num[z]==) continue;
  68. siji(i,,) {
  69. siji(j,,){
  70. if(used[i][j]) continue;
  71. if(calc[i][j][z]==) {
  72. used[i][j]=;
  73. --num[z];
  74. siji(k,,) {
  75. int xx=i+dirx[k],yy=j+diry[k];
  76. if(xx>= && xx<= && yy>= && yy<=) {
  77. --calc[xx][yy][c[i][j]-'A'+];
  78. ++calc[xx][yy][z];
  79. }
  80. }
  81. if(!flag) {
  82. tempchange[dep]='A'+z-;
  83. row[dep]=i;
  84. col[dep]=j;
  85. }
  86.  
  87. dfs(dep+);
  88. used[i][j]=;
  89. ++num[z];
  90. siji(k,,) {
  91. int xx=i+dirx[k],yy=j+diry[k];
  92. if(xx>= && xx<= && yy>= && yy<=) {
  93. ++calc[xx][yy][c[i][j]-'A'+];
  94. --calc[xx][yy][z];
  95. }
  96. }
  97. }
  98. }
  99. }
  100. }
  101.  
  102. }
  103. void solve() {
  104. init();
  105. siji(i,,) {
  106. siji(j,,) {
  107. if(calc[i][j][]==) {
  108. used[i][j]=;
  109. --num[];
  110. siji(k,,) {
  111. int xx=i+dirx[k],yy=j+diry[k];
  112. if(xx>= && xx<= && yy>= && yy<=) {
  113. --calc[xx][yy][c[i][j]-'A'+];
  114. ++calc[xx][yy][];
  115. }
  116. }
  117. if(!flag) {
  118. tempchange[]='D';
  119. row[]=i;
  120. col[]=j;
  121. }
  122. dfs();
  123. used[i][j]=;
  124. ++num[];
  125. siji(k,,) {
  126. int xx=i+dirx[k],yy=j+diry[k];
  127. if(xx>= && xx<= && yy>= && yy<=) {
  128. ++calc[xx][yy][c[i][j]-'A'+];
  129. --calc[xx][yy][];
  130. }
  131. }
  132. }
  133. }
  134. }
  135. printf("%d\n",ans);
  136. }
  137. int main(int argc, char const *argv[])
  138. {
  139. #ifdef ivorysi
  140. freopen("wissqu.in","r",stdin);
  141. freopen("wissqu.out","w",stdout);
  142. #else
  143. freopen("f1.in","r",stdin);
  144. #endif
  145. solve();
  146. return ;
  147. }
  1.  

USACO 6.4 Wisconsin Squares的更多相关文章

  1. USACO 3.2 Magic Squares

    Magic SquaresIOI'96 Following the success of the magic cube, Mr. Rubik invented its planar version, ...

  2. USACO Section1.2 Palindromic Squares 解题报告

    palsquare解题报告 —— icedream61 博客园(转载请注明出处)------------------------------------------------------------ ...

  3. USACO 1.2 Palindromic Squares (进制转换,回文)

    /* ID:twd30651 PROG:palsquare LANG:C++ */ #include<iostream> #include<fstream> #include& ...

  4. [hash-bfs]USACO 3.2 Magic Squares 魔板

    魔 板 魔板 魔板 题目描述 在成功地发明了魔方之后,拉比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 5 我们知道魔板的每一个方格都有一种颜色 ...

  5. USACO 6.4 章节

    The Primes 题目大意 5*5矩阵,给定左上角 要所有行,列,从左向右看对角线为质数,没有前导零,且这些质数数位和相等(题目给和) 按字典序输出所有方案... 题解 看上去就是个 无脑暴搜 题 ...

  6. USACO6.4-Wisconsin Squares:搜索

    Wisconsin Squares It's spring in Wisconsin and time to move the yearling calves to the yearling past ...

  7. USACO 完结的一些感想

    其实日期没有那么近啦……只是我偶尔还点进去造成的,导致我没有每一章刷完的纪念日了 但是全刷完是今天啦 讲真,题很锻炼思维能力,USACO保持着一贯猎奇的题目描述,以及尽量不用高级算法就完成的题解……例 ...

  8. 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 ...

  9. 【USACO 3.2】Magic Squares

    题意 4*2个格子分别为 1234 8765 的魔板有3种操作,A:上下两排互换,B:最后一列放到第一列前面,C:中间四个顺时针旋转1格. 现在给出目标状态,找出最少步数可从原始状态到达目标状态,且输 ...

随机推荐

  1. Java基础-初识面向对象编程(Object-Oriented-Programming)

    Java基础-初识面向对象编程(Object-Oriented-Programming) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. Java是一门面向对象的程序设计语言.那么什 ...

  2. java基础-System类常用方法介绍

    java基础-System类常用方法介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.System类概念 在API中system类介绍的比较简单,我们给出定义,system中 ...

  3. 转:苹果Xcode帮助文档阅读指南

    一直想写这么一个东西,长期以来我发现很多初学者的问题在于不掌握学习的方法,所以,Xcode那么好的SDK文档摆在那里,对他们也起不到什么太大的作用.从论坛.微博等等地方看到的初学者提出的问题,也暴露出 ...

  4. 科学计算三维可视化---Mlab基础(数据可视化)

    推文:科学计算三维可视化---TVTK库可视化实例 使用相关函数:科学计算三维可视化---Mlab基础(管线控制函数) 一:mlab.pipeline中标量数据可视化 通过持续实例,来感受mlab对数 ...

  5. nandflash,norflash,sdram,emmc,rom,ram等各种存储器识别

    老是被nandflash,norflash,sdram,emmc,rom,ram搞混,所以在这里总结一下,也为了更好的分清他们之间的关系,以至于别人问的时候不至于说不清. 我们不谈这些名次的由来,只说 ...

  6. 高效使用jquery之一:请使用'On'函数

    on函数是在jquery 1.7 加入的 描述: 在选定的元素上绑定一个或多个事件处理函数. 定义:.on( events [, selector ] [, data ], handler(event ...

  7. 20155336 2016-2017-2《JAVA程序设计》第五周学习总结

    20155336 2016-2017-2<JAVA程序设计>第五周学习总结 教材学习内容总结 第八章 语法与继承构架 使用try.catch 特点: 使用try.catch语法,JVM会尝 ...

  8. Collections -- 集合的工具类

    Collections是JDK针对集合提供的一个工具类,他提供一系列静态方法实现对各种集合的搜索.排序.线程安全化等操作. 1.搜索 如可以使用Collections提供的二分查找方法binarySe ...

  9. Eclipse中如何改变主题

    童鞋们, eclipse主题太丑?想设置护眼的主题? 看看这些主题: 请移驾: Eclipse Color Themes 怎么设设置? 1. 打开”eclipse marketplace“, 如下图: ...

  10. Eclipse中如何调整字体

    Eclipse 字体有两处,一处是控制台的字体,一处是主窗口.这里分别介绍控制台和主窗口字体的调节方法. Window -> Preferences -> General -> Ap ...