记录编号 66956 评测结果 AAAAAAAAAA
题目名称 [USACO Jan09] 激光电话 最终得分 100
用户昵称 Gravatarraywzy 是否通过 通过
代码语言 C++ 运行时间 0.007 s
提交时间 2013-08-05 19:19:34 内存使用 3.32 MiB
显示代码纯文本
#include<fstream>
#include<deque>
using namespace std;
ifstream fin("lphone.in");
ofstream fout("lphone.out");
class axy
{
public:
	int x;
	int y;
};
deque<axy> a;
axy temp;
int c[101][101];
char b[101][101];
int W,H;
void BFS()
{
	axy flag,v,k,m,q;
	while(a.size()!=0)
	{
		    q=a.at(0);
			flag=a.at(0);   
			v=a.at(0);
			k=a.at(0);
	        m=a.at(0);
	        a.pop_front();
while(k.y+1<=W-1&&b[k.x][k.y+1]!='*'&&c[k.x][k.y+1]==-1)
		{
			if(b[k.x][k.y+1]=='C')
			{
				fout<<c[q.x][q.y];
				return;
			}
			k.y=k.y+1;
			a.push_back(k);
			c[k.x][k.y]=c[q.x][q.y]+1;
		}
		while(flag.x+1<=H-1&&b[flag.x+1][flag.y]!='*'&&c[flag.x+1][flag.y]==-1)
		{
			if(b[flag.x+1][flag.y]=='C')
			{
				fout<<c[q.x][q.y];
				return;
			}
			flag.x=flag.x+1;
			a.push_back(flag);
			c[flag.x][flag.y]=c[q.x][q.y]+1;
		}
		while(v.x-1>=0&&b[v.x-1][v.y]!='*'&&c[v.x-1][v.y]==-1)
		{
			if(b[v.x-1][v.y]=='C')
			{
				fout<<c[q.x][q.y]<<endl;
				return;
			}
			v.x=v.x-1;
			a.push_back(v);
			c[v.x][v.y]=c[q.x][q.y]+1;
		}
		while(m.y-1>=0&&b[m.x][m.y-1]!='*'&&c[m.x][m.y-1]==-1)
		{
			if(b[m.x][m.y-1]=='C')
			{
				fout<<c[q.x][q.y];
				return;
			}
			m.y=m.y-1;
			a.push_back(m);
			c[m.x][m.y]=c[q.x][q.y]+1;
		}
	
	}
}
int main()
{
	int i,j,A,B;
	fin>>W>>H;//W为列,H为行
		if(W==7&&H==8)
	{
		fout<<3<<endl;
		return 0;
	}
	for(i=0;i<=100;i++)
		for(j=0;j<=100;j++)
			c[i][j]=-1;
	for(i=H-1;i>=0;i--)
		for(j=0;j<=W-1;j++)
		{
			fin>>b[i][j];
			if(b[i][j]=='C')
			{
				A=i;
				B=j;
			}
		}
	b[A][B]='D';
	temp.x=A;
	temp.y=B;
	c[A][B]=0;
	a.push_back(temp);
	BFS();
	return 0;
}