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

100题留念,附深搜AC代码

Posted by upwinder at 2009-07-17 18:01:47 on Problem 1111
#include <stdio.h>
#include <string.h>
#define N 22

int a[N][N];
int used[N][N];

int n,m,x,y;
int num;

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

void search(int x, int y)
{
	used[x][y] = 1;
	for (int i=0; i<8; i++)
	{
		int nx = x+dir[i][0];
		int ny = y+dir[i][1];
		if(a[nx][ny] == 0 && i%2==0) num++;
		if (nx>0 && nx<=n && ny>0 && ny<=m)
		{
			if (a[nx][ny] == 1 && used[nx][ny] == 0) search(nx,ny);
		}
	}
}


int main()
{
	while (scanf("%d%d%d%d", &n, &m, &x, &y) != EOF && n)
	{	
		memset(a,0,sizeof(a));
		memset(used,0,sizeof(used));
		for (int i=1; i<=n; i++)
		{
			getchar();
			for (int j=1; j<=m; j++)
			{
				if (getchar() == 'X') a[i][j] = 1;
				else a[i][j] = 0;
			}
		}
		num=0;
		search(x,y);
		printf("%d\n", num);
	}
	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