zoukankan      html  css  js  c++  java
  • 白银莲花池

    #include <bits/stdc++.h>
    
    using namespace std;
    
    #define p make_pair
    #define ll long long
    #define INF 0x3f3f3f3f
    #define MAXN 1000010
    #define MAXM 1010
    
    inline int read()
    {
        int x = 0,ff = 1;char ch = getchar();
        while(!isdigit(ch))
        {
            if(ch == '-') ff = -1;
            ch = getchar();
        }
        while(isdigit(ch))
        {
            x = (x << 1) + (x << 3) + (ch ^ 48);
            ch = getchar();
        }
        return x * ff;
    }
    
    int n,m,sx,sy,tx,ty,sum = 0,a[MAXM][MAXM],vis[MAXM][MAXM],v[MAXM][MAXM];
    int dx[8] = {2,2,-2,-2,1,1,-1,-1};
    int dy[8] = {1,-1,1,-1,2,-2,2,-2};
    ll f[MAXM][MAXM],dis[MAXM][MAXM],d[MAXM][MAXM];
    
    void BFS()
    {
        memset(dis,0x3f,sizeof(dis));
        memset(vis,false,sizeof(vis));
        queue < pair < int , int > > q;
        q.push(p(sx,sy)); dis[sx][sy] = 0; vis[sx][sy] = true;
        while(!q.empty())
        {
            int x = q.front().first;
            int y = q.front().second;
            q.pop();
            vis[x][y] = false;
    //        printf("x = %d y = %d
    ",x,y);
            for(int i = 0;i <= 7;++i)
            {
                int xx = x + dx[i];
                int yy = y + dy[i];
                if(xx < 1 || xx > n || yy < 1 || yy > m || a[xx][yy] == 2) continue;
                if(dis[xx][yy] > dis[x][y] + v[xx][yy])
                {
                    dis[xx][yy] = dis[x][y] + v[xx][yy];
    //            printf("xx = %d yy = %d
    ",xx,yy);
                    if(xx == tx && yy == ty) continue;
                    if(!vis[xx][yy]) 
                    {
                        vis[xx][yy] = true;
                        q.push(p(xx,yy));
                    }
                }
            }
        }
        
    }
    
    void BFS2()
    {
        memset(d,0x3f,sizeof(d));
        memset(vis,false,sizeof(vis));
        queue < pair < int , int > > q;
        q.push(p(sx,sy)); d[sx][sy] = 0; vis[sx][sy] = true;
        while(!q.empty())
        {
            int x = q.front().first;
            int y = q.front().second;
            q.pop();
            vis[x][y] = false;
    //        printf("x = %d y = %d
    ",x,y);
            for(int i = 0;i <= 7;++i)
            {
                int xx = x + dx[i];
                int yy = y + dy[i];
                if(xx < 1 || xx > n || yy < 1 || yy > m || a[xx][yy] == 2) continue;
                if(dis[xx][yy] != dis[x][y] + v[xx][yy]) continue;
                if(d[xx][yy] > d[x][y] + 1)
                {
                    d[xx][yy] = d[x][y] + 1;
    //            printf("xx = %d yy = %d
    ",xx,yy);
                    if(xx == tx && yy == ty) continue;
                    if(!vis[xx][yy]) 
                    {
                        vis[xx][yy] = true;
                        q.push(p(xx,yy));
                    }
                }
                
            }
        }
        
    }
    
    void BFS3()
    {
        memset(vis,false,sizeof(vis));
        queue < pair < int , int > > q;
        q.push(p(sx,sy)); vis[sx][sy] = true; f[sx][sy] = 1;
        while(!q.empty())
        {
            int x = q.front().first;
            int y = q.front().second;
            q.pop();
            vis[x][y] = false;
            for(int i = 0;i <= 7;++i)
            {
                int xx = x + dx[i];
                int yy = y + dy[i];
                if(xx < 1 || xx > n || yy < 1 || yy > m || a[xx][yy] == 2) continue;
                if(dis[xx][yy] != dis[x][y] + v[xx][yy]) continue;
                if(d[xx][yy] != d[x][y] + 1) continue;
                f[xx][yy] += f[x][y];
                if(xx == tx && yy == ty) continue; 
                if(!vis[xx][yy]) 
                {
                    vis[xx][yy] = true;
                    q.push(p(xx,yy));
                }    
            }
        }
    }
    
    int main()
    {
        n = read(); m = read();
        for(int i = 1;i <= n;++i)
        {
            for(int j = 1;j <= m;++j)
            {
                a[i][j] = read();
                if(a[i][j] == 3) { sx = i; sy = j; v[i][j] = 0;}
                if(a[i][j] == 4) { tx = i; ty = j; v[i][j] = 0;}
                if(a[i][j] == 0) v[i][j] = 1;
                if(a[i][j] == 1) v[i][j] = 0;
            }
        }
        BFS();
        if(dis[tx][ty] == dis[0][0]) { printf("-1
    "); return 0; }
        printf("%lld
    ",dis[tx][ty]);
        BFS2();
        printf("%lld
    ",d[tx][ty]);
        BFS3();
        printf("%lld
    ",f[tx][ty]);
        return 0;
    }
  • 相关阅读:
    深度分页,不存在的
    netty做客户端实战(二)
    netty服务端实战(一)
    netty做上位机主动同时连接多个下位机时,接受报文报 java.lang.IndexOutOfBoundsException: srcIndex: 0异常错误
    使用线程实现多生产者,多消费者demo
    vscode中vue安装,使用的插件
    实用的RBAC模型
    Springboot+thymeleaf+shiro整合
    Log4j作用
    SpringBoot整合thymeleaf
  • 原文地址:https://www.cnblogs.com/AK-ls/p/10397857.html
Copyright © 2011-2022 走看看