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 KatrineYang at 2016-08-27 10:01:16 on Problem 2386
#include <iostream>
#include <stdio.h>
using namespace std;

int N, M;
bool isLake[120][120] = {0};
bool used[120][120] = {0};

bool inRange(int x, int y){
	return x >= 0 && x < N && y >= 0 && y < M;
}

int dir[8][2] = {{0,1},{0,-1},{1,0},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1}};

void dfs(int x, int y){
	used[x][y] = 1;
	for(int i = 0; i < 8; i++){
		int x_ = x+dir[i][0], y_ = y+dir[i][1];
		if(inRange(x_,y_) && isLake[x_][y_] && !used[x_][y_]){
			dfs(x_,y_);
		}
	}
}

int main() {
	scanf("%d%d", &N, &M);
	for(int i = 0; i < N; i++){
		char data[120];
		scanf("%s", data);
		for(int j = 0; j < M; j++){
			if(data[j] == 'W') isLake[i][j] = 1;
		}
	}
	int cnt = 0;
	for(int i = 0; i < N; i++){
		for(int j = 0; j < M; j++){
			if(!isLake[i][j] || used[i][j]) continue;
			cnt++;
			dfs(i, j);
		}
	}
	printf("%d\n", cnt);
	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