HDU 1312 Red and Black (第一道广搜)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1312

 

大意:从@出发,只能站在不能站在#上。。。。求能踩到的.的个数(包括起始点@)

 

#include <set>
#include <map>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <cctype>
#include <cstring>
#include <sstream>
#include <fstream>
#include <cstdlib>
#include <cassert>
#include <iostream>
#include <algorithm>

using namespace std;
//Constant Declaration
/*
--------------------------*/
//#define LL long long
#define LL __int64
const int M = 100;
const int INF = 1<<30;
const double EPS = 1e-11;
const double PI = acos(-1.0);
/*--------------------------*/
// some essential funtion
/*
----------------------------------*/
void Swap(int &a,int &b){ int t=a;a=b;b=t; }
int Max(int a,int b){ return a>b?a:b; }
int Min(int a,int b){ return a<b?a:b; }
int Gcd(int a,int b){ while(b){b ^= a ^=b ^= a %= b;} return a; }
/*----------------------------------*/
//for (i = 0; i < n; i++)
/*
----------------------------------*/


char g[M][M];
int to[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int n, m;

struct Point
{
int x, y;
};

int Bfs(int x, int y)
{
int sum = 1;
int i;
queue <Point> Q;
Point a, front, go;
a.x = x;
a.y = y;
Q.push(a);
g[x][y] = '#';
while (!Q.empty())
{
front = Q.front();
Q.pop();
for (i = 0; i < 4; i++)
{
go.x = front.x + to[i][0];
go.y = front.y + to[i][1];
if (go.x>=0 && go.x<n && go.y>=0 && go.y<m && g[go.x][go.y] == '.')
{
sum++;
Q.push(go);
g[go.x][go.y] = '#';//关键
}
}
}
return sum;
}


int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
//int t, case1 = 0;
//scanf("%d", &t);

int i, j;
int x, y;
while (scanf("%d%d", &m, &n), n+m)
{
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
scanf(" %c", &g[i][j]);
if (g[i][j] == '@')
{
x = i;
y = j;
}
}
}
printf("%d\n", Bfs(x, y));
}

return 0;
}



posted on 2012-04-03 00:38  [S*I]SImMon_WCG______*  阅读(302)  评论(0编辑  收藏  举报

导航