`
java-mans
  • 浏览: 11409864 次
文章分类
社区版块
存档分类
最新评论

AOJ-579-期末考试之考试传纸条

 
阅读更多

AOJ-579-期末考试之考试传纸

http://icpc.ahu.edu.cn/OJ/Problem.aspx?id=579

BFS,按模版写的,比赛时数据输入处理弄错了,哎。。。太弱了。。。快哭了


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int n,m;
char map[105][105];
int ans[105][105];
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
typedef struct
{
	int x;
	int y;
}node;
int bfs(node s,node e)
{
	node que[10005];
	node tt;
	int head=0,tail=1,i;
	que[0]=s;
	ans[s.x][s.y]=0;
	map[s.x][s.y]='#';
	while(head<tail)
	{
		s=que[head++];
		for(i=0;i<4;i++)
		{
			tt.x=s.x+dir[i][0];
			tt.y=s.y+dir[i][1];
			if(tt.x>=0&&tt.x<n&&tt.y>=0&&tt.y<m&&map[tt.x][tt.y]!='#')
			{
				ans[tt.x][tt.y]=ans[s.x][s.y]+1;
				if(tt.x==e.x&&tt.y==e.y)
				return ans[e.x][e.y];
				map[tt.x][tt.y]='#';
				que[tail++]=tt;
			}
		}
	}
	return 0;
}
int main()
{
	int i,j;
	char ch;
	node s,e;
	int k1,k2;
	int ans;
	while(scanf("%d %d",&n,&m)!=EOF)
	{
		memset(map,0,sizeof(map));
		for(i=0;i<n;i++)
		{
			getchar();
			for(j=0;j<m;j++)
			{
				scanf("%c",&ch);
				if(ch=='A')
				{
					s.x=i;
					s.y=j;
				}
				else if(ch=='B')
				{
					e.x=i;
					e.y=j;
				}
			    else if(ch=='T')  //监考老师的一圈都是不可达的
				{
					for(k1=i-1;k1<=i+1;k1++)
					for(k2=j-1;k2<=j+1;k2++)
					if(k1>=0&&k1<n&&k2>=0&&k2<m)
					map[k1][k2]='#';
				}
				if(map[i][j]!='#')
				map[i][j]=ch;
			}
		}
		if(map[s.x][s.y]=='#'||map[e.x][e.y]=='#')
		{
			printf("-1\n");
			continue;
		}
		ans=bfs(s,e);
		if(ans)
		printf("%d\n",ans);
		else
		printf("-1\n");
	}
	return 0;
}




分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics