2019_GDUT_新生专题I选集 C POJ-1979

题目:

There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can’t move on red tiles, he can move only on black tiles.Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Input
The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.

There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.

‘.’ - a black tile

‘#’ - a red tile

‘@’ - a man on a black tile(appears exactly once in a data set)

The end of the input is indicated by a line consisting of two zeros.

Output

For each data set, your program should output a line which contains
the number of tiles he can reach from the initial tile (including
itself).

Sample Input

6 9

…#.
…#





#@…#
.#…#.
11 9

.#…

.#.#######.

.#.#…#.

.#.#.###.#.

.#.#…@#.#.

.#.#####.#.

.#…#.

.#########.

11 6

…#…#…#…

…#…#…#…

…#…#…###

…#…#…#@.

…#…#…#…

…#…#…#…

7 7

…#.#…

…#.#…

###.###

…@…

###.###

…#.#…

…#.#…

0 0

Sample Output

45

59

6

13

分析:和B题一样做法,但是需要在输入时记下@的位置,从这个位置开始bfs或者dfs一遍即可。本题只有上下左右的方块是视为相连的。

代码
BFS:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,m,a[25][25],ans;

void bfs(int x1,int y1)
{
	int queue[500][2],l=0,r=1,x,y;
	int p[]={0,0,1,-1},q[]={1,-1,0,0};
	memset(queue,0,sizeof(queue));
	queue[1][1]=x1;
	queue[1][2]=y1;
	a[x1][y1]=1;
	while (l<r)
	{
		ans++;
		l++;
		for (int i=0;i<4;i++)
		{
			x=queue[l][1]+p[i];
			y=queue[l][2]+q[i];
			if (x<=n && y<=m && !a[x][y] && x>=1 && y>=1)
			{
				r++;
				queue[r][1]=x;
				queue[r][2]=y;
				a[x][y]=1;
			}
		}
	}
}

int main()
{
	char s[101];
	int x,y;
	scanf("%d%d",&m,&n);
	while (m!=0 && n!=0)
	{
		memset(a,0,sizeof(a));
		ans=0;
		for (int i=1;i<=n;i++)
		{
			cin>>s;
			for (int j=0;j<strlen(s);j++)
			{
				if (s[j]=='#') a[i][j+1]=1;
				if (s[j]=='@')
					 x=i,y=j+1;
			}
		}
		bfs(x,y);
		cout<<ans<<endl;
		scanf("%d%d",&m,&n);
	}
	return 0;
}

DFS:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int a[110][110],n,m,ans=1,xf,yf;
int dx[]={0,0,-1,1},dy[]={1,-1,0,0};

void dfs(int x1,int y1)
{
	int x,y;
	for (int i=0;i<4;i++)
	{
		x=x1+dx[i];
		y=y1+dy[i];
		if (x>0 && y>0 && x<=n && y<=m && a[x][y]==1)
		{
			ans++;
			a[x][y]=0;
			dfs(x,y);
		}
	}
}

int main()
{
	char s[110];
	cin>>m>>n;
	while (m*n!=0)
	{
		ans=1;
		memset(a,0,sizeof(a));
		for (int i=1;i<=n;i++)
		{
			cin>>s;
			for (int j=0;j<m;j++)
			{
				if (s[j]=='.' || s[j]=='@')
				a[i][j+1]=1;
				if (s[j]=='@')
				xf=i,yf=j+1;
			}
		}
		a[xf][yf]=0;
		dfs(xf,yf);
		cout<<ans<<endl;	
		cin>>m>>n;
	}
	return 0;
}
发布了14 篇原创文章 · 获赞 0 · 访问量 308

猜你喜欢

转载自blog.csdn.net/qq_39581539/article/details/103964100