hdu 1175连连看

来源:互联网 发布:怎么申请不了淘宝主播 编辑:程序博客网 时间:2024/06/07 14:35

Accepted

#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
int s[1002][1002];
struct node
{
    int x;
    int y;
    int num;
    int ff;
    bool operator < (const node &a) const
    {
        return num>a.num;
    }
};
int dir[4][2]={1,0,0,1,0,-1,-1,0};
int vis[1002][1002];
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF&&n+m)
    {
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
            {
                scanf("%d",&s[i][j]);
            }
        int t;
        scanf("%d",&t);
        while(t--)
        {
            node a,b;
            scanf("%d%d%d%d",&a.x,&a.y,&b.x,&b.y);
            if((s[a.x][a.y]!=s[b.x][b.y])||s[a.x][a.y]==0||s[b.x][b.y]==0)
            {
                printf("NO\n");
                continue;
            }
            memset(vis, 0, sizeof(vis));
            int flag=0;
            a.num=-1;
            a.ff=-1;
            vis[a.x][a.y]=1;
            priority_queue<node> q;
            while(!q.empty())
            {
                q.pop();
            }
            q.push(a);
            while(!q.empty())
            {
                node c;
                c=q.top();
                q.pop();
                for(int i=0;i<4;i++)
                {
                    node d;
                    d.x=c.x+dir[i][0];
                    d.y=c.y+dir[i][1];
                    if(c.ff!=i)//判断是否转弯,
                    {
                        d.ff=i;
                        d.num=c.num+1;
                    }
                    else
                    {
                        d.ff=c.ff;
                        d.num=c.num;
                    }
                    if(d.num>2)
                    {
                        continue;
                    }
                    if((s[d.x][d.y]==0||(s[d.x][d.y]==s[b.x][b.y]&&d.x==b.x&&d.y==b.y))&&d.x>=1&&d.x<=n&&d.y>=1&&d.y<=m&&!vis[d.x][d.y])
                    {
                        vis[d.x][d.y]=1;
                        if(d.x==b.x&&d.y==b.y&&d.num<=2)
                        {
                            flag=1;
                            break;
                        }
                        q.push(d);
                    }
                }
                if(flag)
                {
                    break;
                }
            }
            flag?printf("YES\n"):printf("NO\n");
        }
    }
}

Memory Limit Exceeded

#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
int map[1010][1010], n, m;
int dir[4][2]={1,0,0,1,0,-1,-1,0};
int vis[1010][1010], x1, y1, x2, y2, i;
struct node
{
    int x, y, sum, step;
    friend bool operator <(node k, node t)
    {
        return k.sum > t.sum;
    }
};
int judge(int a, int b, int c, int d)
{


    if(map[a][b]!=map[c][d] || map[a][b]!=0 || map[c][d]!=0)
    {
        printf("NO\n");
    }
    else
    {
        priority_queue<node>q;
        node now, next;
        while(!q.empty())
        {
            q.pop();
        }
        int flag=0;
        now.x = a;
        now.y = b;
        now.step = -1;
        now.sum = -1;
        vis[a][b] = 1;
        q.push(now);
        while(!q.empty())
        {
            now = q.top();
            q.pop();
            for(i=0; i<4; i++)
            {
                next.x = now.x + dir[i][0];
                next.y = now.y + dir[i][1];
                if(next.x>=1 && next.x<=n && next.y>=1 && next.y<=m && !vis[next.x][next.y] && (map[next.x][next.y]==0 || (next.x==c && next.y==d)))
                {
                    if(now.step!=i)//判断是否转弯
                    {
                        next.step=i;
                        next.sum = now.sum + 1;
                    }
                    else
                    {
                        next.step = now.step;
                        next.sum = now.sum;
                    }
                    if(next.sum>2)
                        continue;
                    if(next.x==a && next.y==b && next.sum<=2)
                    {
                        flag = 1;
                        break;
                    }
                    vis[next.x][next.y] = 1;
                }
                q.push(next);
            }
            if(flag)
            {
                break;
            }
        }
        flag?printf("YES\n"):printf("NO\n");
    }
}


int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(n==0 && m==0)
            break;
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=m; j++)
            {
                scanf("%d",&map[i][j]);
            }
        }
        int q;
        scanf("%d",&q);
        while(q--)
        {
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            int ans;
            memset(vis,0,sizeof(vis));
            judge(x1,y1,x2,y2);
        }
    }
}





0 0