记录编号 553840 评测结果 WWWWWWWWWWWWWW
题目名称 [USACO Feb07] 银色莲花池 最终得分 0
用户昵称 Gravatar城南花已开 是否通过 未通过
代码语言 C++ 运行时间 0.000 s
提交时间 2020-08-27 23:46:47 内存使用 0.00 MiB
显示代码纯文本
# include <iostream>
# include <cstdio>
using namespace std;
int m,n,pool1[31][31],qx[902],qy[902],sum2=0,startx,starty,lh[1500]={0};
int min2=2333333,before1[902],total1=0,sum1[902],a,b,min1=2333333,head=0,tail=1,i,j;
int heng[9]={0,1,2,2,1,-1,-2,-2,-1},shu[9]={0,2,1,-1,-2,-2,-1,1,2},step1[1500];
bool pool2[31][31];
void bfs(){
	qx[1]=startx;
	qy[1]=starty;
	sum1[1]=0;
	do{
		head++;
		pool2[qx[head]][qy[head]]=false;
		if(pool1[qx[head]][qy[head]]==4){
			total1++;
			pool2[qx[head]][qy[head]]=true;
			step1[total1]=sum1[head];
			a=head;
			while(pool1[qx[before1[a]]][qy[before1[a]]]!=3){
				if(pool1[qx[before1[a]]][qy[before1[a]]]==0){
					lh[total1]++;
				}
				b=before1[a];
				a=b;
			}
		}
		else{
			for(i=1;i<=8;i++){
				if(qx[head]+heng[i]>=1&&qx[head]+heng[i]<=m&&qy[head]+shu[i]>=1&&qy[head]+shu[i]<=n){
					if(pool2[qx[head]+heng[i]][qy[head]+shu[i]]==true){
						tail++;
						qx[tail]=qx[head]+heng[i];
						qy[tail]=qy[head]+shu[i];
						before1[tail]=head;
						sum1[tail]=sum1[head]+1;
					}
				}
			}
		}
	}while(head<tail);
}
int main(){
	freopen("silvlily.in","r",stdin);
	freopen("silvlily.out","w",stdout);
	scanf("%d%d",&m,&n);
	for(i=1;i<31;i++){
		for(int j=1;j<31;j++){
			pool2[i][j]=true;
		}
	}
	for(i=1;i<=m;i++){
		for(j=1;j<=n;j++){
			scanf("%d",&pool1[i][j]);
			if(pool1[i][j]==2){
				pool2[i][j]=false;
			}
			if(pool1[i][j]==3){
				startx=i;
				starty=j;
			}
		}
	}
	bfs();
	if(total1==0){
		printf("%d",-1);
		return 0;
    }
	for(i=1;i<=total1;i++){
		if(lh[i]<min1){
			min1=lh[i];
		}
	}
	for(i=1;i<=total1;i++){
		if(lh[i]==min1){
			if(step1[i]<min2)
			min2=step1[i];
		}
	}
	for(i=1;i<=total1;i++){
		if(lh[i]==min1&&step1[i]==min2){
			sum2++;
		}
	}
	printf("%d\n%d\n%d",min1,min2,sum2);
	fclose(stdin);
	fclose(stdout);
	return 0;
}