USACO 2.4.3 Overfencing

第一次接触flood fill,不过做完才发现原来这就是所谓的flood fill...我怎么觉的就是土土BFS呢....

 

 

我的思路是找到2个出口,做2次BFS,更新每一格的值,对于每一个格来说取最小的,最后取所有的格子中的最大值就可以了...还是不能理解flood fill是啥~标程怎么写的也和BFS长那么象~

不管如何总算AC了...

呵呵现在最喜欢的就是看到下面这种情况了..

Executing...
Test 1: TEST OK [0.011 secs, 3124 KB]
Test 2: TEST OK [0.000 secs, 3124 KB]
Test 3: TEST OK [0.000 secs, 3124 KB]
Test 4: TEST OK [0.022 secs, 3120 KB]
Test 5: TEST OK [0.000 secs, 3120 KB]
Test 6: TEST OK [0.000 secs, 3124 KB]
Test 7: TEST OK [0.011 secs, 3124 KB]
Test 8: TEST OK [0.011 secs, 3124 KB]
Test 9: TEST OK [0.011 secs, 3124 KB]
Test 10: TEST OK [0.011 secs, 3120 KB]

All tests OK.

下面是我丑陋的代码~写了老长...

 

 

 

/*
ID: chenh193
PROG: maze1
LANG: C++
*/
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int N,M;
vector<string> mat;
int hash[201][201];
char buf[201];
const int dir[4][2]={1,0,-1,0,0,1,0,-1};
int l,r;
struct Rec
{
    int x,y,step;
}Q[100*100],ins[2],top,tmp;
int main()
{
    int i,j,ilen=0,idx,ans;
    freopen("maze1.in","r",stdin);
    freopen("maze1.out","w",stdout);
    cin>>M>>N;
    getchar();
    for(i=0;i<=2*N;i++)
    {
        gets(buf);
        mat.push_back(buf);
    }
    //find exit axis
    for(i=1;i<=2*N-1;i+=2)
        for(j=1;j<=2*M-1;j+=2)
            hash[i][j]=0x7fffffff;
    for(i=1;i<=2*M-1;i+=2)
    {
        if(mat[0][i]==' ')
        {
            ins[ilen].x=1;
            ins[ilen].y=i;
            ins[ilen].step=1;
            ilen++;
        }
        if(mat[2*N][i]==' ')
        {
            ins[ilen].x=2*N-1;
            ins[ilen].y=i;
            ins[ilen].step=1;
            ilen++;       
        }
    }
    for(i=1;i<=2*N-1;i+=2)
    {
        if(mat[i][0]==' ')
        {
            ins[ilen].x=i;
            ins[ilen].y=1;
            ins[ilen].step=1;
            ilen++;
        }
        if(mat[i][2*M]==' ')
        {
            ins[ilen].x=i;
            ins[ilen].y=2*M-1;
            ins[ilen].step=1;
            ilen++;       
        }
    }
    for(idx=0;idx<ilen;idx++)
    {
        l=r=0;
        Q[r++]=ins[idx];
        while(l<=r)
        {
            top=Q[l++];
            top.step++;
            for(i=0;i<4;i++)
            {
                tmp=top;
                tmp.x+=dir[i][0];
                tmp.y+=dir[i][1];
                if(tmp.x<0||tmp.y<0||tmp.x>2*N||tmp.y>2*M||mat[tmp.x][tmp.y]!=' ')continue;
                tmp.x+=dir[i][0];
                tmp.y+=dir[i][1];
                if(tmp.x<0||tmp.y<0||tmp.x>2*N||tmp.y>2*M)continue;
                if(tmp.step<hash[tmp.x][tmp.y])
                    hash[tmp.x][tmp.y]=tmp.step;
                else
                    continue;
                Q[r++]=tmp;
            }
        }
    }
    ans=0;
    for(i=1;i<=2*N-1;i+=2)
        for(j=1;j<=2*M-1;j+=2)
            if(hash[i][j]>ans)
                ans=hash[i][j];
    printf("%d/n",ans);
    return 0;
}

你可能感兴趣的:(c)