2018.3.1【 HDU - 6113 】解题报告(百度之星初赛,BFS,DFS,图像处理)

度度熊的01世界

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1891    Accepted Submission(s): 702


Problem Description
度度熊是一个喜欢计算机的孩子,在计算机的世界中,所有事物实际上都只由0和1组成。

现在给你一个n*m的图像,你需要分辨他究竟是0,还是1,或者两者均不是。

图像0的定义:存在1字符且1字符只能是由一个连通块组成,存在且仅存在一个由0字符组成的连通块完全被1所包围。

图像1的定义:存在1字符且1字符只能是由一个连通块组成,不存在任何0字符组成的连通块被1所完全包围。

连通的含义是,只要连续两个方块有公共边,就看做是连通。

完全包围的意思是,该连通块不与边界相接触。
 

Input
本题包含若干组测试数据。
每组测试数据包含:
第一行两个整数n,m表示图像的长与宽。
接下来n行m列将会是只有01组成的字符画。

满足1<=n,m<=100
 

Output
如果这个图是1的话,输出1;如果是0的话,输出0,都不是输出-1。
 

Sample Input

32 32 00000000000000000000000000000000 00000000000111111110000000000000 00000000001111111111100000000000 00000000001111111111110000000000 00000000011111111111111000000000 00000000011111100011111000000000 00000000111110000001111000000000 00000000111110000001111100000000 00000000111110000000111110000000 00000001111110000000111110000000 00000001111110000000011111000000 00000001111110000000001111000000 00000001111110000000001111100000 00000001111100000000001111000000 00000001111000000000001111000000 00000001111000000000001111000000 00000001111000000000000111000000 00000000111100000000000111000000 00000000111100000000000111000000 00000000111100000000000111000000 00000001111000000000011110000000 00000001111000000000011110000000 00000000111000000000011110000000 00000000111110000011111110000000 00000000111110001111111100000000 00000000111111111111111000000000 00000000011111111111111000000000 00000000111111111111100000000000 00000000011111111111000000000000 00000000001111111000000000000000 00000000001111100000000000000000 00000000000000000000000000000000 32 32 00000000000000000000000000000000 00000000000000001111110000000000 00000000000000001111111000000000 00000000000000011111111000000000 00000000000000111111111000000000 00000000000000011111111000000000 00000000000000011111111000000000 00000000000000111111110000000000 00000000000000111111100000000000 00000000000001111111100000000000 00000000000001111111110000000000 00000000000001111111110000000000 00000000000001111111100000000000 00000000000011111110000000000000 00000000011111111110000000000000 00000001111111111111000000000000 00000011111111111111000000000000 00000011111111111111000000000000 00000011111111111110000000000000 00000000001111111111000000000000 00000000000000111111000000000000 00000000000001111111000000000000 00000000000111111110000000000000 00000000000011111111000000000000 00000000000011111111000000000000 00000000000011111111100000000000 00000000000011111111100000000000 00000000000000111111110000000000 00000000000000001111111111000000 00000000000000001111111111000000 00000000000000000111111111000000 00000000000000000000000000000000 3 3 101 101 011
 

Sample Output

0 1 -1
 

Source
2017"百度之星"程序设计大赛 - 初赛(A)
 

Recommend
liuyiding

【题目大意】

给出一个由0 1组成的图,判断图形是0还是1还是其他。

【解题思路】

1.首先在在外部加'0'框(用来判断完全包围)。

2.判断0 1的连通块个数,根据题目,加框后0连通块个数为1,1连通块个数为1则为‘1’,如果0连通块数为2,1连通块数为1则为‘0’。

【解题代码】

DFS

#include 
#include 
#include 
#include  
#include 
#include 
#include 
using namespace std;
typedef long long ll;
const int maxn=120;

