• luogu cogs P1141 01迷宫


    题目描述

    有一个仅由数字0与1组成的n×n格迷宫。若你位于一格0上,那么你可以移动到相邻4格中的某一格1上,同样若你位于一格1上,那么你可以移动到相邻4格中的某一格0上。

    你的任务是:对于给定的迷宫,询问从某一格开始能移动到多少个格子(包含自身)。

    输入输出格式

    输入格式:

    输入的第1行为两个正整数n,m。

    下面n行,每行n个字符,字符只可能是0或者1,字符之间没有空格。

    接下来m行,每行2个用空格分隔的正整数i,j,对应了迷宫中第i行第j列的一个格子,询问从这一格开始能移动到多少格。

    输出格式:

    输出包括m行,对于每个询问输出相应答案。

    输入输出样例

    输入样例#1:
    2 2
    01
    10
    1 1
    2 2
    
    输出样例#1:
    4
    4
    

    说明

    所有格子互相可达。

    对于20%的数据,n≤10;

    对于40%的数据,n≤50;

    对于50%的数据,m≤5;

    对于60%的数据,n≤100,m≤100;

    对于100%的数据,n≤1000,m≤100000。

    TLE 3点:

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    #include<queue>
    #include<cstdlib>
    #include<cstring>
    
    using namespace std;
    const int N=1001;
    const int xd[]={0,-1,0,1};
    const int yd[]={-1,0,1,0};
    
    struct node{
        int x,y;
    }now,top,nxt;
    int a[N][N];
    int vis[N][N];
    queue<node>q;
    int n;
    int Q;
    int strx,stry;
    int answer;
    
    inline int read()
    {
        int x=0;char c=getchar();
        while(c<'0'||c>'9')c=getchar();
        return x=c-'0';
    }
    
    inline void bfs(int x,int y)
    {
        now.x=x;
        now.y=y;
        vis[x][y]=1;
        q.push(now);
        while(!q.empty())
        {
            top=q.front();
            q.pop();
            for(int i=0;i<4;i++)
            {
                int x=top.x+xd[i];
                int y=top.y+yd[i];
                if(!vis[x][y]&&x>0&&x<=n&&y>0&&y<=n&&a[top.x][top.y]+a[x][y]==1)
                {
                    vis[x][y]=1;
                    nxt.x=x;
                    nxt.y=y;
                    q.push(nxt);
                    answer++;
                }
            }
        }
        return ;    
    }
    
    int main()
    {
        freopen("maze01.in","r",stdin);
        freopen("maze01.out","w",stdout);
        scanf("%d%d",&n,&Q);
        
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                a[i][j]=read();
        
        while(Q--)
        {
            scanf("%d%d",&strx,&stry);
            answer=1;
            memset(vis,0,sizeof(vis));
            bfs(strx,stry);
            printf("%d
    ",answer);
        }
        
        return 0;
    }

    预处理后离线处理(思路666):

    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <queue>
    #include <algorithm>
    #include<iostream>
    
    using namespace std;
    const int MAXN=1010;
    const int step[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
    
    int N,M,map[MAXN][MAXN];
    int ans[MAXN][MAXN]; 
    class Point{public:int x,y;};
    queue <Point> ps,q;
    int res,qx,qy;
     
    inline void bfs(int si,int sj)
    {
        q.push((Point){si,sj});
        int nx,ny; Point tmp;
        ans[si][sj]=1; res=1;
        while(q.size())
        {
            tmp=q.front(); q.pop();
            for(int i=0;i<4;i++)
            {
                nx=tmp.x+step[i][0];
                ny=tmp.y+step[i][1];
                if(nx<1 || nx>N || ny<1 || ny>N) continue;
                if(map[tmp.x][tmp.y]+map[nx][ny]!=1) continue;
                if(ans[nx][ny]) continue;
                res++; q.push((Point){nx,ny});
                ps.push((Point){nx,ny});
                ans[nx][ny]=1;
            }
        }
        while(ps.size())//zhe yi bu so shuai;
        {
            tmp=ps.front(); ps.pop();
            ans[tmp.x][tmp.y]=res;
        }
    }
     
    int main()
    {
        freopen("maze01.in","r",stdin);
        freopen("maze01.out","w",stdout);
        scanf("%d %d
    ",&N,&M);char c;
        for(int i=1;i<=N;i++)
        {
            for(int j=1;j<=N;j++)
            {
                c=0;  while(c!='0' && c!='1') 
                scanf("%c",&c); map[i][j]=c-'0';
            }
        }
        for(int i=1;i<=N;i++)
        {
            for(int j=1;j<=N;j++)
            {
                if(ans[i][j]) continue;
                ps.push((Point){i,j});
                bfs(i,j);
            }
        }     
        for(int i=1;i<=M;i++)
        {
            scanf("%d%d
    ",&qx,&qy);
            printf("%d
    ",ans[qx][qy]);
        }
        return 0;
    }
  • 相关阅读:
    未来简史之数据主义(Dataism)
    10分钟看懂《人类简史》和《未来简史》
    SignalR来做实时Web聊天
    .Net Core应用搭建的分布式邮件系统设计
    AspNetCore-MVC实战系列(四)之结尾
    AspNetCore-MVC实战系列(三)之个人中心
    AspNetCore-MVC实战系列(二)之通过绑定邮箱找回密码
    AspNetCore
    爱留图
    .NetCore上传多文件的几种示例
  • 原文地址:https://www.cnblogs.com/lyqlyq/p/7074382.html
Copyright © 2020-2023  润新知