寻宝
Sample Input
2 2 PE ES 4 4 PXEE EXEE EEEE EEEE
Sample Output
NO YES
#include<stdio.h>
int k,c[4][2]={-1,0,0,-1,1,0,0,1};
int b[6][6];
void sort(char a[][6],int x,int y)
{
int i;
for(i=0;i<4;i++)
{
if(a[x+c[i][0]][y+c[i][1]]=='E'&&b[x+c[i][0]][y+c[i][1]]==0)
{
k++;
x+=c[i][0],y+=c[i][1];
b[x][y]=1;
sort(a,x,y);
}
}
}
void main()
{
int i,j,m,n,x,y;
while(scanf("%d %d",&m,&n)!=EOF)
{
k=0;
int t=0;
char a[6][6];
for(i=0;i<m;i++)
{
scanf("%s",a[i]);
for(j=0;j<n;j++)
{
b[i][j]=0;
if(a[i][j]=='P')x=i,y=j,b[i][j]=1;
else if(a[i][j]=='E')t++;
}
}
sort(a,x,y);
if(k==t)printf("YES\n");
else printf("NO\n");
}
}