use MathJax to parse formulas
Description
500年前,Jesse是我国最卓越的剑客。他英俊潇洒,而且机智过人^_^。
突然有一天,Jesse心爱的公主被魔王困在了一个巨大的迷宫中。Jesse听说这个消息已经是两天以后了,他知道公主在迷宫中还能坚持T天,他急忙赶到迷宫,开始到处寻找公主的下落。 时间一点一点的过去,Jesse还是无法找到公主。最后当他找到公主的时候,美丽的公主已经死了。从此Jesse郁郁寡欢,茶饭不思,一年后追随公主而去了。T_T 500年后的今天,Jesse托梦给你,希望你帮他判断一下当年他是否有机会在给定的时间内找到公主。
他会为你提供迷宫的地图以及所剩的时间T。请你判断他是否能救出心爱的公主。
Input
题目包括多组测试数据。 每组测试数据以三个整数N,M,T(0<n, m≤20, t>0)开头,分别代表迷宫的长和高,以及公主能坚持的天数。 紧接着有M行,N列字符,由".","*","P","S"组成。其中 "." 代表能够行走的空地。 "*" 代表墙壁,Jesse不能从此通过。 "P" 是公主所在的位置。 "S" 是Jesse的起始位置。 每个时间段里Jesse只能选择“上、下、左、右”任意一方向走一步。 输入以0 0 0结束。
Output
如果能在规定时间内救出公主输出“YES”,否则输出“NO”。
Sample Input
4 4 10 .... .... .... S**P 0 0 0
Sample Output
YES
思路:因为有时间的要求,所以不能用dfs,深搜只能确定能不能走到
代码:
#include <iostream> #include<stdio.h> #include<string.h> #include<algorithm> #include<queue> using namespace std; char s[25][25]; int vis[25][25]; int a[4]={0,0,1,-1}; int b[4]={-1,1,0,0}; int n,m,t,X,Y; struct note{ int x; int y; int step; }; note front_head; //起点的坐标和步数 int bfs() { queue<note> q; q.push(front_head); note next_queue; note now_head; while(!q.empty()) { now_head=q.front(); q.pop(); if(now_head.x==X&&now_head.y==Y) //检查是否是终点 return now_head.step; for(int i=0;i<4;i++) { next_queue.x=now_head.x+a[i]; //下一个点的坐标和步数 next_queue.y=now_head.y+b[i]; if(next_queue.x>=1&&next_queue.x<=m&&next_queue.y>=0&&next_queue.y<n&&vis[next_queue.x][next_queue.y]==0)//是否出界是否被访问过是否是* { vis[next_queue.x][next_queue.y]=1; //表示被访问了 next_queue.step = now_head.step+1; //步数加1 q.push(next_queue); //存入队列 } } } return -1; } int main() { int i,j,ans; while(scanf("%d%d%d",&n,&m,&t)!=EOF) { front_head.step=0; if(n==0&&m==0&t==0) break; getchar(); memset(vis,0,sizeof(vis)); for(i=1;i<=m;i++) scanf("%s",&s[i]); for(i=1;i<=m;i++) for(j=0;j<n;j++) { if(s[i][j]=='S') { vis[i][j]=1; front_head.x=i; front_head.y=j; } if(s[i][j]=='P') { X=i; Y=j; } if(s[i][j]=='*') //被标记 不再访问 vis[i][j]=1; } ans=bfs(); if(ans<=t&&ans!=-1) printf("YES\n"); else printf("NO\n"); } return 0; }
广搜模板代码
struct
note{
int x; //横坐标
int step; //步数
int y; //纵坐标
};
void BFS(note front_head) //BFS
{
queue<note> Q; //建立空队列
Q.push(front_head); //将起始点加入队列
note next_queue; //下一个队列元素
note now_head; //当前队头元素
while (!Q.empty()) //循环条件是队列不为空
{
now_head = Q.front(); //找到当前队头元素
Q.pop(); //已经找到则出队
for (i = 0 ; i < 8 ; i ++) //搜索8个方向
{
next_queue.x = now_head.x + NEXT[i][ 0 ];
next_queue.y = now_head.y + NEXT[i][ 1 ];
if (next_queue.x < 0 ||next_queue.y < 0 ||next_queue.x > 7 ||next_queue.y > 7 )
continue ;
if (book[next_queue.x ][next_queue.y ]!= 1 ) //没有走过该点
{
book[next_queue.x ][next_queue.y ] = 1 ;
next_queue.step = now_queue.step+ 1 ;
Q.push(next_queue); //将找到满足条件的下一个队列元素加入队尾
//如果下一个队列元素满足终止条件,结束函数
if (next_queue.x == end_i&&next_queue.y == end_j)
{
step = next_queue.step;
return ;
}
}
}
}
return ;
}
int x; //横坐标
int step; //步数
int y; //纵坐标
};
void BFS(note front_head) //BFS
{
queue<note> Q; //建立空队列
Q.push(front_head); //将起始点加入队列
note next_queue; //下一个队列元素
note now_head; //当前队头元素
while (!Q.empty()) //循环条件是队列不为空
{
now_head = Q.front(); //找到当前队头元素
Q.pop(); //已经找到则出队
for (i = 0 ; i < 8 ; i ++) //搜索8个方向
{
next_queue.x = now_head.x + NEXT[i][ 0 ];
next_queue.y = now_head.y + NEXT[i][ 1 ];
if (next_queue.x < 0 ||next_queue.y < 0 ||next_queue.x > 7 ||next_queue.y > 7 )
continue ;
if (book[next_queue.x ][next_queue.y ]!= 1 ) //没有走过该点
{
book[next_queue.x ][next_queue.y ] = 1 ;
next_queue.step = now_queue.step+ 1 ;
Q.push(next_queue); //将找到满足条件的下一个队列元素加入队尾
//如果下一个队列元素满足终止条件,结束函数
if (next_queue.x == end_i&&next_queue.y == end_j)
{
step = next_queue.step;
return ;
}
}
}
}
return ;
}