2013年12月12日 星期四

[USACO] 2-4-1 The Tamworth Two


/*
ID: 551100k1
LANG: C++
TASK: ttwo
*/
#include<stdio.h>
int d[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
char s[15][15]={0};
int flag=0,ans;
int main(){
    freopen("ttwo.in","r",stdin);
    freopen("ttwo.out","w",stdout);
    int i,j,ax,ay,bx,by,ad=0,bd=0;
    for(i=1;i<=10;i++){
        scanf("%s",&s[i][1]);
        for(j=1;j<=10;j++){
            if(s[i][j]=='F') ax=i,ay=j,s[i][j]='.';
            if(s[i][j]=='C') bx=i,by=j,s[i][j]='.';
        }
    }
    for(i=1;i<=1000000;i++){
        if(s[ax+d[ad][0]][ay+d[ad][1]]=='.')
            ax+=d[ad][0],ay+=d[ad][1];
        else ad=(ad+1)%4;
        if(s[bx+d[bd][0]][by+d[bd][1]]=='.')
            bx+=d[bd][0],by+=d[bd][1];
        else bd=(bd+1)%4;
        if(ax==bx && ay==by){
            flag=1,ans=i;
            break;
        }
    }
    if(flag) printf("%d\n",ans);
    else puts("0");
}

沒有留言:

張貼留言