AOJ-579-期末試験の試験用紙

1997 ワード

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
"); continue; } ans=bfs(s,e); if(ans) printf("%d
",ans); else printf("-1
"); } return 0; }