Time Limit : 1000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 29 Accepted Submission(s) : 6
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有些地方是障碍,她必须绕行,从迷宫的一个位置,只能走到与它相邻的4个位置中,当然在行走过程中,gloria不能走到迷宫外面去。令人头痛的是,gloria是个没什么方向感的人,因此,她在行走过程中,不能转太多弯了,否则她会晕倒的。我们假定给定的两个位置都是空地,初始时,gloria所面向的方向未定,她可以选择4个方向的任何一个出发,而不算成一次转弯。gloria能从一个位置走到另外一个位置吗?
Input
第1行为一个整数t (1 ≤ t ≤ 100),表示测试数据的个数,接下来为t组测试数据,每组测试数据中,
第1行为两个整数m, n (1 ≤ m, n ≤ 100),分别表示迷宫的行数和列数,接下来m行,每行包括n个字符,其中字符'.'表示该位置为空地,字符'*'表示该位置为障碍,输入数据中只有这两种字符,每组测试数据的最后一行为5个整数k, x 1, y 1, x 2, y 2 (1 ≤ k ≤ 10, 1 ≤ x 1, x 2 ≤ n, 1 ≤ y 1, y 2 ≤ m),其中k表示gloria最多能转的弯数,(x 1, y 1), (x 2, y 2)表示两个位置,其中x 1,x 2对应列,y 1, y 2对应行。
第1行为两个整数m, n (1 ≤ m, n ≤ 100),分别表示迷宫的行数和列数,接下来m行,每行包括n个字符,其中字符'.'表示该位置为空地,字符'*'表示该位置为障碍,输入数据中只有这两种字符,每组测试数据的最后一行为5个整数k, x 1, y 1, x 2, y 2 (1 ≤ k ≤ 10, 1 ≤ x 1, x 2 ≤ n, 1 ≤ y 1, y 2 ≤ m),其中k表示gloria最多能转的弯数,(x 1, y 1), (x 2, y 2)表示两个位置,其中x 1,x 2对应列,y 1, y 2对应行。
Output
每组测试数据对应为一行,若gloria能从一个位置走到另外一个位置,输出“yes”,否则输出“no”。
Sample Input
2 5 5 ...** *.**. ..... ..... *.... 1 1 1 1 3 5 5 ...** *.**. ..... ..... *.... 2 1 1 1 3
Sample Output
no yes
Source
“网新恩普杯”杭州电子科技大学程序设计邀请赛
#include<iostream> #include<cstring> #include<cstdio> #include<cstdlib> #include<algorithm> #include<queue> #include<stack> #include<cmath> using namespace std; #define inf 99999999 #define maxn 105 /* 和传统搜索方法不同, 相对于以前的一步搜索,对状态的权重并没有改进, 也就是所走一步或走两步s步其转弯次数没有变化。。。 所以对于queue结构,应该一次性把一个方向的节点(条件允许) 全部入栈并标记(判断边界或障碍物等都是必须的) 否则如果一次一步的话会超内存或时间。 为什么这样会更优呢? 可以通过和以前BFS类比的方法, 以前的一步迭代搜,每次搜一步的原因是以步数为参考值, 也就是一步的空间其实并不是一步,而是这个方向上的最小权重集合(可重集) ,基于此,在这题上我们不难想到,这样的方法是更高效的。。。 */ int n,m; char c; int k,sx,sy,ex,ey; int mp[maxn][maxn]; struct Node { int x,y,cnt; Node(int p=0,int q=0,int t=-1) { x=p,y=q; cnt=t; } }; int dir1[]={-1,0,1,0}; int dir2[]={0,-1,0,1};// bool judge(int x,int y) { if(x<0||x>=n) return false; if(y<0||y>=m) return false; return true; } int vis[maxn][maxn]; int BFS() { memset(vis,0,sizeof(vis)); int tx,ty;Node tmp; queue<Node> seq; seq.push(Node(sx,sy)); vis[sx][sy]=1; while(!seq.empty()) { Node t=seq.front(); seq.pop(); //if(t.cnt>k) continue; // if(t.x!=ex&&t.y!=ey&&t.cnt+1>k) continue; // if(t.x==ex&&t.y==ey) cout<<t.cnt<<endl; if(t.x==ex&&t.y==ey&&t.cnt<=k) return 1; //int state= compute(t.x,t.y,ex,ey) ; // if(state==2) if(t.cnt+2>k) continue; // if(state==1) return 1; for(int i=0;i<4;i++) { tx=t.x+dir1[i]; ty=t.y+dir2[i]; while(tx>=0&&tx<=n-1&&ty>=0&&ty<=m-1&&mp[tx][ty]!=1) { if(vis[tx][ty]==0) { vis[tx][ty]=1; seq.push(Node(tx,ty,t.cnt+1)); // cout<<tx<<" "<<ty<<endl; } tx+=dir1[i]; ty+=dir2[i]; } /* if(mp[tx][ty]||vis[tx][ty]) continue; if(!judge(tx,ty)) continue; tmp=Node(tx,ty); tmp.cnt=t.cnt; tmp.dir=i; if(t.dir!=-1&&t.dir!=i) tmp.cnt++; vis[tx][ty]=cnt; seq.push(tmp); */ } } return 0; } int main() { ios::sync_with_stdio(false); int t;cin>>t; while(t--) { //scanf("%d%d",&n,&m);getchar(); cin>>n>>m; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { cin>>c; //scanf("%c",&c); if(c=='.') mp[i][j]=0; else mp[i][j]=1;//0代表空地,1代表障碍 } //getchar(); } cin>>k>>sy>>sx>>ey>>ex; sy--,sx--,ex--,ey--; //scanf("%d%d%d%d&d",&k,&sx,&sy,&ex,&ey); //getchar(); if(BFS()) printf("yes\n"); else printf("no\n"); } return 0; }