代码1

//解法1:深搜
#include <bits/stdc++.h>
using namespace std;
#define N 25
char mp[N][N];//地图
int w, h, ct;//h:行数,w:ct:结果计数
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};//方向数组
bool vis[N][N];
void dfs(int sx, int sy)
{
	for(int i = 0; i < 4; ++i)
	{
		int x = sx + dir[i][0], y = sy + dir[i][1];
		if(x >= 1 && x <= h && y >= 1 && y <= w && vis[x][y] == false && mp[x][y] != '#')
		{
			ct++;
			vis[x][y] = true;
			dfs(x, y);
		}
	}
}
int main()
{
	int stx, sty;//起始位置
    while(true)
    {
    	cin >> w >> h;
    	if(w == 0 && h == 0)
    		return 0;
    	for(int i = 1; i <= h; ++i)
    		for(int j = 1; j <= w; ++j)
    		{
    			cin >> mp[i][j];
    			if(mp[i][j] == '@')
    				stx = i, sty = j;
    		}
    	memset(vis, 0, sizeof(vis));//多组数据,注意状态还原
    	ct = 1;
    	vis[stx][sty] = true;
		dfs(stx, sty);
    	cout << ct << endl;
	}
    return 0;
}

代码2

//解法2:广搜
#include <bits/stdc++.h>
using namespace std;
#define N 25
struct Node
{
	int x, y;
	Node(){}
	Node(int a, int b):x(a),y(b){}
};
char mp[N][N];//地图
int w, h;//w:列数 h:行数
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};//方向数组
bool vis[N][N];
int bfs(int sx, int sy)//传入起始位置
{
	queue<Node> que;
	vis[sx][sy] = true;
	que.push(Node(sx, sy));
	int ct = 1;//计数,看可以到达几个黑色格子
	while(que.empty() == false)
	{
		Node u = que.front();
		que.pop();
		for(int i = 0; i < 4; ++i)
		{
			int x = u.x + dir[i][0], y = u.y + dir[i][1];
			if(x >= 1 && x <= h && y >= 1 && y <= w && vis[x][y] == false && mp[x][y] != '#')
			{
				vis[x][y] = true;
				que.push(Node(x, y));
				ct++;
			}
		}
	}
	return ct;
}
int main()
{
	int stx, sty;
    while(true)
    {
    	cin >> w >> h;
    	if(w == 0 && h == 0)
    		return 0;
    	for(int i = 1; i <= h; ++i)
    		for(int j = 1; j <= w; ++j)
    		{
    			cin >> mp[i][j];
    			if(mp[i][j] == '@')
    				stx = i, sty = j;
    		}
    	memset(vis, 0, sizeof(vis));
    	cout << bfs(stx, sty) << endl;
	}
    return 0;
}

代码3

//解法3:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#define N 1001
using namespace std;
int m,n;
char ch;
int maps[N][N];
int vis[N][N];
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
int cnt;
void dfs(int x,int y)
{
    for(int i=0;i<4;i++)
    {
        int nx=x+dir[i][0];
        int ny=y+dir[i][1];
        if(nx>=1&&ny>=1&&nx<=n&&ny<=m&&vis[nx][ny]==0&&maps[nx][ny]==1)
        {
            vis[nx][ny]=1;
            cnt++;
            dfs(nx,ny);
        }
    }
}
int main()
{
    while(scanf("%d%d",&m,&n)!=EOF&&m&&n)
    {
        int x,y;
        cnt=1;
        memset(vis,0,sizeof(vis));
        memset(maps,0,sizeof(maps));
 
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
            {
                cin>>ch;
                if(ch=='@')
                {
                    x=i;
                    y=j;
                    maps[i][j]=1;
                }
                if(ch=='.')
                    maps[i][j]=1;
                if(ch=='#')
                    maps[i][j]=0;
            }
        vis[x][y]=1;
        dfs(x,y);
        cout<<cnt<<endl;
    }
    return 0;
}

更多推荐