poj 3083 Children of the Candy Corn

http://poj.org/problem?id=3083
题目不难 主要是繁琐
#include
<iostream> #include<stdio.h> #include<string.h> #include<string> #include<queue> #include<algorithm> #include<map> using namespace std; const int N=50; int x[4]={0,-1,0,1}; int y[4]={-1,0,1,0}; int LL[4]={-1,0,1,2}; int RR[4]={1,0,-1,2}; int stx,sty,ndx,ndy; bool field[N][N]; bool had[N][N]; int ans1,ans2,ans3; int n,m; char ctemp; void bfs() { memset(had,false,sizeof(had)); queue<int>strx; queue<int>stry; queue<int>step; strx.push(stx); stry.push(sty); step.push(1); had[stx][sty]=true; while(!strx.empty()) { int x1,y1,step1,l1,l2; x1=strx.front(); y1=stry.front(); step1=step.front(); strx.pop();stry.pop();step.pop(); for(int i=0;i<4;++i) { //cout<<x1<<" "<<y1<<endl; l1=x1+x[i];l2=y1+y[i]; //cout<<l1<<" "<<l2<<endl; if(l1==ndx&&l2==ndy) { ans3=step1+1; return; } if(!had[l1][l2]&&field[l1][l2]) { had[l1][l2]=true; //cout<<l1<<" "<<l2<<" "<<(step1+1)<<endl; strx.push(l1); stry.push(l2); step.push(step1+1); } } } } bool ok(int i,int j,int k1,int &i1,int &j1) { i1=i+x[k1]; j1=j+y[k1]; if(field[i1][j1]) return true; return false; } void dfs(char LR,int i,int j,int k,int stepn) { int i1,j1,k1; k=k+4; for(int l=0;l<4;++l) { if(LR=='L') k1=(k+LL[l])%4; else k1=(k+RR[l])%4; //cout<<k<<" "<<k1<<endl; if(ok(i,j,k1,i1,j1)) {//cout<<i<<" "<<j<<" "<<i1<<" "<<j1<<endl; if(i1==ndx&&j1==ndy) { if(LR=='L') ans1=stepn+1; else ans2=stepn+1; return; } dfs(LR,i1,j1,k1,stepn+1); return; } } return ; } int main() { int T; cin>>T; while(cin>>n>>m) { memset(field,false,sizeof(field)); for(int i=1;i<=m;++i) { for(int j=1;j<=n;++j) { cin>>ctemp; if(ctemp!='#') { field[i][j]=true; } if(ctemp=='S') { stx=i; sty=j; }else if(ctemp=='E') { ndx=i; ndy=j; } } } int k; if(field[stx+1][sty]) { k=3; }else if(field[stx][sty+1]) { k=2; }else if(field[stx-1][sty]) { k=1; }else { k=0; } dfs('L',stx,sty,k,1); dfs('R',stx,sty,k,1); bfs(); cout<<ans1<<" "<<ans2<<" "<<ans3<<endl; } return 0; }

你可能感兴趣的:(children)