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 flowertree_xiaoxiang at 2015-09-25 14:13:31 on Problem 1979
//============================================================================
//
// File        :    poj1979.cpp
// Author      :    flowertree
// Time        :    2015年9月25日
// About       :    DFS
//
//============================================================================

#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;

#define MAX 25

char s[MAX][MAX];
int sum;

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

void DFS(int i,int j,int m,int n)
{
	s[i][j] = '#';
	sum++;
	int tempx,tempy;
	for(int k = 0; k < 4; k++)
	{
		tempx = i + a[k][0];
		tempy = j + a[k][1];
		if((tempx >= 0 && tempx < m) && (tempy >= 0 && tempy < n) && s[tempx][tempy] == '.')
		{
			DFS(tempx,tempy,m,n);
		}
	}
}

int main()
{
	int m,n;
	int markx,marky;
	while(cin >> n >> m,m && n)
	{
		for(int i = 0; i < m; i++)
		{
			scanf("%s",s[i]);
			for(int j = 0; j < n; j++)
			{
				if(s[i][j] == '@')
				{
					markx = i;
					marky = j;
				}
			}
		}
		sum = 0;
		DFS(markx,marky,m,n);
		cout << sum << endl;
	}
	system("pause");
	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