zoukankan      html  css  js  c++  java
  • ACM-AK吧!少年

    题目描述:AK吧!少年

       AK is an ACM competition finished all of the problems. AC This problem is one of the steps,so fight to AK。There are a number of graphics"_" and "#",In the graph,"#"make up "A" or "K" these two letters。Makea program to identify these letters, we want to kown the number of these twoletters.

    输入

    Input contains multiple test cases.
    Each test case contains two integer N (10<=N<=100) and M (10<=M<=100) is the graphics of the number of rows and columns。
    Next graphics, letter fonts and titles to describe these two letters the same standard fonts, letter size is not necessarily the same, and does not skew, each letter occupies a separate rectangular area itself is connected, do not cross, and other letters or connected.

    输出

    Each test case output the number of "A" and "K".

    样例输入

    10 47
    _______________________________________________
    _____________####________#####______###________
    ____________#######_______####_____###_________
    ___________###__###________###___#####_________
    ________#####____###_______###_###_____________
    _________###______###______######______________
    ________##############_____###_###_____________
    _______###__________###____###___###___________
    ______#####________####___###_____###__________
    _____###______________###__###_______###_______

    样例输出

    1 1

    思路:DFS判断图形即可。

    #include "stdafx.h"
    //AK吧,少年!
    //解题思路:
    //1.深搜或者广搜遍历
    //2.遇到第一个合适的节点检查是“A”还是“K”,A和K的区别是x+1,y-1是不是#
    //因为是一次遍历就遍历完全所有相连节点,所以只要判断符合条件的节点的特殊位置就可以判断是哪个字母
    
    
    
    
    #include <stdio.h>
    #include <string.h>
    const int MAX = 105;
    int vis[MAX][MAX];
    char map[MAX][MAX];
    int dir[8][2] = { 0, 1, 0, -1, 1, 0, -1, 0, -1, -1, -1, 1, 1, -1, 1, 1 };
    int n, m,ans[2];
    
    
    void DFS(int x, int y)
    {
        vis[x][y] = 1;
        for (int i = 0; i < 8; i++)
        {
            int nx = dir[i][0] + x;
            int ny = dir[i][1] + y;
            if (nx >= 0 && ny >= 0 && nx < n && ny < m && !vis[nx][ny] && map[nx][ny] == '#')
            {
                DFS(nx, ny);
            }
    
        }
    }
    
    #include <iostream>
    #include <queue>
    using namespace std;
    struct Point
    {
        int x, y;
    };
    queue<Point> q;
    void BFS(int x, int y)
    {
        vis[x][y] = 1;
        Point p;
        p.x = x;
        p.y = y;
        q.push(p);
        while (!q.empty())
        {
            Point p = q.front();
            q.pop();
            int x = p.x;
            int y = p.y;
            vis[x][y] = 1;
            for (int i = 0; i < n; i++)
            {
                int nx = dir[i][0] + x;
                int ny = dir[i][1] + y;
                if (nx >= 0 && ny >= 0 && nx < n && ny < m && !vis[nx][ny] && map[nx][ny] == '#')
                {
                    Point p;
                    p.x = nx;
                    p.y = ny;
                    q.push(p);
                }
            }
    
        }
        
    }
    
    int check(int x, int y)
    {
        if (map[x + 1][y - 1] == '#') return 0;
        else return 1;
    }
    
    int main()
    {
        while (scanf("%d %d", &n, &m) != EOF)
        {
            memset(vis, 0, sizeof(vis));
            memset(ans, 0, sizeof(ans));
            for (int i = 0; i < n; i++)
            {
                scanf("%s", map[i]);
            }
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    if (!vis[i][j] && map[i][j] == '#')
                    {
                        //DFS(i, j);
                        BFS(i, j);
                        ans[check(i, j)] ++;
                    }
                }
            }
            printf("%d %d
    ", ans[0], ans[1]);
    
        }
        return 0;
    }
  • 相关阅读:
    Ubuntu安装最新版的nodejs
    Mac安装并破解OmniGraffle7
    Mac安装并破解StarUML
    Windows10使用Chocolatey安装mysql之后无法使用的解决办法
    Visual Studio编辑类模板的位置
    VS2017连接到中国区的Azure
    Windows上包管理器之Chocolatey初体验
    CENTOS7.3 64位架设使用MYSQL数据库的ASP.NET CORE网站
    从无到有开发自己的Wordpress博客主题---主页模板
    c# 获得方法的所属类(或调用者)的类名,方法名
  • 原文地址:https://www.cnblogs.com/x739400043/p/8505412.html
Copyright © 2011-2022 走看看