HDU 2102 A计划 (BFS)

来源:互联网 发布:淘宝开婴童家纺 编辑:程序博客网 时间:2024/05/17 22:22

简单BFS,注意好状态转移过程 即可,还有就是一旦踏入传送机,立刻会被传送,所以要立刻改变z坐标

细节参见代码:

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<map>#include<vector>#include<queue>#include<list>#include<set>#include<cmath>using namespace std;typedef long long ll;const int mod = 1e9 + 7;const int INF = 1e9;const int maxn = 15;int n,m,T,a,b,c,t,d[2][maxn][maxn];int dx[] = {0,1,0,-1};int dy[] = {1,0,-1,0};struct node{    int z,x,y;    node(int z=0, int x=0, int y=0):z(z),x(x),y(y) {}    bool operator == (const node& rhs) const {        return z == rhs.z && x == rhs.x && y == rhs.y;    }}S,P;char s[2][maxn][maxn];bool bfs(node S) {    queue<node> q;    memset(d,-1,sizeof(d));    d[S.z][S.x][S.y] = 0;    q.push(S);    while(!q.empty()) {        node u = q.front(); q.pop();        if(u == P && d[u.z][u.x][u.y] <= t) return true;        for(int i=0;i<4;i++) {            int z = u.z, x = u.x + dx[i], y = u.y + dy[i];            if(x < 0 || x >= n || y < 0 || y >= m) continue;            if(s[z][x][y] == '#') z = 1 - z;            if(s[z][x][y] == '#' && s[1-z][x][y] == '#') continue;            if(d[z][x][y] == -1 && s[z][x][y] != '*') {                d[z][x][y] = d[u.z][u.x][u.y] + 1;                q.push(node(z,x,y));            }        }    }    return false;}int main() {    scanf("%d",&T);    while(T--) {        scanf("%d%d%d",&n,&m,&t);        for(int k=0;k<=1;k++) {            for(int i=0;i<n;i++) scanf("%s",s[k][i]);        }        for(int k=0;k<=1;k++) {            for(int i=0;i<n;i++) {                for(int j=0;j<m;j++) {                    if(s[k][i][j] == 'S') S = node(k,i,j);                    if(s[k][i][j] == 'P') P = node(k,i,j);                }            }        }        if(bfs(S)) printf("YES\n");        else printf("NO\n");    }    return 0;}


0 0
原创粉丝点击