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

ft,sample 都没错,好心人给些数据啊,谢谢了了!!!!

Posted by dexter at 2004-04-01 16:27:27 on Problem 1562
#include<stdio.h>
#include<stdlib.h>
#define MAX 100
struct direction{
	int row ;
	int col ;
}dir[8] ;
void make_dir( void )
{
	dir[7].row= dir[0].row= dir[1].row =-1 ;
	dir[5].row= dir[4].row = dir[3].row = 1 ;
	dir[6].row= dir[2].row = 0 ;
	dir[7].col= dir[6].col = dir[5].col = -1 ;
	dir[1].col= dir[2].col = dir[3].col = 1 ;
	dir[0].col= dir[4].col = 0 ;
}
struct position{
	int r;
	int col;
}stack[MAX];
char matrix[MAX+1][MAX+1];
int visited[MAX+1][MAX+1];
int row,column;
int firstadjvex(int a,int b,int *x,int *y)
{
	int k;
	int nx,ny;
	for(k=0;k<8;k++)
	{
		nx=a+dir[k].row;
		ny=b+dir[k].col;
		if(nx>=1&&nx<=row&&ny>=1&&ny<=column&&matrix[nx][ny]=='@'&&visited[nx][ny]==0)
		{
			*x=nx;
			*y=ny;
			return 1;
		}
	}
	*x=0;
	*y=0;
	return 0;
}
int nextadjvex(int x,int y,int *nx,int *ny)
{
	int k;
	int tempx,tempy;
	for(k=0;k<8;k++)
	{
		tempx=x+dir[k].row;
		tempy=y+dir[k].col;
		if(tempx>=1&&tempx<=row&&tempy>=1&&tempy<=column&&matrix[tempx][tempy]=='@'&&visited[tempx][tempy]==0)
		{
			*nx=tempx;
			*ny=tempy;
			return 1;
		}
	}
	*nx=0;
	*ny=0;
	return 0;
}
int findunvisited(int *x,int *y)
{
	int i,j;
	for(i=1;i<=row;i++)
		for(j=1;j<=column;j++)
			if(!visited[i][j]&&(matrix[i][j]=='@'))
			{
				*x=i;
				*y=j;
				return 1;
			}
	*x=0;
	*y=0;
	return 0;
}
int search()
{
	int i,j;
	int x,y;
	int num;
	int front;
	int rear;
	int adjx,adjy;
	for(i=1;i<=row;i++)
		for(j=1;j<=column;j++)
			visited[i][j]=0;
	num=0;
	while(findunvisited(&x,&y))
	{
		visited[x][y]=1;
		front=rear=MAX;
		num++;
		front--;
		rear--;
		stack[front].r=x;
		stack[front].col=y;
		while(rear<=front)
		{
			x=stack[front].r;
			y=stack[front].col;
			front--;
			for(firstadjvex(x,y,&adjx,&adjy);adjx&&adjy;nextadjvex(x,y,&adjx,&adjy))
			{
				visited[adjx][adjy]=1;
				rear--;
				stack[rear].r=adjx;
				stack[rear].col=adjy;
			}
		}
	}
	return num;
}
int init()
{
	int i,j;
	scanf("%d %d",&row,&column);
	if(row==0)
		return 0;
	getchar();
	for(i=1;i<=row;i++)
	{
		for(j=1;j<=column;j++)
			scanf("%c",&matrix[i][j]);
		getchar();
	}
	return 1;
}
main()
{
	make_dir(  );
	while(init())
	{
		printf("%d\n",search());
	}
}



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