#26094: 找不到錯誤


yutang0323@gmail.com (阿爾笛)

學校 : 不指定學校
編號 : 104861
來源 : [114.37.17.166]
最後登入時間 :
2021-07-15 21:07:45
b059. 4. 靈犬尋寶 -- 95學年度全國資訊學科能力競賽 | From: [114.37.17.166] | 發表日期 : 2021-07-15 21:17

//bfs 靈犬尋寶
#include<iostream>
#include<queue>
#include<utility>
#include<cstring>
using namespace std;

int world[100][100]={}; // 需要注意左下為(0,0) 橫坐標第一個
int step[100][100];
int dx[4] = {1,-1,0,0};
int dy[4] = {0,0,1,-1};
int gx[8] = {3,1,-3,-3,1,-1,1,-1};
int gy[8] = {3,-1,1,-1,3,3,-3,-3};
bool flag;
pair<int,int>final;
pair<int,int>start;

void bfs(){
    step[start.first][start.second]=0;
    queue<pair<int,int> >q;
    q.push(start);
    while(!q.empty()){
        pair<int,int> now =q.front();
        q.pop();
        int row = now.first;
        int col = now.second;

        //確認是否是寶物
        if(row==final.first && col==final.second){
            cout<<step[row][col]<<endl;
            flag=1;
            break;
        }


        for(int i=0;i<4;i++){

            //確認周邊障礙物
            int new_row = row + dx[i];
            int new_col = col + dy[i];
            if(new_row >99 || new_row<0 || new_col >99 || new_col<0) continue;//會不會超出
            if(world[new_row][new_col]) continue; //檢查周邊是不是障礙物

            //開始走路
            for(int j=i;j<i+2;j++){
                new_row = row + gx[j];
                new_col = col + gy[j];
                if(new_row >99 || new_row<0 || new_col >99 || new_col<0) continue;
                if(step[new_row][new_col]!=-1) continue;
                if(world[new_row][new_col]) continue;
                step[new_row][new_col] = step[row][col]+1;
                q.push({new_row,new_col});
            }
        }

    }
    if(!flag) cout<<"impossible\n";
}



int main(){
    //input前面先處理座標方向問題
    int n;//障礙物個數
    while(cin>>n){
        memset(step,-1,sizeof(step));
        memset(world,0,sizeof(world));
        flag=0;
        for(int i=0;i<n;i++){
            int x,y;
            cin>>y>>x;
            x=99-x-1;
            world[x][y]=1;
        }
        cin>>start.second>>start.first;
        start.first = 99-start.first-1;
        cin>>final.second>>final.first;
        final.first = 99 - final.first-1;
        bfs();
    }
}

 
#26095: Re:找不到錯誤


yutang0323@gmail.com (阿爾笛)

學校 : 不指定學校
編號 : 104861
來源 : [114.37.17.166]
最後登入時間 :
2021-07-15 21:07:45
b059. 4. 靈犬尋寶 -- 95學年度全國資訊學科能力競賽 | From: [114.37.17.166] | 發表日期 : 2021-07-15 21:24

//bfs 靈犬尋寶
#include
#include
#include
#include
using namespace std;

int world[100][100]={}; // 需要注意左下為(0,0) 橫坐標第一個
int step[100][100];
int dx[4] = {1,-1,0,0};
int dy[4] = {0,0,1,-1};
int gx[8] = {3,1,-3,-3,1,-1,1,-1};
int gy[8] = {3,-1,1,-1,3,3,-3,-3};
bool flag;
pair<int,int>final;
pair<int,int>start;

void bfs(){
    step[start.first][start.second]=0;
    queue<pair<int,int> >q;
    q.push(start);
    while(!q.empty()){
        pair<int,int> now =q.front();
        q.pop();
        int row = now.first;
        int col = now.second;

        //確認是否是寶物
        if(row==final.first && col==final.second){
            cout<<step[row][col]<<endl;
            flag=1;
            break;
        }


        for(int i=0;i<4;i++){

            //確認周邊障礙物
            int new_row = row + dx[i];
            int new_col = col + dy[i];
            if(new_row >99 || new_row99 || new_col<0) continue;//會不會超出
            if(world[new_row][new_col]) continue; //檢查周邊是不是障礙物

            //開始走路
            for(int j=i;j<i+2;j++){
                new_row = row + gx[j];
                new_col = col + gy[j];
                if(new_row >99 || new_row99 || new_col<0) continue;
                if(step[new_row][new_col]!=-1) continue;
                if(world[new_row][new_col]) continue;
                step[new_row][new_col] = step[row][col]+1;
                q.push({new_row,new_col});
            }
        }

    }
    if(!flag) cout<<"impossible\n";
}



int main(){
    //input前面先處理座標方向問題
    int n;//障礙物個數
    while(cin>>n){
        memset(step,-1,sizeof(step));
        memset(world,0,sizeof(world));
        flag=0;
        for(int i=0;i<n;i++){
            int x,y;
            cin>>y>>x;
            x=99-x-1;
            world[x][y]=1;
        }
        cin>>start.second>>start.first;
        start.first = 99-start.first-1;
        cin>>final.second>>final.first;
        final.first = 99 - final.first-1;
        bfs();
    }
}


求測資一

 
ZeroJudge Forum