hdu 1728

/*
   bfs 自己写的过程中出现了不少问题
   注意: 起点和终点可能是同一点
    
*/

#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
#include <iostream>
using namespace std;

#define N 110

char map[N][N];

bool vis[N][N];

int dir[4][2]={1,0, -1,0, 0,-1, 0,1};

struct point
{
	int x,y;
	int  step;       
};

int k,x1,y1,x2,y2;

int n,m;

bool check(int x,int y)
{
	if(x>=1 && x<=m && y>=1 && y<=n && map[x][y]=='.' )
		return 1;
	return 0;     
}

int bfs()
{
    queue<point> q;
    point start,cur;
    
    start.x=x1;
    start.y=y1;
    start.step=-1;
    
    vis[x1][y1]=1;
    q.push(start);  //////  找了半天错误 
    
    while(!q.empty())
    {
        cur=q.front();
        q.pop();
      	int x=cur.x;
		int y=cur.y;
		
        for(int i=0;i<4;i++)
        {
			int xx=x + dir[i][0];
			int yy=y + dir[i][1];
			
			while(check(xx,yy))
			{
				if(!vis[xx][yy])
				{
					start.x=xx;
					start.y=yy;
					start.step=cur.step+1;
					
					q.push(start);
					
					vis[xx][yy]=1;
					
					if(xx==x2 && yy==y2 && start.step<=k)
					{
						   
                        return 1;     
					}
                  
				}    
				
				xx=xx + dir[i][0];
				yy=yy + dir[i][1];         
			}
        }
    }
	return 0;   
  
}


int main()
{
	int t;
	scanf("%d",&t);
	
	while(t--)
	{
		scanf("%d%d",&m,&n);
		
		for(int i=1;i<=m;i++)
			for(int j=1;j<=n;j++)
				cin>>map[i][j];

		//	for( i=1;i<=m;i++)
		//	{
		//		for(int j=1;j<=n;j++)
		//			printf("%c",map[i][j]);
		//		printf("\n");
		//	}
			
			
			scanf("%d%d%d%d%d",&k,&y1,&x1,&y2,&x2);  
			
			memset(vis,0,sizeof(vis));  
	   
		    if(x1==x2 && y1==y2)
		    {
                      printf("yes\n");  
             continue;           
            }
		
			 if(bfs())
			  printf("yes\n");   
			  else 
			   printf("no\n");
			
	}
	return 0;    
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值