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

Re:水题1A,DFS就行了。

Posted by zhangshiyi at 2021-07-18 16:47:30 on Problem 1979
In Reply To:水题1A,DFS就行了。 Posted by:1196494730 at 2018-10-19 16:43:42
#include<iostream>
#include<cstring>
#define N 29
using namespace std;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
int n,m,sx,sy,vst[N][N];char d[N][N];
int dfs(int x,int y) {
	vst[x][y]=1;
	int ret=1;
	for (int k=0;k<4;++k) {
		int nx=x+dx[k],ny=y+dy[k];
		if (nx<1||nx>n||ny<1||ny>m) continue;
		if (vst[nx][ny]||d[nx][ny]!='.') continue;
		ret+=dfs(nx,ny);
	}
	return ret;
}
int main() {
	while (cin>>m>>n) {
		if (n==0&&m==0) return 0;
		memset(vst,0,sizeof vst);
		for (int i=1;i<=n;++i)
			for (int j=1;j<=m;++j) {
				cin>>d[i][j];
				if (d[i][j]=='@') sx=i,sy=j;
			}
		cout<<dfs(sx,sy)<<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