美文网首页
poj1979 Red and Black

poj1979 Red and Black

作者: 刘小小gogo | 来源:发表于2018-08-27 23:51 被阅读0次

http://poj.org/problem?id=1979

image.png
image.png
image.png

典型的dfs
注意:将走过的路设置为"#",不需要还原回去。

//
// Created by ljs on 2018/8/27.
//
#include <iostream>
using namespace std;
int m, n;
int dir[4][4] = {{-1, 0}, {0, 1},{1, 0}, {0, -1}};
int count = 0;

void dfs(char maze[21][21], int x, int y){
    count++;
    for(int i = 0; i < 4; i++){
        int xx = x + dir[i][0];
        int yy = y + dir[i][1];
        if(xx >= 0 && xx <= m-1 && yy >= 0 && yy <= n-1 && maze[xx][yy] == '.'){
            maze[xx][yy] = '#';
            dfs(maze,xx, yy);
        }
    }
}
int main(){
    while(1){
        cin>>n>>m;
        if(m == 0 && n == 0) break;
        int startx, starty;
        char maze[21][21];
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                cin>>maze[i][j];
                if(maze[i][j] == '@'){
                    startx = i;
                    starty = j;
                }
            }
        }
        maze[startx][starty] == '#';
        count = 0;
        dfs( maze, startx, starty);
        cout<<count<<endl;
    }
}


相关文章

  • poj1979 Red and Black

    http://poj.org/problem?id=1979 典型的dfs注意:将走过的路设置为"#",不需要还原回去。

  • 红黑树

    Red-Black Tree Red-Black Tree is a self-balancing Binary ...

  • Red-Black Tree

    Root is black Leaf(NIL) is black If node is red, its chil...

  • JDK 1.8 map

    HashMap Implementatioan 实现结构 Array + Red-Black Tree Red-B...

  • 红黑树与avl的缺别

    Red black tree over avl tree

  • POJ-1979 Red and Black Q:There is a rectangular room, ...

  • Joey-晓得-19.3.1

    This idiom describes four colors “blue”, “red”, “black” a...

  • Red&Black

    夜晚,还涤荡着一种柔软的梦 穿过街巷, 行至最深处,去找寻一杯红色的痴缠 念岁月的静好,不再计较, 那不安分的萌动...

  • BLACK&RED

    我記得有這樣一句話:山上層層桃李花,雲間煙火是人家。人間煙火可能就像我現在一樣,沉浸在這個璀璨世界里,聞到焦糊的紅...

  • Red Black Tree

    Introduction https://www.geeksforgeeks.org/red-black-tree...

网友评论

      本文标题:poj1979 Red and Black

      本文链接:https://www.haomeiwen.com/subject/ztpxwftx.html