HDU1312( Red and Black )

来源:互联网 发布:免费手机短信恢复软件 编辑:程序博客网 时间:2024/04/29 13:57
Problem : 1312 ( Red and Black )     Judge Status : AcceptedRunId : 5535840    Language : C++    Author : ssunCode Render Status : Rendered By HDOJ C++ Code Render Version 0.01 Beta#include<iostream>#include<string>using namespace std;int n,m;int count;//计数器char map[23][23];//地图数组bool visited[23][23];//访问状态数组void bfs(int x, int y){    if(x<0 || x>m || y<0 || y>n || map[x][y]=='#' || visited[x][y]==true )//判断是否满足遍历条件        return;    else    {        visited[x][y] = true;//记录该位置已被访问过        count++;        bfs(x+1,y);        bfs(x-1,y);        bfs(x,y-1);        bfs(x,y+1);    }}int main(){    int i,j;    char stx,sty;    while(cin>>n>>m)    {        if(!n&&!m) break;        count = 0;        memset(map,'#',sizeof(map));//初始化地图和访问状态        memset(visited,false,sizeof(visited));        for(i=1; i<=m; i++)        {            for(j=1; j<=n; j++)            {                cin>>map[i][j];                if(map[i][j] == '@')                {                    stx = i, sty =j;//记录起始位置                }            }            getchar();//略去回车键            //cout<<map[i]<<endl;        }        bfs(stx,sty);//开始遍历        printf("%d\n",count);    }    return 0;}

原创粉丝点击