您的位置:首页 > 产品设计 > UI/UE

UESTC149 解救小Q

2016-07-15 17:17 465 查看
小Q被邪恶的大魔王困在了迷宫里,love8909决定去解救她。
迷宫里面有一些陷阱,一旦走到陷阱里,就会被困身亡:(,迷宫
里还有一些古老的传送阵,一旦走到传送阵上,会强制被传送到
传送阵的另一头。
现在请你帮助love8909算一算,他至少需要走多少步才能解
救到小Q?

Input

第一行为一个整数T,表示测试数据组数。
每组测试数据第一行为两个整数N,M,(1 <= N, M <= 50)表示
迷宫的长和宽。
接下来有N行,每行M个字符,是迷宫的具体描述。
'.'表示安全的位置,'#'表示陷阱,
'Q'表示小Q的位置,'L'表示love8909所在位置,
数据保证love8909只有一个,数据也保证小Q只有一个。
小写字母'a'-'z'表示分别表示不同的传送阵,数据保证传送阵
两两配对。

Output

每组数据输出一行,解救小Q所需的最少步数,如果无论如何都
无法救小Q,输出-1。

Sample Input

2

5 5
....L
.###.
b#b#a
##.##
...Qa

5 5
....L
.###.
.#.#.
##.##
...Q.

Sample Output

3
-1

注意 从当前‘a'传送到另一个’a‘的时候 只标记第一个’a'。

/* ***********************************************
Author        :guanjun
Created Time  :2016/7/15 16:11:53
File Name     :1.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 10010
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;
priority_queue<int,vector<int>,greater<int> >pq;
struct Node{
int x,y;
};
struct cmp{
bool operator()(Node a,Node b){
if(a.x==b.x) return a.y> b.y;
return a.x>b.x;
}
};
struct node{
int x,y;
int dis;
};
bool cmp(int a,int b){
return a>b;
}
int n,m;
char mp[60][60];
int sx,sy,ex,ey;
int vis[60][60];
int dir[4][2]={1,0,0,1,0,-1,-1,0};
map<pair<int,int>,pair<int,int> >mpp;
int bfs(){
cle(vis);
node u={sx,sy,0};
vis[sx][sy]=1;

queue<node>q;
q.push(u);
while(!q.empty()){
u=q.front(),q.pop();
if(u.x==ex&&u.y==ey)return u.dis;
node v;
for(int i=0;i<4;i++){
int nx=u.x+dir[i][0];
int ny=u.y+dir[i][1];
//cout<<v.x<<" "<<v.y<<endl;
if(!vis[nx][ny]&&mp[nx][ny]!='#'&&nx<=n&&nx>=1&&ny<=m&&ny>=1){
char c=mp[nx][ny];

if(c<='z'&&c>='a'){
v.x=mpp[{nx,ny}].first;
v.y=mpp[{nx,ny}].second;
v.dis=u.dis+1;
q.push(v);
}
else{
v.x=nx;v.y=ny;
v.dis=u.dis+1;
q.push(v);
}
vis[nx][ny]=1;
}
}
}
return -1;
}
vector<pair<int,int> >v[60];
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
int t;
char c;
cin>>t;
while(t--){
mpp.clear();
for(int i=0;i<=55;i++)v[i].clear();

cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>c;
if(c=='L')sx=i,sy=j;
if(c=='Q')ex=i,ey=j;
if(c<='z'&&c>='a'){
v[int(c-'a')].push_back({i,j});
}
mp[i][j]=c;
}
}
for(int i=0;i<26;i++){
if(v[i].size()==2){
mpp[{v[i][0].first,v[i][0].second}]={v[i][1].first,v[i][1].second};
mpp[{v[i][1].first,v[i][1].second}]={v[i][0].first,v[i][0].second};
}
}
cout<<bfs()<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: