求教大佬,我的指针为什么是错的。
查看原帖
求教大佬,我的指针为什么是错的。
67403
Carrot_killer楼主2021/3/7 21:33
#include<iostream>
#include<cstring>
using namespace std;
bool map[11][11];
struct pos{
    int x,y;
    int dire;
};
int dx[5]={0,0,1,0,-1};//顺序:上右下左
int dy[5]={0,1,0,-1,0};
pos cow,farmer;
pos first_cow,first_farmer;
bool equal_(pos a,pos b){
    if(a.dire==b.dire&&a.x==b.x&&a.y==b.y) return true;
    else return false;
}
void check_dire(pos *k){
    int nowx=k->x+dx[k->dire];
    int nowy=k->y+dy[k->dire];
    if(nowx<=0||nowy<=0||nowx>10||nowy>10||map[nowx][nowy]==0)//如果撞到墙了,或者不能走
    k->dire=(k->dire-1)%4+1;//换方向
    else{//不换方向就前进
        k->x=nowx;
        k->y=nowy;
    }
}
void search(int time){
    if(cow.x==farmer.x&&cow.y==farmer.y){
        cout<<time;
        return;
    }
    /*if(time!=0&&equal_(farmer,first_farmer)&&equal_(cow,first_cow)){
        cout<<0;
        return;
    }*/
    check_dire(&cow);
    check_dire(&farmer);//检查两者的方向是否正确,并且前进
    search(time+1);
    return;
    
}
int main(){
    memset(map,1,sizeof(map));
    char c;
    for(int i=1;i<=10;i++){
        for(int j=1;j<=10;j++){
            cin>>c;
            switch(c){
                case '*':
                map[i][j]=0;break;
                case '.':
                case 'C':
                cow.dire=1;cow.x=i;cow.y=j;break;
                case 'F':
                farmer.dire=1;farmer.x=i;farmer.y=j;break;
            }
        }
    }
    first_cow=cow;
    first_farmer=farmer;
    search(0);
    system("pause");
    return 0;
}
2021/3/7 21:33
加载中...