首页 > 其他 > 详细

POJ 1979 Red and Black (简单dfs)

时间:2017-11-09 12:57:10      阅读:165      评论:0      收藏:0      [点我收藏+]

题目:

技术分享

 

简单dfs,没什么好说的

 

 

代码:

#include <iostream>
using namespace std;
typedef long long ll;
#define INF 2147483647

int w,h;
char a[22][22];
int dir[4][2] = {-1,0,1,0,0,-1,0,1};
int ans = 0;

void dfs(int x,int y){
    if(x < 0 || x >= h || y < 0 || y >= w || a[x][y] == #) return;
    ans++;
    a[x][y] = #;
    for(int i = 0;i < 4; i++){
        dfs(x+dir[i][0],y+dir[i][1]);
    }
}

int main(){
    while(cin >> w >> h){
        if(w == 0 && h == 0) break;
        ans = 0;
        int sx,sy;
        for(int i = 0;i < h; i++){
            for(int j = 0;j < w; j++){
                cin >> a[i][j];
                if(a[i][j] == @){
                    sx = i;sy = j;
                }
            }
        }
        dfs(sx,sy);
        cout << ans << endl;
    }
    return 0;
} 

 

POJ 1979 Red and Black (简单dfs)

原文:http://www.cnblogs.com/zhangjiuding/p/7808356.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!