牛牛走迷宫-NC21583(优化搜索)

牛牛走迷宫

题号:NC21583
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld

题目描述 

有一个矩形迷宫(1 ≤ n,m ≤ 50)
` #`表示墙 `.`表示空地
起点(r1,c1)终点(r2,c2)
每一步可以采取下面策略之一

- 花费1秒的时间走向相邻的空地
- 花费2秒的时间传送到四个基本方向最近的空地

最少需要几秒能从起点到达终点 如果不能输出-1

输入描述:

第一行输入两个整数n,m (1 ≤ n,m ≤ 50)

接下来n行每行m个字符表示迷宫

接下来一行四个整数r1,c1,r2,c2分别表示起点与终点

输出描述:

输出一个整数

示例1

输入

复制

4 4
.##.
.###
.###
....
0 0 3 3

输出

复制

4

示例2

输入

复制

7 6
......
#####.
#.###.
#####.
#.###.
#####.
#.....
0 0 6 1

输出

复制

5

备注:

30%的数据:n,m<=10

另外30%的数据:n,m<=40

另外40%的数据:n,m<=50
/*
*@Author:   GuoJinlong
*@Language: C++
*/
//#include <bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<list>
#include<set>
#include<iomanip>
#include<cstring>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<cassert>
#include<sstream>
#include<algorithm>
using namespace std;
const int mod=1e9+7;
typedef long long  ll;
#define ls (rt<<1)
#define rs (rt<<1|1)
#define mid (l+r)/2
#define mms(x, y) memset(x, y, sizeof x)
#define over(i,s,t) for(register long long i=s;i<=t;++i)
#define lver(i,t,s) for(register long long i=t;i>=s;--i)
const int MAXN = 305;
const int INF = 0x3f3f3f3f;
const int N=5e4+7;
const int maxn=1e5+5;
const double EPS=1e-10;
const double Pi=3.1415926535897;
//inline double max(double a,double b){
//    return a>b?a:b;
//}
//inline double min(double a,double b){
//    return a<b?a:b;
//}
 
