zoukankan      html  css  js  c++  java
  • CCF_201312-5_I’m stuck!

      一次bfs从起点开始找到起点能到达的点,一次bfs从终点开始找到能到终点的点,最后输出答案即可。

      刚开始写的时候,考虑找起点能到达的点的时候,用了dfs,提交只有20分,仔细想了一下,会存在无限循环的情况。

    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<queue>
    using namespace std;
    
    struct point{
        int x,y;
    }start,stop;
    
    char a[55][55];
    int r,c,cango[55][55],canfrom[55][55],dir[][2] = {{-1,0},{1,0},{0,-1},{0,1}};
    
    void bfs1(point start)
    {
        queue<point> q;
        q.push(start);
        while(!q.empty())
        {
            int x = q.front().x,y = q.front().y;
            q.pop();
            if(x<0 || x>=r || y<0 || y>=c || a[x][y]=='#' || cango[x][y])   continue;
            cango[x][y] = 1;
            if(a[x][y]=='S' || a[x][y]=='T' || a[x][y]=='+')
            {
                for(int i = 0;i < 4;i++)
                {
                    point temp;
                    temp.x = x+dir[i][0];
                    temp.y = y+dir[i][1];
                    q.push(temp);
                }
            }
            else if(a[x][y] == '-')
            {
                for(int i = 2;i < 4;i++)
                {
                    point temp;
                    temp.x = x+dir[i][0];
                    temp.y = y+dir[i][1];
                    q.push(temp);
                }
            }
            else if(a[x][y] == '|')
            {
                for(int i = 0;i < 2;i++)
                {
                    point temp;
                    temp.x = x+dir[i][0];
                    temp.y = y+dir[i][1];
                    q.push(temp);
                }
            }
            else
            {
                point temp;
                temp.x = x+dir[1][0];
                temp.y = y+dir[1][1];
                q.push(temp);
            }
        }
    }
    
    void bfs2(point stop)
    {
        queue<point> q;
        q.push(stop);
        while(!q.empty())
        {
            int x= q.front().x,y = q.front().y;
            q.pop();
            canfrom[x][y] = 1;
            for(int i = 0;i < 4;i++)
            {
    
                int xx = x+dir[i][0],yy = y+dir[i][1];
                if(xx<0 || xx>=r || yy<0 || yy>=c || canfrom[xx][yy])  continue;
                if(a[xx][yy]=='S' || a[xx][yy]=='T' || a[xx][yy]=='+' || i==0&&a[xx][yy]=='.' || i<=1&&a[xx][yy]=='|' || i>=2&&a[xx][yy]=='-')
                {
                    point temp;
                    temp.x = xx;
                    temp.y = yy;
                    q.push(temp);
                }
            }
        }
    }
    int main()
    {
        scanf("%d%d",&r,&c);
        getchar();
        for(int i = 0;i < r;i++)   gets(a[i]);
        for(int i = 0;i < r;i++)
        {
            for(int j = 0;j < c;j++)
            {
                if(a[i][j] == 'S')  start.x = i,start.y = j;
                else if(a[i][j] == 'T') stop.x = i,stop.y = j;
            }
        }
        bfs1(start);
    
        if(!cango[stop.x][stop.y])
        {
            printf("I'm stuck!
    ");
            return 0;
        }
        bfs2(stop);
        int num = 0;
        for(int i = 0;i < r;i++)
        {
            for(int j = 0;j < c;j++)
            {
                if(cango[i][j] && !canfrom[i][j])   num++;
            }
        }
        printf("%d
    ",num);
    
    }
  • 相关阅读:
    屏幕的真实分辨率大小
    CCConfiguration::sharedConfiguration()->loadConfigFile cocos2d-x 中文乱码问题及国际化解决方案
    git 放弃提交到提交之前
    cocos2d-x 输出debug信息
    Ubuntu设置环境变量
    有时候需要统计手机的型号和版本号,利用程序可以获取到相应的手机信息.
    读取 android sys/下的信息
    android 读取 raw 中的文件。
    C/C++中结构体(struct)
    异步图片下载引擎(升级版——ExecutorService+handler)
  • 原文地址:https://www.cnblogs.com/zhurb/p/5839364.html
Copyright © 2011-2022 走看看