zoukankan      html  css  js  c++  java
  • NEUQOJ 1999: 三角形or四边形?【搜索联通块/模拟】

    http://newoj.acmclub.cn/problems/1999

    1999: 三角形or四边形?

    描述

    题目描述:

    JiangYu很无聊,所以他拿钉子在板子上戳出了一个由.#组成的10*10八联通点阵图。 请机智的你判断图中的#组成的是三角形还是四边形?

    其中一种3 jiao *为

    . . . . . . . . . .

    . . . . . . . . . .

    . . . . . . . . . .

    . . . . # . . . . .

    . . . # . # . . . .

    . . # . . . # . . .

    . # . . . . . # . .

    ######### .

    . . . . . . . . . .

    . . . . . . . . . .

    其中一种4 bian *为

    . . . . . . . . . .

    . . . . . . . . . .

    . ########.

    . # . . . . . . #.

    . # . . . . . . #.

    . # . . . . . . #.

    . ########.

    . . . . . . . . . .

    . . . . . . . . . .

    . . . . . . . . . .

    输入:

    一个10*10点阵图

    输出:

    三角形输出"3 jiao *“ 四边形输出"4 bian *”

    样例输入
    ..........
    ..........
    ..........
    ....#.....
    ...#.#....
    ..#...#...
    .#.....#..
    #########.
    ..........
    ..........
    样例输出
    3 jiao *
    提示
    描述

    题目描述:

    JiangYu很无聊,所以他拿钉子在板子上戳出了一个由.#组成的10*10八联通点阵图。 请机智的你判断图中的#组成的是三角形还是四边形?

    其中一种3 jiao *为

    . . . . . . . . . .

    . . . . . . . . . .

    . . . . . . . . . .

    . . . . # . . . . .

    . . . # . # . . . .

    . . # . . . # . . .

    . # . . . . . # . .

    ######### .

    . . . . . . . . . .

    . . . . . . . . . .

    其中一种4 bian *为

    . . . . . . . . . .

    . . . . . . . . . .

    . ########.

    . # . . . . . . #.

    . # . . . . . . #.

    . # . . . . . . #.

    . ########.

    . . . . . . . . . .

    . . . . . . . . . .

    . . . . . . . . . .

    输入:

    一个10*10点阵图

    输出:

    三角形输出"3 jiao *“ 四边形输出"4 bian *”

    【分析】:

    斜四边形:行2尖以及列2尖、 平四边形:0尖、 梯形:1个行尖,0个列尖

    正放三角形:1个行尖、倒放三角形:1个列尖    (但是另一个都不为0)

    【BFS】:

     随便从一个点dfs,有8个方向。只要改变一次方向就有一个角,记录他有几个角,
     注意遍历方向的顺序 

    【代码】:

    #include<iostream>
    #include<algorithm>
    #include<string.h>
    #include<cstring>
    #include<cstdio>
    using namespace std;
    #define ll long long
    #define ull unsigned long long
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define N 15
    using namespace std;
    char s[N][N];
    
    const int dx[]={-1,0,1,1,1,0,-1,-1};
    const int dy[]={-1,-1,-1,0,1,1,1,0};
    
    bool ok(int x, int y) //未越界+遇到#
    {
        return x>=0 && y>=0 && x<10 && y<10 && s[x][y]=='#';
    }
    
    int dfs(int x, int y, int d)
    {
        int res = 0;
        s[x][y]='.';
        int nx = x + dx[d],ny = y + dy[d];
        if(!ok(nx,ny)) res++;
        else return res+=dfs(nx,ny,d);
        for(int i=0;i<8;i++)
        {
            int nx=x+dx[i],ny=y+dy[i];
            if(ok(nx,ny)) return res+=dfs(nx,ny,i);
        }
        return res;
    }
    
    int main()
    {
        for(int i=0;i<10;i++)
            scanf("%s",s+i);
        int cnt=0;
        for(int i=0;i<10;i++){
            for(int j=0;j<10;j++){
                if(s[i][j]=='#')
                    cnt = dfs(i,j,0);
            }
        }
        if(cnt>4) puts("4 bian *");
        else puts("3 jiao *");
    }
    
    /*
    保证每条边长度>2,保证所有的#之间是联通的。
    
    ..#.......
    .#.#......
    #####.....
    ..........
    ..........
    ..........
    ..........
    ..........
    ..........
    ..........
    
    
    ....#.....
    ...##.....
    ..#.#.....
    ...##.....
    ....#.....
    ..........
    ..........
    ..........
    ..........
    ..........
    
    ...#......
    ..#.#.....
    .#...#....
    ..#.#.....
    ...#......
    ..........
    ..........
    ..........
    ..........
    ..........
    
    ..........
    ..........
    ..........
    ..........
    ..........
    ..........
    ..........
    .....###..
    .....#.#..
    .....###..
    
    ....#.....
    ...##.....
    ..#.#.....
    .#..#.....
    .####.....
    ..........
    ..........
    ..........
    ..........
    ..........
    
    ....#.....
    ...##.....
    ..#.#.....
    .#..#.....
    #####.....
    ..........
    ..........
    ..........
    ..........
    ..........
    */
    DFS
    #include<iostream>
    #include<algorithm>
    #include<string.h>
    #include<cstring>
    #include<cstdio>
    using namespace std;
    #define ll long long
    #define ull unsigned long long
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define N 15
    using namespace std;
    char s[N][N];
    
    int main()
    {
    
        int f=0,f1=0,cnt,cnt1;
        for(int i=0;i<10;i++)
            gets(s[i]);
        for(int i=0;i<10;i++)
        {
            cnt=cnt1=0;
            for(int j=0;j<10;j++)
            {
                if(s[i][j]=='#')
                    cnt++;
                if(s[j][i]=='#')
                    cnt1++;
            }
            if(cnt == 1) {
                f++ ;
            }
            if(cnt1 == 1){
                f1++;
            }
        }
    
        if(f==1&&f1!=0 || f1==1) puts("3 jiao *");
        else puts("4 bian *");
    
        return 0;
    }
    
    /*
    保证每条边长度>2,保证所有的#之间是联通的。
    
    ..#.......
    .#.#......
    #####.....
    ..........
    ..........
    ..........
    ..........
    ..........
    ..........
    ..........
    
    
    ....#.....
    ...##.....
    ..#.#.....
    ...##.....
    ....#.....
    ..........
    ..........
    ..........
    ..........
    ..........
    
    ...#......
    ..#.#.....
    .#...#....
    ..#.#.....
    ...#......
    ..........
    ..........
    ..........
    ..........
    ..........
    
    ..........
    ..........
    ..........
    ..........
    ..........
    ..........
    ..........
    .....###..
    .....#.#..
    .....###..
    
    ....#.....
    ...##.....
    ..#.#.....
    .#..#.....
    .####.....
    ..........
    ..........
    ..........
    ..........
    ..........
    
    ....#.....
    ...##.....
    ..#.#.....
    .#..#.....
    .####.....
    ..........
    ..........
    ..........
    ..........
    ..........
    */
    模拟
  • 相关阅读:
    sed中求公共前缀
    DB9 公头母头引脚定义及连接
    J2SE基础:4.面向对象的特性一
    Android APK反编译具体解释(附图)
    wxWidgets刚開始学习的人导引(1)——前言
    使用Visual Studio 2010 创建简单的Silverlight应用程序
    MyEclipse下XFire开发Webservice实例
    实战DeviceIoControl 之中的一个:通过API訪问设备驱动程序
    素数推断算法(高效率)
    Ansi,UTF8,Unicode,ASCII编码的差别
  • 原文地址:https://www.cnblogs.com/Roni-i/p/9029381.html
Copyright © 2011-2022 走看看