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

  • POJ 4001 #注意边界问题
#include 
#include 
#include 
using namespace std;

int n, m;
int vis[200005];

struct data{
    int x;
    int step;
};

int BFS(){
    vis[n] = 1;
    queueq;
    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 
#include 
#include 
#include 
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(){
    queueq;
    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=0 && v.y
  • POJ 4115  #地图上相同的点,携带不同数目的查克拉的状态是不一样的,所以vis数组是三维
#include 
#include 
#include 
#include 
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(){
    queueq;
    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=0 && v.y 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
  • POJ 4129 #每个点都有多个状态 vis是三维
#include
#include
#include
#include
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(){

    queueq;
    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

 

你可能感兴趣的:(bfs,BFS,算法)