zoukankan      html  css  js  c++  java
  • 【宽搜】Vijos P1051 送给圣诞夜的极光

    题目链接:

      https://vijos.org/p/1051

    题目大意

      给一张‘-’和‘#’的图,规定曼哈顿距离小于等于2的‘#’属于同一图案,求图案数。【曼哈顿距离:对于A(x1,y1)和B(x2,y2),A和B之间的曼哈顿距离为|x1-x2|+|y1-y2|】

    题目思路:

      【宽搜】

      找到一个没访问过的#,BFS把同图案的所有#标记。最后输出答案即可

     1 //
     2 //by coolxxx
     3 //
     4 #include<iostream>
     5 #include<algorithm>
     6 #include<string>
     7 #include<iomanip>
     8 #include<memory.h>
     9 #include<time.h>
    10 #include<stdio.h>
    11 #include<stdlib.h>
    12 #include<string.h>
    13 #include<stdbool.h>
    14 #include<math.h>
    15 #define min(a,b) ((a)<(b)?(a):(b))
    16 #define max(a,b) ((a)>(b)?(a):(b))
    17 #define abs(a) ((a)>0?(a):(-(a)))
    18 #define lowbit(a) (a&(-a))
    19 #define sqr(a) ((a)*(a))
    20 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
    21 #define eps 1e-8
    22 #define J 10
    23 #define MAX 0x7f7f7f7f
    24 #define PI 3.1415926535897
    25 #define N 107
    26 using namespace std;
    27 int cas,cass;
    28 int n,m,lll,ans;
    29 char map[N][N];
    30 bool u[N][N];
    31 void work(int i,int j)
    32 {
    33     if(u[i][j])return;
    34     u[i][j]=1;
    35     if(i-2>0 && map[i-2][j]=='#')work(i-2,j);
    36     if(i-1>0 && map[i-1][j]=='#')work(i-1,j);
    37     if(i+1<=n && map[i+1][j]=='#')work(i+1,j);
    38     if(i+2<=n && map[i+2][j]=='#')work(i+2,j);
    39     if(j-2>0 && map[i][j-2]=='#')work(i,j-2);
    40     if(j-1>0 && map[i][j-1]=='#')work(i,j-1);
    41     if(j+1<=m && map[i][j+1]=='#')work(i,j+1);
    42     if(j+2<=m && map[i][j+2]=='#')work(i,j+2);
    43     if(i-1>0 && j-1>0 && map[i-1][j-1]=='#')work(i-1,j-1);
    44     if(i-1>0 && j+1<=m && map[i-1][j+1]=='#')work(i-1,j+1);
    45     if(i+1<=n && j-1>0 && map[i+1][j-1]=='#')work(i+1,j-1);
    46     if(i+1<=n && j+1<=m && map[i+1][j+1]=='#')work(i+1,j+1);
    47 }
    48 int main()
    49 {
    50     #ifndef ONLINE_JUDGE
    51 //    freopen("1.txt","r",stdin);
    52 //    freopen("2.txt","w",stdout);
    53     #endif
    54     int i,j,k;
    55 //    while(~scanf("%s",s1))
    56     while(~scanf("%d",&n))
    57 //    for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
    58     {
    59         memset(u,0,sizeof(u));
    60         ans=0;
    61         scanf("%d",&m);
    62         for(i=1;i<=n;i++)
    63             scanf("%s",map[i]+1);
    64         for(i=1;i<=n;i++)
    65         {
    66             for(j=1;j<=m;j++)
    67             {
    68                 if(!u[i][j] && map[i][j]=='#')
    69                 {
    70                     work(i,j);
    71                     ans++;
    72                 }
    73             }
    74         }
    75         printf("%d
    ",ans);
    76     }
    77     return 0;
    78 }
    79 
    80 /*
    81 //
    82 
    83 //
    84 */
    千万不要点
  • 相关阅读:
    GMap.NET 随谈
    Chrome Uncaught Error: NETWORK_ERR: XMLHttpRequest Exception 101
    Java 自定义枚举
    如何得到全国省市经纬度范围
    Jquery formwizard 入门 【前台 向导功能】
    Eclipse Juno 安装 Aptana 3.3,支持jquery智能提示
    Ant学习【实践1】
    euerka总结 当幸福来敲门
    Spring Boot 热启动插件 当幸福来敲门
    ASP.NET 2.0 Provider Model 详细分析
  • 原文地址:https://www.cnblogs.com/Coolxxx/p/5506460.html
Copyright © 2011-2022 走看看