int n,m;
char mp[maxn][maxn];
int vis[maxn][maxn];
int dir[4][2]={{-1,0},{0,-1},{1,0},{0,1}};
struct State // BFS 队列中的状态数据结构
{
	int x;
	int y;
};
bool CheckState(State next) // 约束条件检验
{
	if(next.x<0||next.x>n||next.y<0||next.y>m||vis[next.x][next.y]) // 满足条件	
	{
		return 0;
	}	
	else // 约束条件冲突
		return 1;
}

//void check1(int i,int j,char c)
//{
//	
//	queue  q; //bfs队列
//	State now,next;
//	now.x=i,now.y=j;
//	vis[i][j]=1;
//	q.push(now);
//	while(!q.empty())
//	{
//		now=q.front();
//		q.pop();
//		for(int t=0;t<4;t++)
//		{
//			next.x=now.x+dir[t][0];
//			next.y=now.y+dir[t][1];
//			if(CheckState(next,c))
//			{
//				q.push(next);
//				vis[next.x][next.y]=1;
//			}
//		}
//	}
//} 
void check1(int x,int y,char c)  
{  
	State next;
    vis[x][y]=1;  
    for(int i=0;i<4;i++)  
    {  
    	next.x=x+dir[i][0];
		next.y=y+dir[i][1]; 
        if(!CheckState(next)) continue;   
        if(mp[next.x][next.y]!=c) continue;  
        check1(next.x,next.y,c);  
    }  
}  



int main()
{
	while(~scanf("%d%d",&n,&m))
	{
		//注意加框
		for(int i=0;i<=105;i++)  
        {  
           for(int j=0;j<=105;j++)  
           {  
               mp[i][j]='0';  
           }  
        }  
//		char c=getchar();//吃掉回车 
		for(int i=1;i<=n;i++)
		{
			scanf("%s",mp[i]+1);
			mp[i][m+1]='0'; 
		}
		n++,m++;
		memset(vis,0,sizeof(vis));
		int count0=0,count1=0;
		//检测连通 
		for(int i=0;i<=n;i++)
		{
			for(int j=0;j<=m;j++)
			{
				if(vis[i][j]) continue;
				else if(mp[i][j]=='0') count0++;
				else count1++;
				check1(i,j,mp[i][j]);
			}
		}
		if(count0==1&&count1==1) printf("1\n");
		else if(count0==2&&count1==1) printf("0\n");
		else printf("-1\n");
	}
}

BFS

#include 
#include 
#include 
#include  
#include 
#include 
#include 
using namespace std;
typedef long long ll;
const int maxn=120;

int n,m;
char mp[maxn][maxn];
int vis[maxn][maxn];
int dir[4][2]={{-1,0},{0,-1},{1,0},{0,1}};
struct State // BFS 队列中的状态数据结构
{
	int x;
	int y;
//	int Step_Counter; // 搜索步数统计器
};
bool CheckState(State next,char c) // 约束条件检验
{
	if(!vis[next.x][next.y]&&mp[next.x][next.y]==c&&next.x>=0&&next.x<=n&&next.y>=0&&next.y<=m) // 满足条件	
	{
		return 1;
	}	
	else // 约束条件冲突
		return 0;
}
void check1(int i,int j,char c)//
{
	
	queue  q; //bfs队列
	State now,next;
	now.x=i,now.y=j;
	vis[i][j]=1;
	q.push(now);
	while(!q.empty())
	{
		now=q.front();
		q.pop();
		for(int t=0;t<4;t++)
		{
			next.x=now.x+dir[t][0];
			next.y=now.y+dir[t][1];
			if(CheckState(next,c))
			{
				q.push(next);
//				printf("push i=%d j=%d \n",next.x,next.y);
				vis[next.x][next.y]=1;
//				mp[next.x][next.y]='2';
			}
		}
	}
//	for(i=0;i

【收获与反思】

两种都尝试一下。注意不要越界导致runtime error

你可能感兴趣的:(HDU,BFS,DFS)