zoukankan      html  css  js  c++  java
  • hdu 1175 连连看

    不知道为什么不用flag直接返回bfs的结果会错,被坑了好久。

    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <set>
    #include <map>
    #include <string>
    #include <math.h>
    #include <stdlib.h>
    using namespace std;
    
    struct node
    {
        int x,y,s,d;
    } t,t0;
    
    int n,m,mp[1024][1024],vis[1024][1024];
    int x,y,x2,y2,flag;
    int dx[4]= {0,0,1,-1},dy[4]= {1,-1,0,0};
    
    void bfs()
    {
        queue<node>q;
        if(x==x2&&y==y2) return ;
        if(mp[x][y]==0||mp[x2][y2]==0) return ;
        if(mp[x][y]!=mp[x2][y2]) return ;
        memset(vis,5,sizeof(vis));
        t.x=x;
        t.y=y;
        t.s=0;
        t.d=-1;
        q.push(t);
        while(!q.empty())
        {
            t0=q.front();
            //printf("%d %d %d
    ",t0.x,t0.y,t0.s);
            q.pop();
            if(t0.x==x2&&t0.y==y2&&t0.s<=2) {flag=1;return ;}
            for(int i=0; i<4; i++)
            {
                t=t0;
                t.x+=dx[i];
                t.y+=dy[i];
                if(t.x<1||t.x>n||t.y<1||t.y>m) continue;
                if(mp[t.x][t.y]==0||(t.x==x2&&t.y==y2))
                {
                    t.d=i;
                    if(i!=t0.d&&-1!=t0.d) t.s++;
                    if(t.s>=3) continue;
                    if(t.s<vis[t.x][t.y])
                    {
                        q.push(t);
                        vis[t.x][t.y]=t.s;
                    }
                }
            }
        }
        //return;
    }
    
    int main()
    {
        while(~scanf(" %d %d",&n,&m))
        {
            if(n==0||m==0) break;
            for(int i=1; i<=n; i++)
             for(int j=1; j<=m; j++)
                scanf("%d",&mp[i][j]);
    
            int T;
            scanf("%d",&T);
            while(T--)
            {
                scanf("%d %d %d %d",&x,&y,&x2,&y2);
                flag=0;
                bfs();
                if(flag) printf("YES
    ");
                else printf("NO
    ");
            }
        }
        return 0;
    }
    

    版权声明:本文为博主原创文章,未经博主允许不得转载。http://xiang578.top/

  • 相关阅读:
    安全探讨之用Win32汇编写双进程守护
    LightTPD 1.4.12
    mysql4存在mysql5没有的性能成绩
    gcolor2-拾色器
    solaris 中挂载usb移动硬盘
    Browsershots:测试你的 Web 企图
    MythTV 0.20
    XorgEdit:xorg.conf 编纂器
    pci168c,1c无线网卡如何在64位Solaris系统上运用
    Fedora8中批改磁盘卷标
  • 原文地址:https://www.cnblogs.com/xryz/p/4848039.html
Copyright © 2011-2022 走看看