HDU 1728 搜索


MLE.......本来是想拿水题找找自信的,,,结果,,,,QAQ!!!!!!!!!

#include <stdio.h>
#include <iostream>
#include <queue>
#include <string.h>
using namespace std;
#define maxn 110
char map[maxn][maxn];
int vis[maxn][maxn];
int cmpx[4]={0,0,1,-1};
int cmpy[4]={-1,1,0,0};
int n,m;
class node
{
public:
    int x,y,k,pos;
    node(){k=pos=0;}
};
int check(int xx,int yy)
{
    if(xx<0||yy<0||xx>=m||yy>=n) return 0;
    if(map[xx][yy]=='*'||vis[xx][yy]) return 0;
    return 1;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&m,&n);
        int i;
        memset(vis,0,sizeof(vis));
        for(i=0;i<m;i++)
        {
            scanf("%s",map[i]);//printf("%s\n",map[i]);
        }
        int turn,sty,stx,enx,eny;
        scanf("%d%d%d%d%d",&turn,&sty,&stx,&eny,&enx);
        sty--,stx--,eny--,enx--;
        node init;init.x=stx,init.y=sty;init.k=-1,init.pos=-1;
        queue<node>que;
        que.push(init);
        int flag=0;
        while(!que.empty())
        {
            node pre=que.front();
            que.pop();
            vis[pre.x][pre.y]=1;
            if(pre.x==enx&&pre.y==eny) {flag=1;break;}
            for(i=0;i<4;i++)
            {
                node tmp=pre;
                tmp.x+=cmpx[i],tmp.y+=cmpy[i];
                if(check(tmp.x,tmp.y))
                {
                    if(i!=pre.pos) tmp.k++;
                    tmp.pos=i;
                    
                    if(tmp.k>turn) continue;
                    que.push(tmp);
                }
            }
        }
        if(flag) cout<<"yes"<<endl;else cout<<"no"<<endl;
    }
    return 0;
}

AC代码:

#include <stdio.h>
#include <iostream>
#include <queue>
#include <string.h>
using namespace std;
#define maxn 110
char map[maxn][maxn];
int vis[maxn][maxn];
int cmpx[4]={0,0,1,-1};
int cmpy[4]={-1,1,0,0};
int n,m;
class node
{
public:
    int x,y,k,pos;
    node(){k=pos=0;}
};
int check(int xx,int yy)
{
    if(xx<0||yy<0||xx>=m||yy>=n) return 0;
    if(map[xx][yy]=='*') return 0;
    return 1;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&m,&n);
        int i;
        memset(vis,-1,sizeof(vis));
        for(i=0;i<m;i++)
        {
            scanf("%s",map[i]);//printf("%s\n",map[i]);
        }
        int turn,sty,stx,enx,eny;
        scanf("%d%d%d%d%d",&turn,&sty,&stx,&eny,&enx);
        sty--,stx--,eny--,enx--;
        node init;init.x=stx,init.y=sty;init.k=-1,init.pos=-1;
        vis[init.x][init.y]=0;
        queue<node>que;
        que.push(init);
        int flag=0;
        while(!que.empty())
        {
            node pre=que.front();
            que.pop();
            if(pre.x==enx&&pre.y==eny) {flag=1;break;}
            for(i=0;i<4;i++)
            {
                node tmp=pre;
                tmp.x+=cmpx[i],tmp.y+=cmpy[i];
                if(check(tmp.x,tmp.y))
                {
                    if(i!=pre.pos) tmp.k=pre.k+1;
                    tmp.pos=i;
                    if(tmp.k>turn) continue;
                    if(vis[tmp.x][tmp.y]==-1) vis[tmp.x][tmp.y]=tmp.k;
                    else
                    {
                        if(vis[tmp.x][tmp.y]>=tmp.k) vis[tmp.x][tmp.y]=tmp.k;  //就因为一个等于号!!!!!!坑了我好久!!!!
                        else continue;
                    }
                    que.push(tmp);
                }
            }
        }
        if(flag) cout<<"yes"<<endl;else cout<<"no"<<endl;
    }
    return 0;
}

刚开始做的时候超时了,本来想用A*做,但是发现还是超时了,,也许A*在这里并不是很实用,因为这里优先级第一的是拐弯的次数,所以步数是次之的,可能出现步数少但是拐弯次数多的情况(真的吗= = )恩。。。。




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值