![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//*
ID: lorelei3
TASK: ttwo
LANG: C++
*/
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
#include <fstream>
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
using namespace std;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
ifstream fin;
ofstream fout;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
const int MAX = 10000000;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
int dx[] =
{ -1, 0, 1, 0 };
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
int dy[] =
{ 0, 1, 0, -1 };
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int cd=0, cx, cy;
int fd=0, fx, fy;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int map[10][10];
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
int main()
{
int i,j;
char ch;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
fin.open("ttwo.in");
fout.open("ttwo.out");
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
for(i=0; i<10; i++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for(j=0; j<10; j++)
{
fin>>ch;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if(ch=='*')
{
map[i][j]=1;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
}else if(ch=='C')
{
cx=i, cy=j;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
}else if(ch=='F')
{
fx=i, fy=j;
}
}
int step=0;
int x,y;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
while(step!=MAX)
{
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
// for cows
x = cx+dx[cd];
y = cy+dy[cd];
if(map[x][y]==1 || x<0 || x>9 || y<0 || y>9)
cd = (cd+1)%4;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else
{
cx = x;
cy = y;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
// for farmer
x = fx+dx[fd];
y = fy+dy[fd];
if(map[x][y]==1 || x<0 || x>9 || y<0 || y>9)
fd = (fd+1)%4;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else
{
fx = x;
fy = y;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
step++;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
//compare
if(cx==fx && cy==fy)
break;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if(step == MAX)
fout<<0<<endl;
else
fout<<step<<endl;
return 0;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
posted on 2010-12-08 11:18
小阮 阅读(216)
评论(0) 编辑 收藏 引用 所属分类:
USACO