题194.洛谷P1518 [USACO2.4]两只塔姆沃斯牛 The Tamworth Two
一、题目
二、题解
本题没什么好说的,记录一下自己咋犯病的
#include <bits/stdc++.h>
using namespace std;
const int Inf=200000;
const int dx[4]={-1,0,1,0};//{0,1,0,-1};我可能真的疯了,我当时以为向上走是x不变,y加1,那样就变成列数加1,向右走了,唉,烦死了
const int dy[4]={0,1,0,-1};//{1,0,-1,0};
char field[100][100];
int main()
{
pair<int,int> F,C;
fill(field[0],field[0]+100*100,'*');
for(int i=1;i<=10;i++)
{
for(int j=1;j<=10;j++)
{
cin>>field[i][j];
if(field[i][j]=='F')
{
field[i][j]='.';
F.first=i;
F.second=j;
}
else if(field[i][j]=='C')
{
field[i][j]='.';
C.first=i;
C.second=j;
}
}
}
int i=0;
int dF=0,dC=0;
while(F!=C)
{
if(field[F.first+dx[dF]][F.second+dy[dF]]=='*')
{
dF=(dF+1)%4;
}
else
{
F.first+=dx[dF];
F.second+=dy[dF];
}
if(field[C.first+dx[dC]][C.second+dy[dC]]=='*')
{
dC=(dC+1)%4;
}
else
{
C.first+=dx[dC];
C.second+=dy[dC];
}
i++;
if(i==Inf)
{
break;
}
}
if(i==Inf)
{
cout<<"0"<<endl;
}
else
{
cout<<i<<endl;
}
}