[PKU] 2157 没做完的题目

#include<iostream>
#include<string>
char map[25][25];
int n, m;
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};  
int kn[5];
int kg[5];
bool fd;
int open;
using namespace std;
void visit(char s)
{
	int i, j;
	for(i=1;i<=n;i++)
		for(j=1;j<=m;j++)
			if(map[i][j]==s)
				map[i][j] = '.';

}
void dfs(int si, int sj) 
{
	int i, Mx, My;
	char plc;
	if(fd)
		return;

	for(i=0;i<4;i++)
	{ 
		Mx = si+dir[i][0];
		My = sj+dir[i][1];
		if(Mx>n||My>m||Mx<=0||My<=0) 
			continue; 
		plc = map[Mx][My];
		if(plc=='G')
		{
			fd = 1;
			return;
		}
		if(plc!='X')
		{ 

			if(plc>='a'&&plc<='e') 
			{
				kg[plc-'a']++;
				if(kg[plc-'a']==kn[plc-'a'])
				{
					visit(plc-32);
					open = 1;
					return;
				}
			}
			else if(plc>='A'&&plc<='E')
				continue;
			map[Mx][My]='X'; 
			dfs(Mx,My); 
			map[Mx][My]='.'; 
		}
	} 
	return; 
}
int main()
{
	int i, j, Sx, Sy; 
	while(scanf("%d%d", &n, &m), n+m)
	{ 
		fd = 0; 
		open = 1;
		memset(kn,0,sizeof(kn));
		memset(kg,0,sizeof(kg));
		for(i=1;i<=n;i++) 
		{
			for(j=1;j<=m;j++)
			{ 
				cin>>map[i][j];
				if(map[i][j]=='S') 
				{
					Sx=i;
					Sy=j; 
				} 
				else if(map[i][j]>='a'&&map[i][j]<='e') 
					kn[map[i][j]-'a']++;
			} 
		}
		map[Sx][Sy]='X';
		while(open)
		{
			open = 0;
			dfs(Sx,Sy);
		}
		for(i=1;i<=n;i++)
		{
			for(j=1;j<=m;j++)
				cout<<map[i][j];
			cout<<endl;
		}
		dfs(Sx,Sy);
		printf(fd?"YES\n":"NO\n");
	} 
	return 0; 
}

你可能感兴趣的:([PKU] 2157 没做完的题目)