广搜模版

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <string>
#include <cstring>
#include <sstream>
using namespace std;

#define input freopen("input.txt","r",stdin)
#define output freopen("output.txt","r",stdout)
#define For1(i,a,b) for (i=a;i<b;i++)
#define For2(i,a,b) for (i=a;i<=b;i++)
#define Dec(i,a,b) for (i=a;i>b;i--)
#define Dec2(i,a,b) for (i=a;i>=b;i--)
#define Sca(x) scanf("%d",&x)
#define Fill(x,a) memset(x,a,sizeof(x))
#define MAXN 0x7fffffff

struct node
{
	int x,y,time;
}que[100];

int dir_x[]={0,1,-1,0,0};
int dir_y[]={0,0,0,1,-1};
int map[10][10];
bool book[10][10];
int main() 
{
	int n,m,t,i,j,k;
	int sx,sy,ex,ey;//sx==startx,sy==starty,ex==endx,ey==endy
	char in[10];
	while(cin>>n>>m>>t)
	{
		Fill(map,0);
		Fill(book,0);
		if (!n&&!m&&!t) break;
		For2(i,1,n)
		{
			cin>>in;
			For2(j,1,m)
				if (in[j-1]=='S')
					map[i][j]=0,sx=i,sy=j;
				else if (in[j-1]=='D')
					map[i][j]=0,ex=i,ey=j;
				else if (in[j-1]=='X')
					map[i][j]=1;
		}//如何处理字符 
		
		if (sx==ex&&sy==ey)
		{
			cout<<"YES"<<endl;
			continue;
		}
			
		int head=1,tail=1;
		que[head].x=sx,que[head].y=sy,que[head].time=0;
		book[sx][sy]=1;
		
		while(head<=tail)
		{
			int newx,newy;
			For2(k,1,4)//四种方向 
			{
				newx=que[head].x+dir_x[k];
				newy=que[head].y+dir_y[k];
				if (newx>0&&newx<=n&&newy>0&&newy<=m&&!book[newx][newy]&&!map[newx][newy])
				//在地图之内,未访问过,地图可走 
				{
					book[newx][newy]=1;
					tail++;
					que[tail].x=newx;
					que[tail].y=newy;
					que[tail].time=que[head].time+1;
					if (newx==ex&&newy==ey)
						if (que[tail].time<=t)
							cout<<"YES"<<endl,tail=100;//结束条件 
						else
							cout<<"NO"<<endl,tail=100;//结束条件 
				}
			}
			head++;
		}
	}
	return 0;
}

你可能感兴趣的:(Algorithm,ACM)