算法复习 BFS 习题整理 POJ4001、4116、4115、4127、4129

  • POJ 4001 #注意边界问题
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;

int n, m;
int vis[200005];

struct data{
    int x;
    int step;
};

int BFS(){
    vis[n] = 1;
    queue<data>q;
    while(!q.empty()) q.pop();
    data u;
    u.x = n;
    u.step = 0;
    q.push(u);
    while(!q.empty()){
        u = q.front();
        q.pop();
        if(u.x == m) return u.step;
        data v;
        for(int i=0; i<3; i++){
            if(i == 0)
                v.x = u.x+1;

            if(i == 1)
                v.x = u.x-1;

            if(i == 2)
                v.x = u.x*2;

            v.step = u.step+1;
            if(v.x >= 0&& v.x < 200000 && !vis[v.x]){
                vis[v.x] = 1;
                q.push(v);
            }
        }
    }
    return 0;
}

int main()
{
    scanf("%d %d", &n, &m); //n人的位置 m牛的位置
    memset(vis, 0, sizeof(vis));
    int ans = BFS();
    printf("%d", ans);
    return 0;
}
  • POJ 4116 #注意杀死守卫耗时2,要特殊处理
#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
using namespace std;

char Map[201][201];
char vis[201][201];

int n,m,xa,ya,xr,yr;

int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};

struct data{
    int x,y;
    int step;
    int flag;
    data(int a,int b, int c, int d):x(a),y(b),step(c),flag(d){}
    data(){}
};

data u,v;

int BFS(){
    queue<data>q;
    q.push(data(xr,yr,0,0));
    while(!q.empty()){
        u = q.front();
        q.pop();
        if(u.flag) {q.push(data(u.x,u.y,u.step+1,0)); continue;}
        if(Map[u.x][u.y] == 'a') return u.step;
        for(int i=0; i<4; i++){
            v.x = u.x+dx[i];
            v.y = u.y+dy[i];

            if(v.x>=0 && v.x<n && v.y>=0 && v.y<m && !vis[v.x][v.y]){
                if(Map[v.x][v.y] == '@' || Map[v.x][v.y] == 'a') q.push(data(v.x,v.y,u.step+1,0));
                if(Map[v.x][v.y] == 'x') q.push(data(v.x,v.y,u.step+1,1));
                vis[v.x][v.y] = 1;
            }
        }
    }
    return 0;
}

int main()
{
    int s;
    scanf("%d", &s);
    while(s--){
        scanf("%d %d\n", &n, &m);
        for(int i = 0; i < n; i++)
			scanf("%s",Map[i]);
        for(int i=0; i<n; i++)
            for(int j=0; j<m; j++){
                vis[i][j] = 0;
                if(Map[i][j] == 'r'){ xr=i, yr=j; vis[i][j] = 1;}
            }
            int ans = BFS();

            if(ans == 0) printf("Impossible\n");
            else printf("%d\n", ans);
    }
    return 0;
}
  • POJ 4115  #地图上相同的点,携带不同数目的查克拉的状态是不一样的,所以vis数组是三维
#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
using namespace std;

char Map[201][201];
int vis[201][201][11];

int n,m,t,xr,yr;

int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};

struct data{
    int x,y;
    int t;
    int step;
    data(int a,int b, int c, int d):x(a),y(b),t(c),step(d){}
    data(){}
};

data u,v;

int BFS(){
    queue<data>q;
    q.push(data(xr,yr,t,0));
    while(!q.empty()){
        u = q.front();
        q.pop();
        if(Map[u.x][u.y] == '+') return u.step;
        for(int i=0; i<4; i++){
            v.x = u.x+dx[i];
            v.y = u.y+dy[i];

            if(v.x>=0 && v.x<n && v.y>=0 && v.y<m && !vis[v.x][v.y][u.t]){ //不超边界并且没访问过
                if(Map[v.x][v.y] == '*' || Map[v.x][v.y] == '+'){
                        q.push(data(v.x, v.y, u.t, u.step+1));
                }
                if(Map[v.x][v.y] == '#' && u.t > 0){ ///大蛇丸的手下 并且能打过
                    //Map[v.x][v.y] = '*';
                    q.push(data(v.x, v.y, u.t-1, u.step+1));
                    vis[v.x][v.y][u.t-1] = 1;
                }
                vis[v.x][v.y][u.t] = 1;
            }
        }
    }
    return -1;
}

int main()
{
    scanf("%d %d %d\n", &n, &m, &t);
    for(int i = 0; i < n; i++)
        scanf("%s",Map[i]);
    memset(vis, 0, sizeof(vis));
    for(int i=0; i<n; i++)
        for(int j=0; j<m; j++){
            if(Map[i][j] == '@'){ xr=i, yr=j; vis[i][j][t] = 1;}
        }
    int ans = BFS();
    printf("%d\n", ans);
    return 0;
}
  • POJ 4129 #每个点都有多个状态 vis是三维
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;

int vis[105][105][15];
char Map[105][105];
int n, m, k, x, y;
int xx[4]={0,0,1,-1};
int yy[4]={1,-1,0,0};

struct data{
    int x,y;
    int step;
    data(int a, int b, int c):x(a),y(b),step(c){}
    data(){}
}u,v;

int BFS(){

    queue<data>q;
    while(!q.empty()) q.pop();
    q.push(data(x, y, 0));
    while(!q.empty()){
        u = q.front();
        q.pop();
        for(int i=0; i<4; i++){
            v.x = u.x + xx[i];
            v.y = u.y + yy[i];
            v.step = u.step+1;
            if(v.x<0 || v.y<0 || v.x>=n || v.y>=m) continue;
            if(Map[v.x][v.y] == 'E') return v.step;

            if(Map[v.x][v.y] == 'S' && !vis[v.x][v.y][v.step%k]){
                vis[v.x][v.y][v.step%k] = 1;
                q.push(v);
            }
            if(Map[v.x][v.y] == '.' && !vis[v.x][v.y][v.step%k]){
                vis[v.x][v.y][v.step%k] = 1;
                q.push(v);
            }
            if(Map[v.x][v.y] == '#' && !vis[v.x][v.y][v.step%k] && v.step%k == 0){
                vis[v.x][v.y][v.step%k] = 1;
                q.push(v);
            }
        }
    }
    return -1;

}

int main(){

    int c;
    scanf("%d", &c);
    while(c--){
        scanf("%d %d %d", &n, &m, &k);
        for(int i=0; i<n; i++)
            scanf("%s", Map[i]);
        memset(vis, 0, sizeof(vis));
        for(int i=0; i<n; i++)
        for(int j=0; j<m; j++){
            if(Map[i][j] == 'S'){
                x = i;
                y = j;
                vis[i][j][0] = 1;
            }
        }
        

        int ans = BFS();
        if(ans == -1) printf("Oop!\n");
        else printf("%d\n", ans);

        //for(int i=0; i<n; i++)
            //printf("%s", Map[i]);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值