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

第一道bfs,付代码

Posted by 821631813 at 2014-07-29 16:10:56 on Problem 1979
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <algorithm>
#define maxn 30
using namespace std;
struct node{
    int x,y;
};
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int sx,sy;
int n,m;
char a[maxn][maxn];
int use[maxn][maxn];
int number;
void bfs()
{
    queue<node>que;
    node hh;
    hh.x=sx;hh.y=sy;
    use[sx][sy]=1;
    que.push(hh);
    while(!que.empty()){
        node gg=que.front();
        que.pop();
        for(int i=0;i<4;i++)
        {
            node zz;
            zz.x=gg.x+dx[i];
            zz.y=gg.y+dy[i];
            if(zz.x<0||zz.x>=n||zz.y<0||zz.y>=m||a[zz.x][zz.y]=='#'||use[zz.x][zz.y]==1)
            continue;
            que.push(zz);
          //  cout<<zz.x<<" "<<zz.y<<endl;
            use[zz.x][zz.y]=1;
            number++;
        }
    }
}
int main()
{
     #ifndef ONLINE_JUDGE
                    freopen("input.txt","r" ,stdin);
                #endif // ONLINE_JUDGE
    while(cin>>m>>n&&(n>0&&m>0)){
        memset(use,0,sizeof(use));
        number=1;
        for(int i=0;i<n;i++){
          for(int j=0;j<m;j++){
            cin>>a[i][j];
            if(a[i][j]=='@'){
                sx=i;
                sy=j;
            }
          }
        }
        bfs();
        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