USACO 2.4 The Tamworth Two


模拟题。按题目要求编码即可,每过一分钟,更新一下农夫和奶牛的状态。如果该状态以前出现过,说明有循环,不可能到达,输出0.

#include <iostream>
#include 
<fstream>

using namespace std;

ifstream fin(
"ttwo.in");
ofstream fout(
"ttwo.out");

#ifdef _DEBUG
#define out cout
#define in cin
#else
#define out fout
#define in fin
#endif

char grid[10][10];

bool visited[10][10][4][10][10][4];

int north = 0;
int east = 1;
int south = 2;
int west = 3;

//方向为0,1,2,3时向前走一步row和column的增加值
int delr[4= {
    
-1,0,1,0
};
int delc[4= {
    
0,1,0,-1
};

// farmer的当前row,column,direction.
int fc,fr,fd;
int cc,cr,cd;

bool available(int i,int j)
{
    
if(i>=0&&i<=9&&j>=0&&j<=9&&grid[i][j]!='*')
        
return true;
    
else
        
return false;
}

void one_step()
{
    
if( available( fr+delr[fd],fc+delc[fd] ) ){
        fr 
+= delr[fd];
        fc 
+= delc[fd];
    }
else{
        fd
+=1;
        fd
%=4;
    }

    
if( available( cr+delr[cd],cc+delc[cd] ) ){
        cr 
+= delr[cd];
        cc 
+= delc[cd];
    }
else{
        cd
+=1;
        cd
%=4;
    }
}

void solve()
{
    
for(int i=0;i<10;++i)
        
for(int j=0;j<10;++j){
            
in>>grid[i][j];
            
if(grid[i][j]=='F')
                fr
=i,fc=j,fd=0;
            
if(grid[i][j]=='C')
                cr
=i,cc=j,cd=0;
        }

    memset(visited,
0,sizeof(visited));

    visited[fc][fr][fd][cc][cr][cd] 
= true;

    
int res = 0;

    
while(true){

        one_step();
        res
++;

        
if( fc==cc&&fr==cr ){
            
out<<res<<endl;
            exit(
0);
        }

        
if(visited[fc][fr][fd][cc][cr][cd]){
            
out<<0<<endl;
            exit(
0);
        }

        visited[fc][fr][fd][cc][cr][cd]
=true;
    }
}

int main(int argc,char *argv[])
{
    solve(); 
    
return 0;
}


posted on 2009-06-26 20:48 YZY 阅读(885) 评论(0)  编辑 收藏 引用 所属分类: AlgorithmUSACO


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


导航

<2009年7月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

统计

常用链接

留言簿(2)

随笔分类

随笔档案

搜索

积分与排名

最新评论

阅读排行榜