码迷,mamicode.com
首页 > 移动开发 > 详细

NEUQOJ 1999: 三角形or四边形?【搜索联通块/模拟】

时间:2018-05-12 19:20:59      阅读:491      评论:0      收藏:0      [点我收藏+]

标签:red   ade   nta   ted   改变   const   res   return   tail   

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】:

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

【代码】:

技术分享图片
#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,保证所有的#之间是联通的。

..#.......
.#.#......
#####.....
..........
..........
..........
..........
..........
..........
..........


....#.....
...##.....
..#.#.....
...##.....
....#.....
..........
..........
..........
..........
..........

...#......
..#.#.....
.#...#....
..#.#.....
...#......
..........
..........
..........
..........
..........

..........
..........
..........
..........
..........
..........
..........
.....###..
.....#.#..
.....###..

....#.....
...##.....
..#.#.....
.#..#.....
.####.....
..........
..........
..........
..........
..........

....#.....
...##.....
..#.#.....
.#..#.....
.####.....
..........
..........
..........
..........
..........
*/
模拟

 

NEUQOJ 1999: 三角形or四边形?【搜索联通块/模拟】

标签:red   ade   nta   ted   改变   const   res   return   tail   

原文地址:https://www.cnblogs.com/Roni-i/p/9029381.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!