• 01迷宫


    https://www.luogu.org/problemnew/show/P1141

    这题全部输入后 把答案存起来 ,再把答案一次性输出

    和边输入边输出    前者时间花的更少。 

    用stack来打表吧,针对同一个n 同一个mp地图,把所有的点分别能到几个地方 都算一遍,(能到达一个相同地方的不同点 ,一定是可以互相联通的

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 #include<cstring>
     5 #include<cmath>
     6 #include<string>
     7 #include<cmath>
     8 #include<set>
     9 #include<vector>
    10 #include<stack>
    11 #include<queue>
    12 #include<map>
    13 using namespace std;
    14 #define ll long long
    15 #define mem(a,x) memset(a,x,sizeof(a))
    16 #define se second
    17 #define fi first
    18 const int INF= 0x3f3f3f3f;
    19 const int N=1e5+5;
    20 
    21 int  n,m,cnt=0;
    22 int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1},ans[1005][1005],g[N],h[N];
    23 char mp[1005][1005];
    24 bool vis[1005][1005];
    25 
    26 struct note
    27 {
    28     int x,y;
    29 }a,b;
    30 queue<note>q;
    31 stack<note>p;
    32 
    33 void bfs(int nx,int ny)
    34 {
    35     while(!q.empty())
    36         q.pop();
    37     a.x=nx;
    38     a.y=ny;
    39     cnt=1;
    40     vis[a.x][a.y]=1;
    41     q.push(a);
    42     p.push(a);
    43     while(!q.empty())
    44     {
    45         a=q.front();
    46         q.pop();
    47         for(int i=0;i<4;i++)
    48         {
    49             b.x=a.x+dx[i];
    50             b.y=a.y+dy[i];
    51             if(b.x>n||b.x<1||b.y>n||b.y<1||vis[b.x][b.y]==1||mp[b.x][b.y]==mp[a.x][a.y])
    52                 continue;
    53 
    54             p.push(b);
    55             cnt++;
    56             vis[b.x][b.y]=1;
    57             q.push(b);
    58         }
    59     }
    60     while(!p.empty())
    61     {
    62         a=p.top();
    63         p.pop();
    64         ans[a.x][a.y]=cnt;
    65     }
    66 }
    67 
    68 int main()
    69 {
    70     cin>>n>>m;
    71     for(int i=1;i<=n;i++)
    72         scanf("%s",mp[i]+1);
    73     for(int i=1;i<=m;i++)
    74         scanf("%d%d",&g[i],&h[i]);
    75     for(int i=1; i<=n; i++)
    76     {
    77         for(int j=1; j<=n; j++)
    78         {
    79             if(!ans[i][j]) //打表
    80             {
    81                 bfs(i,j);
    82             }
    83         }
    84     }
    85     for(int i=1; i<=m; i++)
    86         cout<<ans[g[i]][h[i]]<<endl;
    87 }
  • 相关阅读:
    数组初始化 和 vector初始化
    剑指offer42 左旋转字符串
    k sum(lintcode)
    背包问题2 (lintcode)
    92.背包问题(lintcode)
    72. Edit Distance
    79 最长公共子串 (lintcode)
    77 最长公共子序列 (lintcode)
    132. Palindrome Partitioning II
    Mysql经常使用函数汇总
  • 原文地址:https://www.cnblogs.com/thunder-110/p/9330361.html
Copyright © 2020-2023  润新知