| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
第一道bfs,付代码#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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator