POJ1979 dfs

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Tile {

static int w;
static int h;
static int total;
static int[] stp = new int[2];
static char[][] rect;
static int[][] dir={{-1,0},{1,0},{0,-1},{0,1}};

public static void main(String[] args) throws FileNotFoundException {
    // TODO Auto-generated method stub
    Scanner sc = new Scanner(System.in);
    sc = new Scanner(new File("src/file/tile"));
    while (sc.hasNext()) {
        w = sc.nextInt();
        h = sc.nextInt();
        if(w==0&&h==0)
            break;
        rect = new char[h + 2][w + 2];
        for (int i = 0; i < h + 2; i++) {
            rect[i][0] = '#';
            rect[i][w + 1] = '#';
        }
        for (int i = 0; i < w + 2; i++) {
            rect[0][i] = '#';
            rect[h + 1][i] = '#';
        }
        for (int i = 1; i < h + 1; i++) {
            char[] tmpread = sc.next().toCharArray();
            for (int j = 1; j < w + 1; j++) {
                rect[i][j] = tmpread[j - 1];
                if (tmpread[j - 1] == '@') {
                    stp[0] = i;
                    stp[1] = j;
                }
            }
        }
        total=0;
        Walk(stp[0],stp[1]);
        System.out.println(total+1);
    }
}

private static void Walk(int x, int y) {
    // TODO Auto-generated method stub
    for(int i=0;i<4;i++){
        int nx=x+dir[i][0];
        int ny=y+dir[i][1];
        if(rect[nx][ny]=='.'){
            rect[nx][ny]='#';
            total++;
            Walk(nx,ny);
        }
    }
}

}

sample input:
2 2

#

@#
6 9
….#.
…..#
……
……
……
……
……

@…

.#..#.
11 9
.#………
.#.#######.
.#.#…..#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#…….#.
.#########.
………..
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..

.

…@…

.

..#.#..
..#.#..
0 0

sample output:
1
45
59
6
13

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值