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

终于AC了!第一次AC留念

Posted by Matthew_lyns at 2017-04-21 17:23:27 on Problem 1562
#include <iostream>
#include "stdio.h"

#define M 105
#define N 105

using namespace std;
char map[M][N];
int m, n;
int dir[8][2] = {{0,-1},{-1,-1}, {-1,0}, {-1,1}, {0,1}, {1,1}, {1, 0},{1,-1}};

// define DFS function
void DFS(char map[M][N],int x, int y) 
{
	int newx, newy, i;
	map[x][y] = '*';

	for(i = 0; i < 8; i++)
	{
		newx = x + dir[i][0];
		newy = y + dir[i][1];
		if(map[newx][newy] == '@' && newx >= 0 && newy >= 0 && newx < M && newy < N) {
			DFS(map, newx, newy);
		}
	}

}

int main()
{
	int cnt;
	freopen("input.txt","r",stdin);
	//setbuf(stdout, NULL);
	scanf("%d%d", &m,&n);
	while(m&&n)
	{
		cnt = 0;
		for(int i = 0; i < m; i++)
		{
			scanf("%s", &map[i]);
		}

		for(int i = 0; i < m; i++)
		{
			for(int j = 0; j < n; j++)
			{
				if(map[i][j]== '@'){
					DFS(map, i, j);
					cnt ++;
				}
			}
		}
		printf("%d\n", cnt);
		scanf("%d%d", &m,&n);
	}
	
	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