USACO SECTION 5.2 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

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

 

/*
ID: conicoc1
LANG: C
TASK: wissqu
*/

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define Min(a,b) a<b?a:b
#define Max(a,b) a<b?b:a

char Pasture[6][6];
int Flag[6][6];
int Herd[5]={3,3,3,4,3};
int Ans=0;
int Path[17][3];


int CanPos(char Cow,int i,int j)
{
	if( Pasture[i][j]==Cow||
		Pasture[i+1][j]==Cow||
		Pasture[i-1][j]==Cow||
		Pasture[i][j+1]==Cow||
		Pasture[i][j-1]==Cow||
		Pasture[i+1][j+1]==Cow||
		Pasture[i+1][j-1]==Cow||
		Pasture[i-1][j+1]==Cow||
		Pasture[i-1][j-1]==Cow)
			return 0;
	else	
		return 1;	
}

void DFS(int CowNum)
{
	char Cow,Temp;
	int i,j,k;
	if(CowNum==17){
		Ans++;
		return;
	}
	for(i=0;i<5;i++){
		if(CowNum==1)
			Cow='D';
		else
			Cow='A'+i;
		if(Herd[Cow-'A']!=0)	
			Herd[Cow-'A']--;
		else
			continue;
		for(j=1;j<=4;j++){
			for(k=1;k<=4;k++){
				if(!Flag[j][k]&&CanPos(Cow,j,k)){
					Flag[j][k]=1;
					Temp=Pasture[j][k];
					Pasture[j][k]=Cow;
					if(Ans==0){
						Path[CowNum][0]=Cow;
						Path[CowNum][1]=j;
						Path[CowNum][2]=k;
					}
					DFS(CowNum+1);
					Flag[j][k]=0;
					Pasture[j][k]=Temp;
				}
			}
		}
		Herd[Cow-'A']++;
		if(CowNum==1)
			break;
	}
}

int main()
{
	int i;
	FILE *fin,*fout;
	fin=fopen("wissqu.in","r");
	fout=fopen("wissqu.out","w");
	memset(Flag,0,sizeof(Flag));
	for(i=1;i<=4;i++)
		fscanf(fin,"%s",Pasture[i]+1);
	DFS(1);
	for(i=1;i<=16;i++)
		fprintf(fout,"%c %d %d\n",Path[i][0],Path[i][1],Path[i][2]);
	fprintf(fout,"%d\n",Ans);
	return 0;
}


 

水题并不可怕。。可怕的是只有一个测试数据。。。


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值