ahua的头发

导航

蓝桥刷题--Red and Black

poj 1979 Red and Black

题目地址:http://poj.org/problem?id=1979

只是为了学习思想 代码很乱 没有优化 没有修改 只作为自己的记录

#pragma GCC positionimize(3,"Otrst","inline")
//#include "emplace.hpp"
//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long ll;
const int N=1e6+10;
const int INF=0x3f3f3f;
const double pi = 3.141592653;
const double E = 2.71828182846;
const ll mod=1e9+7;
const int M=1e3+5;
const double eps=1e-8;
#define ci(x) scanf("%d",&x)
#define co(x) printf("%d\n",x)
using namespace std;
char str[105][105];
int n,m; 
int dir[5][5]={{1,0},{-1,0},{0,1},{0,-1}};
int vis[105][105]={0};
int ans=0;
void dfs(int x,int y)
{
    ans++;
    vis[x][y]=true;
    for(int i=0;i<4;i++)
    {
        int xx=x+dir[i][0];
        int yy=y+dir[i][1];
        //printf("%d %d\n",xx,yy);
        if(xx>=0&&xx<m&&yy>=0&&yy<n&&str[xx][yy]=='.'&&vis[xx][yy]==false)
        {
            //printf("dfs %d %d\n",xx,yy);
            dfs(xx,yy);
        }
    }
}
int main()
{
    while(~scanf("%d %d",&n,&m)){
        getchar();
        if(n==0&&m==0)
            break;
        ans=0;
        memset(vis,0,sizeof(vis));
        for(int i=0;i<m;i++)
        {
            cin>>str[i];
        }
        int x,y;
        for(int i=0;i<m;i++)
        {
            bool flag=false;
            for(int j=0;j<n;j++)
            {
                if(str[i][j]=='@')
                {
                    x=i;
                    y=j;
                    flag=true;
                    break;
                }
            }
            if(flag)
                break;
        }
        //printf("%d %d\n",x,y);
        dfs(x,y);
        printf("%d\n",ans);
    }
    return 0;
}

 

posted on 2020-09-24 17:32  ahua的头发  阅读(93)  评论(0编辑  收藏  举报