TZC 2939解救小Q

#include <iostream>
#include <cstring>
#include<vector>
#include<queue>
using namespace std;
int m,n,h[4][2]={1,0,-1,0,0,1,0,-1},bz[55][55];
char map[55][55];
struct point
{
	int x,y,step;
}s,e;
vector<int> aa[28];
queue<point> my;
int BFS(point s)
{   int x,y,i,a,b;
    point t,tt;
	while(!my.empty()) my.pop();
	my.push(s);
	bz[s.x][s.y]=1;
	while(!my.empty())
    {
    	t=my.front();
    	my.pop();
    	for(i=0;i<4;i++)
    	{   x=t.x+h[i][0];y=t.y+h[i][1];
	    	if(x==e.x&&y==e.y) return t.step+1;
 	        if(x>=0&&x<m&&y>=0&&y<n&&bz[x][y]==0&&map[x][y]!='#')
 	        {   bz[x][y]=1;
 	          if(map[x][y]!='.') 
	    	   { 
 		        if(aa[map[x][y]-'a'][0]==x&&aa[map[x][y]-'a'][1]==y)
					 {tt.x=aa[map[x][y]-'a'][2]; tt.y=aa[map[x][y]-'a'][3];}
			    else {tt.x=aa[map[x][y]-'a'][0]; tt.y=aa[map[x][y]-'a'][1];}
				 tt.step=t.step+1;
   	    	   }
   	    	   else
        	    {tt.x=x; tt.y=y; tt.step=t.step+1;}
				my.push(tt);	
   	        }
	    }
    }
    return -1;
}
int main(void)
{
   int t,i,j,k,x,y;
   point p;
   cin>>t;
   while(t--)
   { memset(bz,0,sizeof(bz));
   	  cin>>m>>n;
   	  for(i=0;i<m;i++)
   	  for(j=0;j<n;j++)
   	    {cin>>map[i][j];
   	       if(map[i][j]>='a'&&map[i][j]<='z')
           { 
		   aa[map[i][j]-'a'].push_back(i);
		   aa[map[i][j]-'a'].push_back(j);
   		   }
		   if(map[i][j]=='L') { s.x=i; s.y=j; s.step=0; }
		   if(map[i][j]=='Q') { e.x=i; e.y=j; e.step=0; }
   	    }
   	    cout<<BFS(s)<<endl;
   	    for(i=0;i<28;i++) aa[i].clear();    
   } 
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值