Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

一次性通过,虽然是个大水题,依然很高兴,附代码

Posted by Matthew_lyns at 2017-05-02 11:54:47 on Problem 1164
#include <iostream>

using namespace std;

static int sum = 0;
int m, n;
int numOfRoom;
int maxNumOfRoom;


int data[55][55];
int visited[55][55];

void dfs(int x, int y)
{
	
	if(x >= 0 && x < m && y >= 0 && y < n && visited[x][y] == 0)
	{
		visited[x][y] = 1;
		if((data[x][y] & 1) == 0)
		{
			dfs(x, y - 1);
		}

		if((data[x][y] & 2) == 0)
		{
			dfs(x - 1, y);
		}
		if((data[x][y] & 4) == 0)
		{
			dfs(x, y + 1);
		}
		if((data[x][y] & 8) == 0)
		{
			dfs(x + 1, y);
		}

		sum ++;
	}
}

int main()
{
	int i = 0;
	int j = 0;

	memset(visited, 0, sizeof(visited));

	//freopen("input.txt","r",stdin);
	//setbuf(stdout, NULL);

	cin>> m >> n;

	for(i=0;i < m;i++)  
    {  
        for(j=0;j < n;j++)  
        {  
            cin>>data[i][j];  
        }  
	}  

	numOfRoom = 0;
	maxNumOfRoom = 0;


	for(i=0;i < m;i++)  
    {  
        for(j=0;j < n;j++)  
        {  
            sum = 0;
			if(visited[i][j] == 0) 
			{
				dfs(i, j);
				numOfRoom ++;
			}

			maxNumOfRoom = max(sum, maxNumOfRoom);
        }  
	}

	cout << numOfRoom << endl;
	cout << maxNumOfRoom << endl;
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator