1 6 6 2 ...Y.. ...#.. .#.... ...#.. ...#.. ..#G#.
7
#include<map>
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int MAX = 105 ;
struct edge{ int x,y,step; };
int xy[4][2]={{0,-1},{-1,0},{0,1},{1,0}};
int T,r,c,k,sx,sy,ex,ey;
char str[MAX][MAX];
int vis[MAX][MAX][15];
bool check(edge v){
if(v.x>=0&&v.x<r&&v.y>=0&&v.y<c&&!vis[v.x][v.y][v.step%k]){
if(str[v.x][v.y]!='#')
return true;
if(str[v.x][v.y]=='#'&&v.step%k==0)
return true;
}
return false;
}
void bfs(){
queue<edge>q;
while(q.size()) q.pop();
memset(vis,0,sizeof(vis));
edge p,w;
p.x=sx;p.y=sy;p.step=0;
vis[p.x][p.y][p.step%k]=1;
q.push(p);
while(q.size()){
p=q.front();q.pop();
if(p.x==ex&&p.y==ey){
printf("%d\n",p.step);
return ;
}
for(int i=0;i<4;i++){
w.x=p.x+xy[i][0];
w.y=p.y+xy[i][1];
w.step=p.step+1;
if(check(w)){
vis[w.x][w.y][w.step%k]=1;
q.push(w);
}
}
}
printf("Please give me another chance!\n");
}
int main(){
scanf("%d",&T);
while(T--){
scanf("%d%d%d",&r,&c,&k);
for(int i=0;i<r;i++){
scanf("%s",str[i]);
for(int j=0;j<c;j++){
if(str[i][j]=='Y'){ sx=i;sy=j; }
if(str[i][j]=='G'){ ex=i;ey=j; }
}
}
bfs();
}return 0;
}
HDU_2579_Dating with girls(2) (DFS)
原文地址:http://blog.csdn.net/jhgkjhg_ugtdk77/article/details/44816393