2010年2月23日 星期二

UVa 10102 The path in the colored field

7772863      10102      The path in the...      poao899      Accepted      C++      0.864      2010-02-23 14:56:10                                           .


題目意義有一點點不好懂囧

看了好久好久



//*********************************************************

#include<stdio.h>
#define QLEN 20000
int map[100][100],step[100][100],m,max,min;
char input[110];
struct queue{
    int front,rear,a[QLEN][2];
    void init(){
        front=0;
        rear=0;
    }
    void push(int x,int y,int dep){
        if(x>=0&&x<m&&y>=0&&y<m&&step[x][y]==0){
            //printf("push %d %d:%d\n",x,y,dep);
            step[x][y]=dep;
            a[rear][0]=x;
            a[rear][1]=y;
            if(map[x][y]==3 && min>dep){
                min=dep;
            }
            rear=++rear%QLEN;
        }
    }
    bool pop(){
        if(front==rear)return 0;
        int x=a[front][0],y=a[front][1],d=step[x][y]+1;
        push(x+1,y,d);
        push(x-1,y,d);
        push(x,y+1,d);
        push(x,y-1,d);
        front=++front%QLEN;
        return 1;
    }
}q;
int main(){
    while(~scanf("%d\n",&m)){
        //Init
        max=-1;
        //Input
        for(int i=0;i<m;i++){
            gets(input);
            for(int j=0;j<m;j++){
                map[i][j]=input[j]-'0';
            }
        }
        //BFS & Count
        for(int i=0;i<m;i++)
            for(int j=0;j<m;j++)
                if(map[i][j]==1){
                    min=2147483647;
                    for(int i=0;i<m;i++)
                        for(int j=0;j<m;j++)
                            step[i][j]=0;
                    //printf("start: %d %d\n",i,j);
                    q.init();
                    q.push(i,j,1);
                    //puts("BFS");
                    while(q.pop());
                    //printf("min%d\n",min);
                    if(max<min)max=min;
                }
        //Output
        printf("%d\n",max-1);
    }
}


沒有留言:

張貼留言