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 revofong at 2016-08-28 18:51:21 on Problem 1164
#include "stdio.h"
#include "string.h"

int g_Map[64][64];
bool g_Visited[64][64];
int g_DirsForRow[4] = {0, -1, 0, 1};//w n e s
int g_DirsForCol[4] = {-1, 0, 1, 0};//w n e s

bool can_move(int row, int col, int dir){
	int shift = dir;
	return (g_Map[row][col] & (1 << shift)) == 0;
}

void dfs(int row, int col, int& roomSize){
	if(g_Visited[row][col]){
		return;
	}
	g_Visited[row][col] = true;
	roomSize++;
	for(int dir = 0; dir < 4; dir++){
		if(can_move(row, col, dir)){
			dfs(row + g_DirsForRow[dir], col + g_DirsForCol[dir], roomSize);
		}
	}
}

int main(){
	int rows, cols;
	scanf("%d%d", &rows, &cols);
	for(int i = 0; i < rows; i++){
		for(int j = 0; j < cols; j++){
			scanf("%d", &g_Map[i][j]);
		}
	}

	int roomCount = 0;
	int maxRoomSize = 0;
	for(int i = 0; i < rows; i++){
		for(int j = 0; j < cols; j++){
			if(!g_Visited[i][j]){
				int roomSize = 0;
				dfs(i, j, roomSize);
				roomCount++;
				if(maxRoomSize < roomSize){
					maxRoomSize = roomSize;
				}
			}
		}
	}
	printf("%d\n%d", roomCount, maxRoomSize);
	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