pku/poj 1562

入门级的搜索,有点悲剧

#include <stdio.h>
#include <iostream>
using namespace std;
#include <string.h>
#define pr printf
char ch[105][105];
int r,c;
bool visit[105][105],s[105][105];
int arr[8][2]={{-1,-1},{0,-1},{1,-1},{1,0},{1,1},{0,1},{-1,1},{-1,0}};
bool Judge(int row,int column)
{
     return ((row>=0&&row<r)&&(column>=0&&column<c));
}
//题目要求的就是搜索的次数
void DFS(int a, int b)
{
     for (int i=0; i<8; i++)
     {
         int nr=a+arr[i][0];
         int nc=b+arr[i][1];
         if (Judge(nr,nc)&&!visit[nr][nc]&&s[nr][nc])
         {
            visit[nr][nc]=true;
            DFS(nr,nc);    //一开始写到括号外面去了,看样子大括号不能省啊
         } 
     }
}

int main()
{
    int ans=0,i,j;
    while (scanf("%d%d",&r,&c)!=-1&&r&&c)
    {
          ans=0;
          for (i=0; i<r; i++)
               cin>>ch[i];//scanf ("%s",&ch[i]);//紧记二维数组的输入方式
          for (i=0; i<r; i++)
          {
              for (j=0; j<c; j++)
              {
                  if (ch[i][j]=='*')
                     s[i][j]=false;
                  else if (ch[i][j]=='@')
                     s[i][j]=true;
              }
          }
          memset(visit,0,sizeof(visit));
          for (i=0; i<r; i++)
          {
              for (j=0; j<c; j++)
              {
                  if (!visit[i][j]&&s[i][j])
                  {
                     visit[i][j]=true;
                     DFS(i,j);
                     ans++;                     
                  }
              }
          }
          pr ("%d/n",ans);
    }
    return 0;
}

你可能感兴趣的:(c)