| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 403 人关注过本帖
标题:bfs 很简单的搜索,哪错了呢?
只看楼主 加入收藏
cb_1212
Rank: 1
等 级:新手上路
帖 子:126
专家分:5
注 册:2011-4-28
结帖率:66.67%
收藏
 问题点数:0 回复次数:0 
bfs 很简单的搜索,哪错了呢?
http://acm.hdu.

程序代码:
#include<iostream>
#include<cstdio>
#include<cstdlib>

using namespace std;

const int MAXN=15;

int n,m;
char map[2][MAXN][MAXN];
int dir[][2]={{0,1},{0,-1},{1,0},{-1,0}};
int vis[2][MAXN][MAXN];

struct Node{
    int x,y,step;
    Node(){};
    Node(int a,int b,int c):x(a),y(b),step(c){};
}que[MAXN*MAXN*MAXN];

int bfs()
{
    int h,t;
    memset(vis,0,sizeof(vis));
    vis[0][0][0]=1;
    que[h=t=0]=Node(0,0,0);
    while(h<=t){
        int x=que[h].x,y=que[h].y,z=0,step=que[h++].step;
        for(int i=0;i<4;++i){
            int xx=x+dir[i][0],yy=y+dir[i][1];
            if(xx<0 || xx>=n || yy<0 || yy>=m)    continue;
            if(map[z][xx][yy]=='#'){
                z^=1;
                if(map[z][xx][yy]=='#' 
                    || map[z][xx][yy]=='*' || vis[z][xx][yy])    continue;
                step++;
            }
            if(map[z][xx][yy]=='*' || vis[z][xx][yy])    continue;
            if(map[z][xx][yy]=='P')    return step+1;
            vis[z][xx][yy]=1;
            que[++t]=Node(xx,yy,step+1);
        }
    }
    return -1;    
}

int main()
{
    int c,t;
    cin>>c;
    while(c--){
        cin>>n>>m>>t;
        for(int i=0;i<2;++i)
        for(int j=0;j<n;++j)    cin>>map[i][j];
        if(t==bfs())    cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
    return 0;
}
http://acm.hdu.

程序代码:
#include<iostream>
#include<cstdio>
#include<cstdlib>

using namespace std;

const int MAXN=15;

int n,m;
char map[2][MAXN][MAXN];
int dir[][2]={{0,1},{0,-1},{1,0},{-1,0}};
int vis[2][MAXN][MAXN];

struct Node{
    int x,y,z,step;
    Node(){};
    Node(int a,int b,int c,int d):x(a),y(b),z(c),step(d){};
}que[MAXN*MAXN*MAXN];

int bfs()
{
    int h,t;
    memset(vis,0,sizeof(vis));
    vis[0][0][0]=1;
    que[h=t=0]=Node(0,0,0,0);
    while(h<=t){
        int x=que[h].x,y=que[h].y,z=que[h].z,step=que[h++].step;
        for(int i=0;i<4;++i){
            int xx=x+dir[i][0],yy=y+dir[i][1];
            if(xx<0 || xx>=n || yy<0 || yy>=m)    continue;
            if(map[z][xx][yy]=='#'){
                z^=1;
                if(map[z][xx][yy]=='#' 
                    || map[z][xx][yy]=='*' || vis[z][xx][yy])    continue;
                step++;
            }
            if(map[z][xx][yy]=='*' || vis[z][xx][yy])    continue;
            if(map[z][xx][yy]=='P')    return step+1;
            vis[z][xx][yy]=1;
            que[++t]=Node(xx,yy,z,step+1);
        }
    }
    return -1;    
}

int main()
{
    int c,t;
    cin>>c;
    while(c--){
        cin>>n>>m>>t;
        for(int i=0;i<2;++i)
        for(int j=0;j<n;++j)    scanf("%s",map[i][j]);
        if(t==bfs())    cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
    return 0;
}



不好意思刚才贴错了。。不过这个还是WA。。。

[ 本帖最后由 cb_1212 于 2012-5-16 23:50 编辑 ]
搜索更多相关主题的帖子: 搜索 
2012-05-16 23:23
快速回复:bfs 很简单的搜索,哪错了呢?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018571 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved