我也是这样,不知道为啥捏
by bhkb2561 @ 2023-12-24 17:29:34
我也是 :(\
而且我代码还厌氧 (\
把这托答辩呈给楼主罢()
```c
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
int map[10][10];//The overall map
typedef struct position{
int row;
int col;
int dir;//0North 1East 2South 3West
}pos;
pos co={0,0,0};
pos fa={0,0,0};
void readInitialPositions();
void cowsMarch();
void farmerSeek();
bool inRange(int k);
int main(){
readInitialPositions();
bool caught=false;
int mins=0;
while(mins<=114514){
cowsMarch();
farmerSeek();
++mins;
if(co.row==fa.row&&co.col==fa.col){ caught=true;break; }
}
printf("%d", ((caught)?mins:0) );
return 0;
}
bool inRange(int k){
if(k>=0&&k<10){return true;}
else{return false;}
}
void readInitialPositions(){
char bufferSheild;
char recv;
for(int i=0;i<10;++i){//row
for(int j=0;j<10;++j){//col
recv=getchar();
if(recv=='.'){ map[i][j]=0; }// 0 for empty
else if(recv=='*'){map[i][j]=-1;}// -1 for obstacles
else if(recv=='F'){map[i][j]=0; fa.row=i;fa.col=j;}//enter in farmers
else if(recv=='C'){map[i][j]=0; co.row=i;co.col=j;}//enter in the cows
}
bufferSheild=getchar();//To absorb the input '\n'
}
return ;
}
void cowsMarch(){//do a step if called once
pos fwd={0,0,0};//将要到达的坐标
if(co.dir==0){fwd.col=co.col; fwd.row=co.row-1;}
else if(co.dir==1){fwd.col=co.col+1; fwd.row=co.row;}
else if(co.dir==2){fwd.col=co.col; fwd.row=co.row+1;}
else if(co.dir==3){fwd.col=co.col-1; fwd.row=co.row;}
if((!inRange(fwd.row))||(!inRange(fwd.col))||map[fwd.row][fwd.col]==-1){//illegal
co.dir=(co.dir+1)%4;// spinning 90 degs clockwise
}
else{//legal
co.row=fwd.row;co.col=fwd.col;
}
return ;
}
void farmerSeek(){
pos fwd={0,0,0};//将要到达的坐标
if(fa.dir==0){fwd.col=fa.col; fwd.row=fa.row-1;}
else if(fa.dir==1){fwd.col=fa.col+1; fwd.row=fa.row;}
else if(fa.dir==2){fwd.col=fa.col; fwd.row=fa.row+1;}
else if(fa.dir==3){fwd.col=fa.col-1; fwd.row=fa.row;}
if((!inRange(fwd.row))||(!inRange(fwd.col))||map[fwd.row][fwd.col]==-1){//illegal
fa.dir=(fa.dir+1)%4;// spinning 90 degs clockwise
}
else{//legal
fa.row=fwd.row;fa.col=fwd.col;
}
return ;
}
``````
by Grabprintf @ 2023-12-27 16:40:29
不要用getchar()输入
by cxkky @ 2024-01-08 14:48:16
你把O2优化关一下试试。
by hu2265005977 @ 2024-02-03 22:13:46