POJ 1979 Red and Black

"## GDUT 2020寒假训练 专题一 C

原题链接

题目大意

M*N的区域,.表示黑格子,#表示红色格子,@表示所在位置。这个人可以从所在位置走他上下左右四个方向的相邻黑色格子,求这个人一共可以走过多少个格子(自己所在也算一个)

思路

深搜
读取的时候找到@也就是这个人所在位置,即起点。记录下来起点之后就从起点开始深搜,搜到一个格子就进行标记和ans++。
对于边界的处理还是进行加围栏的操作,将地图外的一圈都置为0表示不可走,就不用进行边界判断了。

代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
//#include<windows.h>
using namespace std;
const int maxn=25;
int mapp[maxn][maxn];
int order[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
int N,M;
int ans;
/*void print()
{
	Sleep(100);
	system("cls");
	for(int i=0;i<=N+1;i++)
	{
		for(int j=0;j<=M+1;j++)
		{
			printf("%3d",mapp[i][j]);
		}
		cout<<endl;
	}
	//system("pause");
	return ;
 } */
void dfs(int x,int y)
{
	if(!mapp[x][y])
	{
		return ;
	}
	ans++;
	mapp[x][y]=0;
	for(int i=0;i<4;i++)
	{
		int _x,_y;
		_x=x+order[i][0];
		_y=y+order[i][1];
		//print();
		dfs(_x,_y);
	}
	return;
}
int main()
{
	
	while(scanf("%d%d",&M,&N)&&M!=0&&N!=0)
	{
		int st_x=-1,st_y=-1;
		for(int i=1;i<=N;i++)
		{
			for(int j=1;j<=M;j++)
			{
				char c;
				cin>>c;
				if(c=='.')
				{
					mapp[i][j]=1;
				}
				else if(c=='#')
				{
					mapp[i][j]=0;
				}
				else if(c=='@')
				{
					mapp[i][j]=1;
					st_x=i;st_y=j;
				}
			}
		}
		if(st_x==-1)
		{
			cout<<0<<endl;
			continue;
		}
		//加围栏 
		for(int i=0;i<=M+1;i++)
		{
			mapp[0][i]=mapp[N+1][i]=0;
		}
		for(int i=1;i<=N+1;i++)
		{
			mapp[i][0]=mapp[i][M+1]=0;
		}
		//print();
		ans=0;
		//mapp[st][ed]=1;
		dfs(st_x,st_y);
		cout<<ans<<endl;
	}
	
	return 0;
}

"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值