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

 /*
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[][]= {}; // 'A'用1表示
int sum=;
int Left[]= {-,,,,,};
bool vis[][]= {}; CC goal[],load[]; const int dx[]= {-,-,-, , , , , , };
const int dy[]= {-, , ,-, , ,-, , };
// 判断(x,y)是否符合
bool check(int x,int y,int c)
{
for(int i=; i<; i++)
{
int newx=x+dx[i];
int newy=y+dy[i];
if(G[newx][newy]==c)
return false;
}
return true;
} void dfs(int cnt=,int c=)
{
if(cnt>=)
{
sum++;
// 记录方案
if(sum==)
{
memcpy(goal,load,sizeof load);
}
return ;
} for(int x=; x<=; x++)
for(int y=; y<=; 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==)
dfs(cnt+,);
else
for(int i=; i<=; i++)
{
if(Left[i])
{
dfs(cnt+,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=; i<=; i++)
{
for(int j=; j<=; j++)
{
int x=getchar()-'A'+;
G[i][j]=x;
}
getchar();
} dfs(); for(int i=; i<; i++)
printf("%c %d %d\n",goal[i].c-+'A',goal[i].x,goal[i].y); printf("%d\n",sum); return ;
}

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

  1. USACO 6.4 Wisconsin Squares

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

  2. P2730 魔板 Magic Squares (搜索)

    题目链接 Solution 这道题,我是用 \(map\) 做的. 具体实现,我们用一个 \(string\) 类型表示任意一种情况. 可以知道,排列最多只有 \(8!\) 个. 然后就是直接的广搜了 ...

  3. USACO 6.4 章节

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

  4. USACO 完结的一些感想

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

  5. Leetcode之广度优先搜索(BFS)专题-279. 完全平方数(Perfect Squares)

    Leetcode之广度优先搜索(BFS)专题-279. 完全平方数(Perfect Squares) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ar ...

  6. Instantaneous Transference--POJ3592Tarjan缩点+搜索

    Instantaneous Transference Time Limit: 5000MS Memory Limit: 65536K Description It was long ago when ...

  7. A Knight's Journey 分类: POJ 搜索 2015-08-08 07:32 2人阅读 评论(0) 收藏

    A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 35564 Accepted: 12119 ...

  8. CodeForces 173C Spiral Maximum 记忆化搜索 滚动数组优化

    Spiral Maximum 题目连接: http://codeforces.com/problemset/problem/173/C Description Let's consider a k × ...

  9. Educational Codeforces Round 1 E. Chocolate Bar 记忆化搜索

    E. Chocolate Bar Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/598/prob ...

随机推荐

  1. 高仿“点触验证码”做的一个静态Html例子

    先上源码: <html> <head> <title>TouClick - Designed By MrChu</title> <meta htt ...

  2. Java 编程的动态性,第 5 部分: 动态转换类--转载

    在第 4 部分“ 用 Javassist 进行类转换”中,您学习了如何使用 Javassist 框架来转换编译器生成的 Java 类文件,同时写回修改过的类文件.这种类文件转换步骤对于做出持久变更是很 ...

  3. [转] Java内部类之闭包(closure)与回调(callback)

    闭包(closure)是一个可调用的对象,它记录了一些信息,这些信息来自于创建它的作用域.通过这个定义,可以看出内部类是面向对象的闭包,因为它 不仅包含外围类对象(创建内部类的作用域)的信息,还自动拥 ...

  4. NYOJ-1070诡异的电梯【Ⅰ】

    这道题是个dp,主要考虑两种情况,刚开始我把状态转移方程写成了dp[i] = min(dp[i-1] + a, dp[i + 1] +b); 后来想想当推到dp[i]的时候,那个dp[i + 1]还没 ...

  5. 10、第十节课jq420151012

    1.点击交替显示隐藏功能  点击交替执行的:fadeToggle(1000) , slideToggle() ,  toggle(1000);      2.点击单独执行    单独显示/隐藏:sho ...

  6. css ie hack整理

    网上有很多关于ie hack的文章,可能由于文章发布后ie的版本还在升级.所以导致有些hack写法已经不适用了.以下是本人整理的ie6-11的一些hack常用写法.(以下默认文档模式为标准模式) 1. ...

  7. start with connect by prior学习

    这是oracle中的树查询,查询出来的数据会根据上下级组成树的结构.select * from mw_sys.mwt_pd_deps start with obj_id = '63EBEC8E-E76 ...

  8. IOS开发,如何用最新的Itunes给手机装ipa文件

    搜了半天,发现网上的很多教“使用Itunes给手机装ipa文件”文字都好几年了,现在的Itunes跟过去几年的功能和界面都有很大改变,话不多说直接两句话讲明白,希望帮到需要的人. 使用Itunes给手 ...

  9. zepto源码研究 - zepto.js - 1

    简要:网上已经有很多人已经将zepto的源码研究得很细致了,但我还是想写下zepto源码系列,将别人的东西和自己的想法写下来以加深印象也是自娱自乐,文章中可能有许多错误,望有人不吝指出,烦请赐教. 首 ...

  10. VS2010 C++ 优化配置

    个人感觉VC6.0太土了,而且有很多bug存在,且微软早就不对其更新.所以,在选择C++编程的时候.使用IDE,VC6.0一段时间以后,我毅然决然的放弃了,觉得还是使用VS2010比较有前途. 但是当 ...