HDU 2102A计划

来源:互联网 发布:遗传算法解决优化问题 编辑:程序博客网 时间:2024/05/22 17:30

题目有几个值得注意的地方:

1、在T时间内找到公主是输出YES的;

2、当map1[i][j]=='#,map2[i][j]=='#'时是会一直处于传输阵中的,所以这种情况是不被允许的;

3、当map1[i][j]=='#或map2[i][j]=='#,而另一个为‘*’时,也是不被允许的。

所以,程序要经过预处理;  

预处理代码:

    for(int i=0; i<n; i++) {
            for(int j=0; j<m; j++) {
                if (map1[i][j]=='#'&&map2[i][j]=='*') {
                    map1[i][j]='*';
                } else if (map1[i][j]=='#'&&map2[i][j]=='#') {
                    map1[i][j]=map2[i][j]='*';
                } else if (map2[i][j]=='#'&& map1[i][j]=='*') {
                    map2[i][j]='*';
                }
            }
        }

总的代码:我是用优先队列做的

#pragma comment(linker, "/STACK:36777216")
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std;
int n,m;
struct node {
    int x,y;
    int step;
    int pos;
    friend bool operator < (node a,node b) {
        return a.step>b.step;
    };
};


int dir_x[4]= {-1,0,1,0};
int dir_y[4]= {0,1,0,-1};
int ans;
bool judge(node a) {
    if(a.x<0||a.x>=n||a.y<0||a.y>=m) {
        return false;
    }
    return true;
}
char map1[15][15];
char map2[15][15];
bool vis[3][15][15];
bool bfs(int pos,int x,int y) {
    priority_queue<node>que;
    memset(vis,false,sizeof(vis));
    node p1;
    p1.x=x;
    p1.y=y;
    p1.step=0;
    p1.pos=pos;
    que.push(p1);
    vis[p1.pos][p1.x][p1.y]=true;
    while(!que.empty()) {
        node p2=que.top();
        if(p2.step>=ans) {
            return false;
        }
        // printf("%d\n",p2.step);
        que.pop();
        for(int i=0; i<4; i++) {
            p1.x=p2.x+dir_x[i];
            p1.y=p2.y+dir_y[i];
//            p1.step=p2.step+1;
            p1.pos=p2.pos;
            if(vis[p1.pos][p1.x][p1.y]==true) {
                continue;
            }
            vis[p1.pos][p1.x][p1.y]=true;
            if(judge(p1)==false) {
                continue;
            }
            if(p1.pos==1) {
                if(map1[p1.x][p1.y]=='*')
                    continue;
            }
            if(p1.pos==2) {
                if(map2[p1.x][p1.y]=='*')
                    continue;
            }


            if(p1.pos==1) {
                if(map1[p1.x][p1.y]=='#') {
                    vis[p1.pos][p1.x][p1.y]=true;
                    p1.pos=2;
                }
            } else if(p1.pos==2) {
                if(map2[p1.x][p1.y]=='#') {
                    vis[p1.pos][p1.x][p1.y]=true;
                    p1.pos=1;
                }
            }
            vis[p1.pos][p1.x][p1.y]=true;
            p1.step=p2.step+1;
            if(p1.pos==1) {
                if(map1[p1.x][p1.y]=='P') {
                    return true;
                }
            }
            if(p1.pos==2) {
                if(map2[p1.x][p1.y]=='P') {
                    return true;
                }
            }
            que.push(p1);
        }
    }
    return false;
}


int main() {
    int T;
    scanf("%d",&T);
    while(T--) {
        scanf("%d%d%d",&n,&m,&ans);
        for(int i=0; i<n; i++) {
            scanf("%s",map1[i]);
        }
        for(int i=0; i<n; i++) {
            scanf("%s",map2[i]);
        }
        for(int i=0; i<n; i++) {
            for(int j=0; j<m; j++) {
                if (map1[i][j]=='#'&&map2[i][j]=='*') {
                    map1[i][j]='*';
                } else if (map1[i][j]=='#'&&map2[i][j]=='#') {
                    map1[i][j]=map2[i][j]='*';
                } else if (map2[i][j]=='#'&& map1[i][j]=='*') {
                    map2[i][j]='*';
                }
            }
        }
        if(bfs(1,0,0)) {
            printf("YES\n");
        } else {
            printf("NO\n");
        }
    }
    return 0;
}

0 0
原创粉丝点击