蓝桥杯以后好长时间没看算法,真的是要靠比赛来逼着自己练习,自主性不够啊

poj 1979 基本题 不解释 记录下代码

#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
char field[20][20];
int W,H;
int count;
int dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}} ;
void dfs(int x,int y){
    field[y][x]='#';
    count++;
    for(int i=0;i<4;i++){
        int dx=x+dir[i][0];
        int dy=y+dir[i][1];
        if(dx<W && dx>=0 && dy<H && dy>=0 && field[dy][dx]!='#'){
            dfs(dx,dy);
        }
    }
}


int main(int argc, char** argv) {
    int x,y;
    while(1){
        cin>>W>>H;
        if(W==0 && H==0) break;
        for(int i=0;i<H;i++){
            for(int j=0;j<W;j++){
                cin>>field[i][j];
                if(field[i][j]=='@'){
                    x=j;
                    y=i;
                }
            }
        }
        count=0;
        dfs(x,y);
        cout<<count<<endl;
    }
    
    return 0;
}

 

posted on 2017-05-11 20:31  kapike  阅读(74)  评论(0)    收藏  举报