USACO SECTION 3.3 Camelot

Camelot
IOI 98

Centuries ago, King Arthur and the Knights of the Round Table used to meet every year on New Year's Day to celebrate their fellowship. In remembrance of these events, we consider a board game for one player, on which one chesspiece king and several knight pieces are placed on squares, no two knights on the same square.

This example board is the standard 8x8 array of squares:

The King can move to any adjacent square from to as long as it does not fall off the board:

A Knight can jump from to , as long as it does not fall off the board:

During the play, the player can place more than one piece in the same square. The board squares are assumed big enough so that a piece is never an obstacle for any other piece to move freely.

The player's goal is to move the pieces so as to gather them all in the same square - in the minimal number of moves. To achieve this, he must move the pieces as prescribed above. Additionally, whenever the king and one or more knights are placed in the same square, the player may choose to move the king and one of the knights together from that point on, as a single knight, up to the final gathering point. Moving the knight together with the king counts as a single move.

Write a program to compute the minimum number of moves the player must perform to produce the gathering. The pieces can gather on any square, of course.

PROGRAM NAME: camelot

INPUT FORMAT

Line 1:Two space-separated integers: R,C, the number of rows and columns on the board. There will be no more than 26 columns and no more than 30 rows.
Line 2..end:The input file contains a sequence of space-separated letter/digit pairs, 1 or more per line. The first pair represents the board position of the king; subsequent pairs represent positions of knights. There might be 0 knights or the knights might fill the board. Rows are numbered starting at 1; columns are specified as upper case characters starting with `A'.

SAMPLE INPUT (file camelot.in)

8 8
D 4
A 3 A 8
H 1 H 8

The king is positioned at D4. There are four knights, positioned at A3, A8, H1, and H8.

OUTPUT FORMAT

A single line with the number of moves to aggregate the pieces.

SAMPLE OUTPUT (file camelot.out)

10

SAMPLE OUTPUT ELABORATION

They gather at B5.
Knight 1: A3 - B5 (1 move)
Knight 2: A8 - C7 - B5 (2 moves)
Knight 3: H1 - G3 - F5 - D4 (picking up king) - B5 (4 moves)
Knight 4: H8 - F7 - D6 - B5 (3 moves)
1 + 2 + 4 + 3 = 10 moves.

 

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

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int Rows,Columns;
int Position[26*30+1];
int Knight;
int Distance[26*30][26*30];
int King[26*30];
int Queue[900][2];   
int front,rear;
int MinPath=54000;

void Enqueue(int Pos,int Path)
{
	Queue[rear][0]=Pos;
	Queue[rear][1]=Path;
	rear=(rear+1)%900;
}


void Dequeue(int *Pos)
{
	Pos[0]=Queue[front][0];
	Pos[1]=Queue[front][1];
	front=(front+1)%900;
}

void BFS(int k)    //计算骑士从k点到所有点的需要的步数 
{
	int Pos[2];
	int TmpDistance[26*30];
	memset(TmpDistance,-1,sizeof(TmpDistance));
	Enqueue(k,0);      
	while(front!=rear){
		Dequeue(Pos);
		if(Distance[k][Pos[0]]!=-1)
				continue;
		Distance[k][Pos[0]]=Pos[1];
		if(Pos[0]>=2*Rows && Pos[0]%Rows>0)
			Enqueue(Pos[0]-2*Rows-1,Pos[1]+1);
		if(Pos[0]>=2*Rows && Pos[0]%Rows<Rows-1)
			Enqueue(Pos[0]-2*Rows+1,Pos[1]+1);
		if(Pos[0]>=Rows && Pos[0]%Rows>1)
			Enqueue(Pos[0]-Rows-2,Pos[1]+1);
		if(Pos[0]>=Rows && Pos[0]%Rows<Rows-2)
			Enqueue(Pos[0]-Rows+2,Pos[1]+1);
		if(Pos[0]<Rows*Columns-2*Rows && Pos[0]%Rows>0)
			Enqueue(Pos[0]+2*Rows-1,Pos[1]+1);
		if(Pos[0]<Rows*Columns-2*Rows && Pos[0]%Rows<Rows-1)
			Enqueue(Pos[0]+2*Rows+1,Pos[1]+1);
		if(Pos[0]<Rows*Columns-Rows && Pos[0]%Rows>1)
			Enqueue(Pos[0]+Rows-2,Pos[1]+1);
		if(Pos[0]<Rows*Columns-Rows && Pos[0]%Rows<Rows-2)
			Enqueue(Pos[0]+Rows+2,Pos[1]+1);
	}	
} 
int main()
{
	FILE *fin,*fout;
	fin=fopen("camelot.in","r");
	fout=fopen("camelot.out","w");
	memset(Distance,-1,sizeof(Distance)); 
	memset(King,-1,sizeof(King));
 	int i=0,j,k,t;
    int sum,Pos[2];
 	char x;
 	int y;
 	
	fscanf(fin,"%d %d ",&Columns,&Rows);
	while(fscanf(fin,"%c %d ",&x,&y)!=EOF)
		Position[i++]=x-'A'+(y-1)*Rows;
	Knight=i-1;

	for(i=0;i<Columns*Rows;i++)
		BFS(i);
	
	Enqueue(Position[0],0);  //计算国王到每个点的距离 
	while(front!=rear){
		Dequeue(Pos);
		if(King[Pos[0]]!=-1)
			continue;
		King[Pos[0]]=Pos[1]; 
		if(Pos[0]>=Rows)
			Enqueue(Pos[0]-Rows,Pos[1]+1);
		if(Pos[0]<Rows*Columns-Rows)
			Enqueue(Pos[0]+Rows,Pos[1]+1);
		if(Pos[0]%Rows>0)
			Enqueue(Pos[0]-1,Pos[1]+1);
		if(Pos[0]%Rows<Rows-1)
			Enqueue(Pos[0]+1,Pos[1]+1);
		if(Pos[0]>=Rows&&Pos[0]%Rows>0)
			Enqueue(Pos[0]-Rows-1,Pos[1]+1);
		if(Pos[0]>=Rows&&Pos[0]%Rows<Rows-1)
			Enqueue(Pos[0]-Rows+1,Pos[1]+1);
		if(Pos[0]<Rows*Columns-Rows&&Pos[0]%Rows>0)
			Enqueue(Pos[0]+Rows-1,Pos[1]+1);
		if(Pos[0]<Rows*Columns-Rows&&Pos[0]%Rows<Rows-1)
			Enqueue(Pos[0]+Rows+1,Pos[1]+1);
	} 
	
	for(i=0;i<Columns*Rows;i++){       //以i为集合点 
		for(sum=0,j=1;j<=Knight&&Distance[Position[j]][i]!=-1;j++){            
			sum+=Distance[Position[j]][i];
		}
		if(j<=Knight)   //有存在骑士永不可达 
			continue;
		if(MinPath>sum+King[i])        
			MinPath=sum+King[i];
		for(k=1;k<=Knight;k++){         //骑士k去接国王 
			sum-=Distance[Position[k]][i];  
			for(t=0;t<Columns*Rows;t++){   //在t点接到国王 
				if(sum>=MinPath)   /*剪枝1*/ 
					break;
				if(Distance[Position[k]][t]==-1) //判断是否可达 
					continue;
				if(sum+Distance[Position[k]][t]+King[t]+Distance[t][i]<MinPath){
					MinPath=sum+Distance[Position[k]][t]+King[t]+Distance[t][i];
				}
			}
			sum+=Distance[Position[k]][i];  	
		}
	}	
	fprintf(fout,"%d\n",MinPath);
	return 0;	
}


折腾了好久终于过了

话说这题的BFS好恶心啊。。

计算最短步数的时候有O(N*N*N)的复杂度啊

大概有2个不可达的特殊情况去掉了

还有一个第三重循环内对步数的剪枝

本来最慢0.5秒的变成了0.1秒

话说看到NOCOW上面有0.02秒的,方法和我基本一样,不信,复制粘贴下来自己提交,给跪了,1S+...不知道怎么回事

有人说这个是水题啊。。居然做的这么累。。

 


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值