您的位置:首页 > 其它

HDU 1728 搜索

2015-04-22 18:24 120 查看
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*在这里并不是很实用,因为这里优先级第一的是拐弯的次数,所以步数是次之的,可能出现步数少但是拐弯次数多的情况(真的吗= = )恩。。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: