codeforces 598D Igor In the Museum

题目链接http://codeforces.com/problemset/problem/598/D

题目分类:dfs

题目分析:处理的时候一次处理一片而不是一个,不然会超时

代码

#include<bits/stdc++.h>

using namespace std;

int n,m,k,a,b,ii;
int ans;
char ch[1500][1500];
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int dp[1500][1500];
int p[100007];

void dfs(int x,int y)
{
    if(x>n||x<1||y>m||y<1)
        return ;
    if(ch[x][y]=='*')
    {
        ans++;
        return ;
    }
    if(dp[x][y]!=0)
        return;

    dp[x][y]=ii;
    for(int i=0;i<4;i++)
    {
        int temp_x=x+dx[i];
        int temp_y=y+dy[i];
        dfs(temp_x,temp_y);
    }
    return;
}

int main()
{
    cin>>n>>m>>k;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            cin>>ch[i][j];
        }
    }

    for(ii=1;ii<=k;ii++)
    {
        cin>>a>>b;
        ans=0;
        if(!dp[a][b])
            dfs(a,b);
        else
           ans=p[dp[a][b]];
        p[ii]=ans;
        cout<<ans<<endl;
    }
    return 0;
}
anytime you feel the pain.hey,refrain.don't carry the world upon your shoulders
原文地址:https://www.cnblogs.com/gaoss/p/4973053.html