无敌的奶牛 • 1个月前
不食,你这样例包有问题的啊,我写的代码输出-1过了,49过不了,我嘞个你有多少错的样例啊?!
评论:
#include<bits/stdc++.h>
using namespace std;
char a[15][15];
int i,j,f1,f2,c1,c2,tf1=-1,tf2=0,tc1=-1,tc2=0,t,k;
bool ch;
int main()
{
scanf("%d",&t);
for(k=1;k<=t;k++)
{
ch=false;
for(i=1;i<=10;i++)
{
for(j=1;j<=10;j++)
{
cin>>a[i][j];
if(a[i][j]=='C')
{
f1=i;
f2=j;
}
else if(a[i][j]=='M')
{
c1=i;
c2=j;
}
}
}
tf1=-1,tf2=0,tc1=-1,tc2=0;
for(i=1;i<=100;i++)
{
if(f1+tf1>=1&&f1+tf1<=10&&f2+tf2>=1&&f2+tf2<=10&&a[f1+tf1][f2+tf2]!='*')
{
f1+=tf1;
f2+=tf2;
}
else
{
if(tf1==-1&&tf2==0)
{
tf1=0;
tf2=1;
}
else if(tf1==0&&tf2==1)
{
tf1=1;
tf2=0;
}
else if(tf1==1&&tf2==0)
{
tf1=0;
tf2=-1;
}
else if(tf1==0&&tf2==-1)
{
tf1=-1;
tf2=0;
}
}
if(c1+tc1>=1&&c1+tc1<=10&&c2+tc2>=1&&c2+tc2<=10&&a[c1+tc1][c2+tc2]!='*')
{
c1+=tc1;
c2+=tc2;
}
else
{
if(tc1==-1&&tc2==0)
{
tc1=0;
tc2=1;
}
else if(tc1==0&&tc2==1)
{
tc1=1;
tc2=0;
}
else if(tc1==1&&tc2==0)
{
tc1=0;
tc2=-1;
}
else if(tc1==0&&tc2==-1)
{
tc1=-1;
tc2=0;
}
}
if(f1==c1&&f2==c2)
{
printf("%d\n",i);
ch=true;
break;
}
}
if(!ch)printf("-1\n");
}
return 0;
}
using namespace std; int n,mx,my,cx,cy; char mp[1001][1001]; int idm=0,idc=0,sum; int fx[4]={-1,0,1,0},fy[4]={0,1,0,-1}; void dfs(int ax,int ay,int bx,int by){
while(sum<100){
int dax=ax+fx[idm],day=ay+fy[idm],dbx=bx+fx[idc],dby=by+fy[idc];
if(mp[dax][day]=='*'||dax>10||day>10||dax<=0||day<=0)idm=(idm+1)%4;
else ax=dax,ay=day;
if(mp[dbx][dby]=='*'||dbx>10||dby>10||dbx<=0||dby<=0)idc=(idc+1)%4;
else bx=dbx,by=dby;
if(ax==bx&&ay==by){
cout<<sum<<endl;
return;
}
sum++;
}
cout<<-1<<endl;
return;
} int main(){
cin>>n;
for(int i=1;i<=n;i++){
for(int j=1;j<=10;j++){
for(int k=1;k<=10;k++){
cin>>mp[j][k];
if(mp[j][k]=='M')mx=j,my=k,mp[j][k]='.';
if(mp[j][k]=='C')cx=j,cy=k,mp[j][k]='.';
}
}
idc=idm=0;
sum=1;
dfs(mx,my,cx,cy);
}
return 0;
} //这样也能过