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

shuiti

Posted by KatrineYang at 2016-11-12 14:09:25 on Problem 1562
#include <iostream>
#include <cstring>
#include <stdio.h>
#include <stdlib.h>
#include <cmath>
#include <algorithm>
using namespace std;

int x,y;
bool oil[105][105];

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

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

void dfs(int i, int j, bool used[105][105]){
	used[i][j] = 1;
	for(int k = 0; k < 8; k++){
		int xx = i+dir[k][0], yy = j+dir[k][1];
		if(inRange(xx,yy) && oil[xx][yy] && !used[xx][yy]){
			dfs(xx,yy,used);
		}
	}
}

int main() {
	while(1){
		scanf("%d%d",&x,&y);
		if(!x){
			break;
		}
		char hang[233];
		for(int i = 0; i < x; i++){
			scanf("%s",hang);
			for(int j = 0; j < y; j++){
				if(hang[j] == '@') oil[i][j] = 1;
				else oil[i][j] = 0;
			}
		}
		bool used[105][105] = {0};
		int cnt = 0;
		for(int i = 0; i < x; i++){
			for(int j = 0; j < y; j++){
				if(oil[i][j] && !used[i][j]) {
					dfs(i,j,used);
					cnt++;
				}
			}
		}
		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