ZOJ 1047 Image Perimeters

题目地址:点击打开链接

思路:挺好的一题

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int dir[4][2] = {{-1,0},{1,0},{0,-1},{0,1}};
int diagonal[4][2] = {{-1,-1},{-1,1},{1,-1},{1,1}},visit[25][25];
int m,n,count1;
char map1[25][25];

void dfs(int x,int y)
{
    int i,newx,newy;
    visit[x][y] = 1;
    for(i=0; i<4; i++)//上下左右搜
    {
        newx = x + dir[i][0];
        newy = y + dir[i][1];
        if(map1[newx][newy] == 'X' && visit[newx][newy] == 0)
            dfs(newx,newy);
        else if(map1[newx][newy] == '.')
            count1++;
    }
    for(i=0; i<4; i++)//对角线搜
    {
        newx = x + diagonal[i][0];
        newy = y + diagonal[i][1];
        if(map1[newx][newy] == 'X' && visit[newx][newy] == 0)
            dfs(newx,newy);
    }
}

int main()
{
    int i,j;
    int click_x,click_y;
    while(scanf("%d%d%d%d",&m,&n,&click_x,&click_y))
    {
        count1 = 0;
        if(m + n + click_x + click_y == 0)
            break;
        memset(map1,'.',sizeof(map1));//在外面围一圈.便于判断
        memset(visit,0,sizeof(visit));
        for(i=1; i<=m; i++)
        {
            for(j=1; j<=n; j++)
            {
                cin>>map1[i][j];
            }
        }
        dfs(click_x,click_y);
        printf("%d\n",count1);
    }
    return 0;
}


你可能感兴趣的:(ZOJ 1047 Image Perimeters)