搜索连通块加转弯问题

1999: 三角形or四边形?

描述

题目描述:

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

其中一种3 jiao *为

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . # . . . . .

. . . # . # . . . .

. . # . . . # . . .

. # . . . . . # . .

######### .

. . . . . . . . . .

扫描二维码关注公众号,回复: 892215 查看本文章

. . . . . . . . . .

其中一种4 bian *为

. . . . . . . . . .

. . . . . . . . . .

. ########.

. # . . . . . . #.

. # . . . . . . #.

. # . . . . . . #.

. ########.

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

输入:

一个10*10点阵图

输出:

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

样例输入
..........
..........
..........
....#.....
...#.#....
..#...#...
.#.....#..
#########.
..........
..........
样例输出

3 jiao *


起始搜的时候随便找一个方向  并且沿着这个方向搜到底

不能继续搜的时候转弯 换方向 


有疑惑的一点是 如果我dir数组不按照顺时针或者逆时针写的话就过不了全部检测点

#include<bits/stdc++.h>
using namespace std;
int dir[8][2] = {{-1,-1},{0,-1},{1,-1},{1,0},{1,1},{0,1},{-1,1},{-1,0}};
char mp[15][15]; 
int visit[15][15];
int turn = 0;

bool check(int x,int y)
{
	if(x >= 1 && y >= 1 && x <= 10 && y <= 10 && mp[x][y] == '#' && !visit[x][y] ) return true;
	
	return false;
}
void dfs(int x, int y, int d)
{
    visit[x][y] = 1;
    
    int tx = x + dir[d][0], ty = y + dir[d][1];
    
    if(check(tx,ty))
    {
    	//如果能继续搜
		dfs(tx,ty,d);  //沿着原来方向继续搜
		return ; 
	}
	else
	{
		turn ++;  //不能搜了  转弯 
	}
	
	for(int i = 0;i < 8;i ++)
	{
		int dx = x + dir[i][0];
		int dy = y + dir[i][1];
		if(check(dx,dy))
		{
			dfs(dx,dy,i);
		}
	}
}

int main()
{
	
	
	for(int i = 1;i <= 10;i ++)
	{
		for(int j = 1;j <= 10;j ++)
		cin >> mp[i][j];
	}
	
	for(int i = 1;i <= 10;i ++)
	{
		for(int j = 1;j <= 10;j ++)
		{
			if(!visit[i][j] && mp[i][j] == '#')
			{
				dfs(i,j,0);  //选取0为第一个搜索方向   (可以是任意) 
			}
		}
	}
	turn --;   //起点开始多向搜索 转完数多加了 1
	if(turn > 3) cout << "4 bian *" << endl;
	else cout << "3 jiao *" << endl; 
    return 0;
}

猜你喜欢

转载自blog.csdn.net/soul_97/article/details/80294747