Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Re:顺便把dfs代码也附上

Posted by 821631813 at 2014-07-29 16:30:18 on Problem 1979
In Reply To:第一道bfs,付代码 Posted by:821631813 at 2014-07-29 16:10:56
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <algorithm>
#define maxn 30
using namespace std;
char map[maxn][maxn];
int dx[]={0,1,-1,0};
int dy[]={1,0,0,-1};
int use[maxn][maxn];
int number;
int n,m,sx,sy;
void dfs(int x,int y){
   // use[x][y]=1;
    for(int i=0;i<4;i++){
        int xx=x+dx[i];
        int yy=y+dy[i];
        if(map[xx][yy]=='#'||xx<0||xx>=n||yy<0||yy>=m||use[xx][yy]==1)
        continue;
        use[xx][yy]=1;
        number++;
        dfs(xx,yy);
  }
}

int main(){
    #ifndef ONLINE_JUDGE
                freopen("input.txt","r" ,stdin);
            #endif // ONLINE_JUDGE
    while(cin>>m>>n){
        if(n==0&&m==0)
         break;
        memset(use,0,sizeof(use));
        number=1;
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
              cin>>map[i][j];
              if(map[i][j]=='@'){
                 sx=i;
                 sy=j;
              }
           }
        }
       use[sx][sy]=1;
       dfs(sx,sy);
       cout<<number<<endl;
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator