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 2017-01-23 05:27:08 on Problem 1979
#include <iostream>
#include <stdio.h>
using namespace std;

int cnt;
char grid[32][32];
bool used[32][32];
int m,n;

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

void dfs(int x, int y){
	cnt ++;
	used[x][y] = true;
	for(int d = 0; d < 4; d++){
		int nx = x+dir[d][0], ny = y+dir[d][1];
		if(nx>=0 && nx<n && ny>=0 && ny<m && !used[nx][ny] && grid[nx][ny] == '.'){
			dfs(nx,ny);
		}
	}
}

int main() {
	while(1){
		scanf("%d%d",&m,&n);
		if(!m && !n) break;
		int sX, sY;
		for(int i = 0; i < n; i++){
			scanf("%s", grid[i]);
			for(int j = 0; j < m; j++){
				if(grid[i][j] == '@'){
					grid[i][j] = '.';
					sX = i, sY = j;
				}
			}
		}
		for(int i = 0; i < n; i++){
			for(int j = 0; j < m; j++){
				used[i][j] = false;
			}
		}
		cnt = 0;
		dfs(sX, sY);
		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