POJ 1979 —— 搜索

Red and Black
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 19668   Accepted: 10514

Description

There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can move only on black tiles. 

Write a program to count the number of black tiles which he can reach by repeating the moves described above. 

Input

The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20. 

There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows. 

'.' - a black tile 
'#' - a red tile 
'@' - a man on a black tile(appears exactly once in a data set) 
The end of the input is indicated by a line consisting of two zeros. 

Output

For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).

Sample Input

6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0

Sample Output

45
59
6
13

Source

題意是給你w*h的格子,’.‘格子可以走,'#'格子不能走,'@'是起點,輸出最大活動範圍

裸的DFS,不用加任何剪枝,腦子抽到了調了半個多小時大哭

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <utility>
#include <cassert>
using namespace std;
///#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , mid  , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
#define mk make_pair
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
const int MAXN = 20 + 5;
const int MAXS = 10000 + 50;
const int sigma_size = 26;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7fffffff;
const int IMIN = 0x80000000;
const int inf = 1 << 30;
#define eps 1e-10
const long long MOD = 1000000000 + 7;
const int mod = 10007;
typedef long long LL;
const double PI = acos(-1.0);
typedef double D;
typedef pair<int , int> pii;
typedef vector<int> vec;
typedef vector<vec> mat;


#define Bug(s) cout << "s = " << s << endl;
///#pragma comment(linker, "/STACK:102400000,102400000")
int w , h;
char g[MAXN][MAXN];
int dx[4] = {-1 , 0 , 1 , 0};
int dy[4] = {0 , 1 , 0 , -1};
int ans;
int vis[MAXN][MAXN];
void dfs(int i , int j)
{
    int ok = 0;
//    vis[i][j] = 1;
    for(int k = 0 ; k < 4 ;  k++)
    {
        int x = i + dx[k] , y = j + dy[k];
        if(!vis[x][y] && 0 <= x && x < h && 0 <= y && y < w && g[x][y] == '.')
        {

            ok = 1;
            ans++;
//            cout << i << '+' << dx[k] << '*' << j << '+' << dy[k] << endl;
//            cout << x << ':' << y << "  ans: " << ans << endl;
            vis[x][y] = 1;
            dfs(x , y);
//            vis[x][y] = 0;
        }
//        if(ok)break;
    }
//    cout << ok << endl;
    if(!ok)return;
}
void solve()
{
    int mx , my;
    for(int i = 0 ; i < h ; i ++)for(int j = 0 ; j < w ; j++)
    {
        if(g[i][j] == '@')
        {
            mx = i;
            my = j;
            break;
        }
    }
//    cout << mx << " () "<< my << endl;
    ans = 1;
    vis[mx][my] = 1;
//    outstars
    dfs(mx , my);
    printf("%d\n" , ans);
}
int main()
{
    while(scanf("%d%d" , &w , &h) , w || h)
    {
        for(int i = 0 ; i < h; i++)scanf("%s" , g[i]);
        clr(vis , 0);
        solve();
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章