TYVJ-1074 武士风度的牛 BFS

调试了一会,发现程序没错,测试数据错了——读入的还是上一题的数据==||||

还是那句老话——“同学,要细心!”

简单BFS。

/*
 * tyvj-1074 武士风度的牛
 * mike-w
 * 2011-10-26
 * ---------------------
 * BFS
 */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define SIZE 155
#define QSIZE 50000
#define SUBMIT

typedef struct _pos
{
	int x,y;
}pos;

char f[SIZE][SIZE];
int g[SIZE][SIZE];
pos q[QSIZE];
int head,tail,len;
int row,col;
int xx1,xx2,yy1,yy2;
int d[8][2]={
	{-1,-2},	{1,-2}, {2,-1}, {2,1},
	{1,2}, {-1,2},	{-2,1}, {-2,-1}
};

int push(int x,int y)
{
	q[tail].x=x;
	q[tail].y=y;
	if(++tail==QSIZE)
		tail=0;
	len++;
	return 0;
}

int pop(int *x, int *y)
{
	*x=q[head].x;
	*y=q[head].y;
	if(++head==QSIZE)
		head=0;
	len--;
	return 0;
}

#ifdef DISP
int disp(void)
{
	int i,j;
	for(i=0;i<row;i++)
		for(j=0;j<col;j++)
			printf("%d%c",g[i][j],(j+1==col)?'\n':' ');
	return 0;
}
#endif

int solve(void)
{
	int x,y,xx,yy,i;
	push(xx1,yy1);
	g[xx1][yy1]=1;
	while(len>0)
	{
		pop(&x,&y);
		for(i=0;i<8;i++)
		{
			xx=x+d[i][0];
			yy=y+d[i][1];
			if(xx>=0 && xx<row && yy>=0 && yy<col && f[xx][yy]=='.' && !g[xx][yy])
			{
				push(xx,yy);
				g[xx][yy]=g[x][y]+1;
				if(xx==xx2 && yy==yy2)
				{
					len=0;
					break;
				}
			}
		}
#ifdef DISP
		disp();
#endif
	}
	return g[xx2][yy2]-1;
}

int main(void)
{
	int i,j;
#ifndef SUBMIT
	freopen("in","r",stdin);
#endif
	scanf("%d%d",&col,&row);
	for(i=0;i<row;i++)
		scanf("%s",f[i]);
	
	for(i=0;i<row;i++)
		for(j=0;j<col;j++)
		{
			if(f[i][j]=='H')
				xx1=i,yy1=j;
			else if(f[i][j]=='K')
				xx2=i,yy2=j;
		}
	f[xx2][yy2]='.';
	printf("%d\n",solve());
	return 0;
}


 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值