标签:des blog http io ar os sp for strong
Description
Input
Output
Sample Input
Sample Output
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cstdlib> #include<cmath> #include<algorithm> #include<queue> #include<vector> #include<set> using namespace std; int n,m,k; int x_s,y_s; int x_e,y_e; char map[111][111]; int step[10][111][111]; int dir[4][2]={0,1, 1,0, 0,-1, -1,0}; struct node { int f,x,y; }; int BFS() { queue<node>q; node now,next; int i; memset(step,-1,sizeof(step)); now.f=0; now.x=x_s; now.y=y_s; q.push(now); step[now.f][now.x][now.y]=0; while(!q.empty()) { now=q.front(); q.pop(); if(now.x==x_e && now.y==y_e) return step[now.f][now.x][now.y]; for(i=0;i<4;i++) { next.x=now.x+dir[i][0]; next.y=now.y+dir[i][1]; next.f=(now.f+1)%k; if(next.x<0 || next.x>=n || next.y<0 || next.y>=m) continue; if(map[next.x][next.y]==‘#‘ && next.f) continue; if(step[next.f][next.x][next.y]!=-1) continue; step[next.f][next.x][next.y]=step[now.f][now.x][now.y]+1; q.push(next); } } return -1; } int main() { int T; int i,l; int ans; scanf("%d",&T); while(T--) { scanf("%d%d%d",&n,&m,&k); for(i=0;i<n;i++) { scanf("%s",map[i]); for(l=0;map[i][l];l++) { if(map[i][l]==‘Y‘) {x_s=i;y_s=l;map[i][l]=‘.‘;} if(map[i][l]==‘G‘) {x_e=i;y_e=l;map[i][l]=‘.‘;} } } ans=BFS(); if(ans==-1) printf("Please give me another chance!\n"); else printf("%d\n",ans); } return 0; }
有点dp的味道,,写了一个 调了一晚上没调出来,,真是弱爆了,,上标程。。。。
标签:des blog http io ar os sp for strong
原文地址:http://www.cnblogs.com/a972290869/p/4109126.html