int xd[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int yd[8] = {1, 0, -1, 0, -1, 1, -1, 1};
 
//void Fire(){
//    queue<node> p;
//    p.push({fx,fy,0});
//    memset(fire, -1, sizeof(fire));
//    fire[fx][fy]=0;
//    while(!p.empty()){
//        node temp=p.front();
//        p.pop();
//        for(int i=0;i<8;i++){
//            int x=temp.x+xd[i];
//            int y=temp.y+yd[i];
//            if(x<0||x>=n||y<0||y>=m||fire[x][y]!=-1){
//                continue;
//            }
//            fire[x][y]=temp.val+1;
//            p.push({x,y,temp.val+1});
//        }
//    }
//}
//int bfs(){
//    queue<node> p;
//    memset(vis, 0, sizeof(vis));
//    p.push({sx,sy,0});
//    while (!p.empty()) {
//        node temp=p.front();
//        vis[temp.x][temp.y]=1;
//        p.pop();
//        for(int i=0;i<4;i++){
//            int x=temp.x+xd[i];
//            int y=temp.y+yd[i];
//            if(x<0||x>=n||y<0||y>=m)  continue;
//            if(x==ex&&y==ey&&temp.val+1<=fire[x][y]) return temp.val+1;
//            if(vis[x][y]||temp.val+1>=fire[x][y]||a[x][y]=='#') continue;
//            p.push({x,y,temp.val+1});
//        }
//    }
//    return -1;
//}

//一维哈希
//int n;
//string s;
//int bas=131;
//typedef unsigned long long ull;
//const ull mod1=100001651;
//ull a[100010];
//ull Hash(string s){
//    ll ans=0;
//    for(int i=0;i<s.size();i++){
//        ans*=bas;
//        ans+=int(s[i]);
//        ans%=mod1;
//    }
//    return ans;
//}

//二维哈希
//using lom=unsigned long long ;
//const lom bas1=131,bas2=233;
//const int M=505;
//int n,m;
//char a[M][M];
//lom _hash[M][M];
//lom p1[M],p2[M];
//
//
//void init(){
//    p1[0]=1;
//    p2[0]=1;
//    for(int i=1;i<=505;i++){
//        p1[i]=p1[i-1]*bas1;
//        p2[i]=p2[i-1]*bas2;
//
//    }
//}
//void Hash(){
//    _hash[0][0]=_hash[0][1]=_hash[1][0]=0;
//    for(int i=1;i<=n;i++){    //前缀和
//        for(int j=1;j<=m;j++){
//            _hash[i][j]=_hash[i][j-1]*bas1+a[i][j]-'a';
//        }
//    }
//    for(int i=1;i<=n;i++){   //二维前缀和
//        for(int j=1;j<=m;j++){
//            _hash[i][j]+=_hash[i-1][j]*bas2;
//        }
//    }
//
//}


//int pre[1010];
//int in[1010];
//int post[1010];
//int k;
//struct node{
//    int value;
//    node *l,*r;
//    node (int value=0,node *l=NULL,node *r=NULL):value(value),l(l),r(r){}
//};
//void builttree(int l,int r,int &t,node * &root){
//    int flag=-1;
//    for(int i=l;i<=r;i++){
//        if(in[i]==pre[t]){
//            flag=i;
//            break;
//        }
//    }
//    if(flag==-1) return;
//    root=new node(in[flag]);
//    t++;
//    if(flag>l) builttree(l,flag-1,t,root->l);
//    if(flag<r) builttree(flag+1,r,t,root->r);
//
//}
//void preorder(node *root){
//    if(root!=NULL)
//    {
//        post[k++]=root->value;
//        preorder(root->l);
//        preorder(root->r);
//
//    }
//}
//void inorder(node *root){
//    if(root!=NULL)
//    {
//        inorder(root->l);
//        post[k++]=root->value;
//        inorder(root->r);
//
//    }
//}
//void postorder(node *root){
//    if(root!=NULL)
//    {
//        postorder(root->l);
//        postorder(root->r);
//        post[k++]=root->value;
//    }
//}

//线段树
//#define MAX 1000000
//int tree[MAX<<2];
//void pushup(int rt){
//    tree[rt]=tree[rt<<1]+tree[rt<<1|1];
//}
//void update(int rt,int i,int val,int l,int r)
//{
//    if(r==l) {
//        tree[rt]+=val;
//        return;
//    }
//    if(i<=mid){
//        update(ls,i,val,l,mid);
//    }
//    else {
//        update(rs,i,val,mid+1,r);
//    }
//    pushup(rt);
//}
//int query(int rt,int l,int r,int k){
//    if(l==r) return l;
//    if(tree[rt<<1|1]>=k)
//        return query(rs,mid+1,r,k);
//    else return query(ls,l,mid,k-tree[rt<<1|1]);
//}


//二维树状数组
//int lowbit(int x){
//    return x&(-x);
//}
//void updata(int x,int y,int c)
//{
//    for(int i=x;i<=n;i+=lowbit(i)){
//        for(int j=y;j<=n;j+=lowbit(j)){
//            sum[i][j]+=c;
//        }
//    }
//}
//int getsum(int x,int y){
//    int ans=0;
//    for(int i=x;i>0;i-=lowbit(i)){
//        for(int j=y;j>0;j-=lowbit(j)){
//            ans+=sum[i][j];
//        }
//    }
//    return ans;
//}

int n,m;
int ex,ey;
char a[110][110];
struct node {
    int x,y,t;
     friend bool operator <(const node &a,const node & b){ //优先队列的优先比较
        return a.t>b.t;
    }
};
int check(int x,int y){
    if(x<0||x>=n||y<0||y>=m) return false;
    return true;
}
int bfs(int x,int y){
    priority_queue<node>p;
    p.push({x,y,0});
    while (!p.empty()) {
        node tmp=p.top();
        p.pop();
        int xx=tmp.x;
        int yy=tmp.y;
        int t=tmp.t;
        if(xx==ex&&yy==ey){
            return t;
        }
        for(int i=0;i<4;i++){
            int x=xx+xd[i];
            int y=yy+yd[i];
            if(check(x,y)&&a[x][y]!='#'&&a[x][y]!='*'){
                a[x][y]='*'; //防止回去
                p.push({x,y,t+1});
            }
            while (check(x,y)&&a[x][y]=='#') { //沿着同一方向前进
                x+=xd[i];
                y+=yd[i];
            }
            if(check(x,y)&&a[x][y]=='.'){
                p.push({x,y,t+2});
            }
        }
    }
    return -1;
}
int main(){
    cin>>n>>m;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            cin>>a[i][j];
        }
    }
    int x,y;
    cin>>x>>y;
    cin>>ex>>ey;
    cout<<bfs(x,y);
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郭晋龙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值