首页 > 其他分享 >北大ACM poj1562

北大ACM poj1562

时间:2023-08-21 17:04:15浏览次数:43  
标签:北大 map int sum dfs poj1562 ACM include

/*problem:poj1562
  *author:gubojun 
  *time:2012.7.27
  *language:C
  *题目大意:求有多少个不相连的区域代码中的解释很多了,别的不说了
  */
 #include<stdio.h>
 #include<string.h>
 #define N 101
 char map[N][N];
 int n,m,sum;
 void dfs(int i,int j)
 {
  //如果 map[i][j]='@'或超出范围,返回 
  if(map[i][j]!='@'||i<0||j<0||i>=m||j>=n) return;
  else
  {
     map[i][j]='*';
     //调用递归把与'@'相连的'@'都变成'*' 
     dfs(i-1,j-1);
     dfs(i-1,j);
     dfs(i-1,j+1);
     dfs(i,j-1);
     dfs(i,j+1);
     dfs(i+1,j-1);
     dfs(i+1,j);
     dfs(i+1,j+1);
  }
 }
 int main()
 {
  int i,j;
  freopen("in.txt","r",stdin); 
  while(scanf("%d%d",&m,&n)!=EOF)//输入图形有多少行多少列—m行n列 
  {
   if(m==0) break;//如果m==0就退出 
   sum=0;//初始化sum 
   for(i=0;i<m;i++)
    scanf("%s",map[i]);//输入图形 
   for(i=0;i<m;i++)
   {
    for(j=0;j<n;j++)
    {
     if(map[i][j]=='@')//判断map中是否有'@' 
     {
      dfs(i,j);//避免重复统计 
      sum++;
         }
       }
      }
      printf("%d\n",sum);//输入结果 
  }
  return 0;
 }

标签:北大,map,int,sum,dfs,poj1562,ACM,include
From: https://blog.51cto.com/u_10101161/7177263

相关文章

  • 北大ACM poj 1001
    网上搜来的借鉴一下#include<stdio.h>main(){doublef;intn,i,j;inttop,bottom;intshow;for(;~scanf("%lf%d",&f,&n);){intp[300]={1},tmp=0;f*=1e6;for(i=0;i++<n;)......
  • 杭电ACM HDU 2816 I Love You Too
    ILoveYouTooTimeLimit:2000/1000MS(Java/Others)   MemoryLimit:32768/32768K(Java/Others)TotalSubmission(s):1258   AcceptedSubmission(s):759ProblemDescriptionThisisatruestory.Amanshowedhislovetoagirl,butthegirldi......
  • 杭电ACM HDU 3346 Lucky Number
    LuckyNumberTimeLimit:2000/1000MS(Java/Others)   MemoryLimit:32768/32768K(Java/Others)TotalSubmission(s):1523   AcceptedSubmission(s):800ProblemDescriptionToChinesepeople,8isaluckynumber.Nowyourtaskistojudgeifanu......
  • 杭电ACM HDU 2560 Buildings
    BuildingsTimeLimit:2000/1000MS(Java/Others)   MemoryLimit:32768/32768K(Java/Others)TotalSubmission(s):1828   AcceptedSubmission(s):1553ProblemDescriptionWedividetheHZNUCampusintoN*Mgrids.Asyoucanseefromthepicturebe......
  • 杭电ACM HDOJ 1039 Easier Done Than Said?
    EasierDoneThanSaid?TimeLimit:2000/1000MS(Java/Others)    MemoryLimit:65536/32768K(Java/Others)TotalSubmission(s):6387    AcceptedSubmission(s):3172ProblemDescriptionPasswordsecurityisatrickything.Usersprefersimplepas......
  • 杭电ACM HDU 3351 Seinfeld
    SeinfeldTimeLimit:2000/1000MS(Java/Others)   MemoryLimit:32768/32768K(Java/Others)TotalSubmission(s):1071   AcceptedSubmission(s):540ProblemDescriptionI’moutofstories.ForyearsI’vebeenwritingstories,somerathersilly,......
  • 西农2022级ACM招新考题
    准备放弃一段时间算法。西农2022级ACM招新上周结束了,五一假期研究了一下题解,整理发在博客。1.这真的是签到题print("\"ACMwelcomesyou\\n\"")2.4和数#include<bits/stdc++.h>usingnamespacestd;constintmaxn=1e6+10;intf[maxn+1];intl,r;boolche......
  • ACM模式输入输出处理
    cin遇到\n会停止读取但是不会读出来1.数组长度确定,多组数据直接一个while循环输入151020输出630while(cin>>a>>b){cout<<a+b<<endl;}第一个数表示组数的,直接给个size,然后for循环输入2151020输出6302.数组长度不确定','分......
  • ACM常见格式提取代码
    1.提取指定范围符号内用逗号隔开数字vector<int>trans(string&str){size_tstart=str.find('[');size_tend=str.find(']');stringnumStr=str.substr(start+1,end-start-1);stringstreamss(numStr);intnumber;......
  • ACM之2000——2050题目答案及解析
    /*****************************2000题**********************************/#include<iostream>#include<algorithm>使用C++中的库函数,实现字符位置调整usingnamespacestd;intmain(void){charn[4];while(cin>>n){if(n[0]>n[1]......