第五个点WA了,为啥啊?求助
查看原帖
第五个点WA了,为啥啊?求助
973361
ly111222楼主2023/7/8 17:36
#include<bits/stdc++.h>
using namespace std;
char mp[11][11];
set<vector<int> > obstacles;
void move(vector<int>& pos, int& turn){
    if(turn==0){
        if(pos[0]==1){//遇到边界
            turn=1;
        }
        else{
            pos[0]--;
            if(obstacles.count(pos)){//碰见障碍
                turn=1;
                pos[0]++;//回退
            }
        }
    }
    else if(turn==1){
        if(pos[1]==10){//遇到边界
            turn=2;
        }
        else{
            pos[1]++;
            if(obstacles.count(pos)){//碰见障碍
                turn=2;
                pos[1]--;//回退
            }
        }
    }
    else if(turn==2){
        if(pos[0]==10){//遇到边界
            turn=3;
        }
        else{
            pos[0]++;
            if(obstacles.count(pos)){//碰见障碍
                turn=3;
                pos[0]--;//回退
            }
        }
    }
    else if(turn==3){
        if(pos[1]==1){//遇到边界
            turn=0;
        }
        else{
            pos[1]--;
            if(obstacles.count(pos)){//碰见障碍
                turn=0;
                pos[1]++;//回退
            }
        }
    }
}
int main(){
    vector<int> cpos(2);
    vector<int> fpos(2);
    for(int i=1;i<=10;i++){
        for(int j=1;j<=10;j++){
            cin >> mp[i][j];
            if(mp[i][j] == '*'){
                vector<int> tmp(2);
                tmp[0] = i;
                tmp[1] = j;
                obstacles.insert(tmp);
            }
            if(mp[i][j] == 'C'){
                cpos[0] = i;
                cpos[1] = j;
            }
            if(mp[i][j] == 'F'){
                fpos[0] = i;
                fpos[1] = j;
            }
        }
    }
    set<vector<int> > ctrace;
    set<vector<int> > ftrace;
    int cturn=0,fturn=0;//0,1,2,3-->上,右,下,左
    int cnt=0;
    ctrace.insert({cpos[0],cpos[1],cturn});
    ftrace.insert({fpos[0],fpos[1],fturn});
    while(1){
        move(cpos,cturn);
        move(fpos,fturn);
        cnt++;
        if(cpos==fpos){
            cout << cnt;
            break;
        }
        if(!ctrace.insert({cpos[0],cpos[1],cturn}).second 
        && !ftrace.insert({fpos[0],fpos[1],fturn}).second){
            cout << 0;
            break;
        }
    }
    return 0;
}
2023/7/8 17:36
加载中...