Light OJ 1377 - Blade and Sword (BFS)
来源:互联网 发布:淘宝无线套餐链接转换 编辑:程序博客网 时间:2024/06/05 02:16
1377 - Blade and Sword
Description
You may have played the game 'Blade and Sword', it's an action game. However, in this problem we are actually solving one stage of the game.
For simplicity, assume that the game stage can be modeled as a 2D grid which hasm rows andn columns. The cells are categorized as follows:
1) '.' represents an empty space. The player can move through it.
2) '#' represents wall, and the player cannot move through it. You can assume that the boundaries of the grid will be walls.
3) '*' means a teleporting cell, once the player moves into this cell, he must chooseany other teleporting cell where he will be taken to. But if he cannot find a desired teleporting cell, he will die. However, after moving to the desired teleporting cell, he can either move to an adjacent cell, or he can teleport again using the same procedure.
4) 'P' means the position of the player and there will be exactly one cell containing 'P'.
5) 'D' means the destination cell and there will be exactly one cell containing'D'.
Now you are given a stage and the player starts moving. It takes one unit of time for the player to move to any adjacent cell from his current position. Two cells are adjacent if they share a side. One unit of time is needed for the teleporting service; that means taking the player from one teleporting cell to any other teleporting cell. Your task is to find the minimum possible time unit required for the player to reach the destination cell.
Input
Input starts with an integer T (≤ 100), denoting the number of test cases.
Each case starts with a line containing two integers: m and n (3 ≤ m, n ≤ 200). Each of the next m lines contains n characters denoting the stage. The given stage follows the restrictions stated above.
Output
For each case, print the case number and the minimum required time. If it's impossible for the player to reach the destination cell, print'impossible'. See the samples for details.
Sample Input
2
4 10
##########
#.P..#*..#
#*......D#
##########
3 9
#########
#P.#..D.#
#########
Sample Output
Case 1: 6
Case 2: impossible
题意:一个图,上面可能会有细胞,如果你走上这个细胞你必须传送到其他的细胞上,不然你就会死亡,求走到目标位置的最小步数
思路:这道题当时题意读错了,以为细胞之间可以无限传送,后来才知道不是的,走上细胞后有两种选择:
1.传送到下一个细胞上,然后进行移动且以后不再进行传送
2.传送待下一个细胞上,然后再传送回来,然后以后不再进行传送
过程其实也不难实现,在步数的结构体里定义一个k记录是从什么地方来的,如果是1则说明是传送过来的,当传送过后,清空细胞数组就行了。。(PS:因为题意没读清,WA了10几次,以后要注意了)
ac代码:
#include<stdio.h>#include<math.h>#include<string.h>#include<stack>#include<queue>#include<vector>#include<iostream>#include<algorithm>#define MAXN 44444#define LL long long#define INF 0xfffffff#define fab(x) (x)>0?(x):(-x)#define mem(x) memset(x,0,sizeof(x))#define PI acos(-1)using namespace std;struct s{int x;int y;int step;int k;};struct ss{int x;int y;}cell[MAXN];char map[210][210];int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};int v[2][210][210];int bx,by,ex,ey;int cnt,bz,n,m;int cellx,celly;int check(s aa){if(aa.x<0||aa.x>=n||aa.y<0||aa.y>=m||v[0][aa.x][aa.y])return 1;if(map[aa.x][aa.y]=='#')return 1;return 0;}void bfs(int xx,int yy){s a,b;int i,j;queue<s>q;a.x=xx;a.y=yy;a.step=0;a.k=0;mem(v);v[0][xx][yy]=1;q.push(a);while(!q.empty()){a=q.front();q.pop();if(a.x==ex&&a.y==ey){bz=1;printf("%d\n",a.step);return;}b.step=a.step+1;if(map[a.x][a.y]=='*'){int flag=0;for(i=0;i<cnt;i++){if(cell[i].x==a.x&&cell[i].y==a.y){flag=1;continue;}b.x=cell[i].x;b.y=cell[i].y;b.k=1;if(!v[1][b.x][b.y]){v[1][b.x][b.y]=1; q.push(b); }}if(flag){cnt=1;cell[0].x=a.x;cell[0].y=a.y;}if(a.k==0)continue;}for(i=0;i<4;i++){b.x=a.x+dir[i][0];b.y=a.y+dir[i][1];if(check(b))continue;v[0][b.x][b.y]=1;b.k=0;q.push(b);}}}int main(){int t,i,j;int cas=0;scanf("%d",&t);while(t--){scanf("%d%d",&n,&m);cnt=0;for(i=0;i<n;i++){scanf("%s",map[i]);for(j=0;j<m;j++){if(map[i][j]=='P')bx=i,by=j;else if(map[i][j]=='D')ex=i,ey=j;else if(map[i][j]=='*')cell[cnt].x=i,cell[cnt].y=j,cnt++;}}bz=0;printf("Case %d: ",++cas);bfs(bx,by);if(bz==0)printf("impossible\n");}return 0;}献上我的几组测试数据:10004 4####.P#.*#*D#...4 5#####.P#.**#*D.#....20 20..........................................................*..P..........................................................................D...................................................................*...................................................................................................................4 4#..#D#*P.*#.....4 10###########.P..#*..##*......D###########3 9##########P.#..D.##########
- Light OJ 1377 - Blade and Sword (BFS)
- LA 5966 Blade and Sword (双向bfs + 想法) - from lanshui_Yang
- UVALive 5968 Blade and Sword(搜索)
- ACM 图论 搜索 SPFA UVALive 5966 Blade and Sword
- !Light oj 1066--BFS--(队列溢出)
- Light oj 1046 - Rider (BFS)
- Light OJ 1055 BFS
- Light OJ 1141--BFS--(隐蔽的BFS)
- Light OJ 1066(bfs+坑)
- Light OJ 1238--BFS(第一道自己做的)
- light oj 1149 - Factors and Multiples (二分匹配)
- Light OJ 1282 Leading and Trailing (对数+快速幂)
- 【Light oj】1282 - Leading and Trailing
- E - Leading and Trailing Light oj 1282
- Light-oj 1282 - Leading and Trailing
- Light OJ 1282 Leading and Trailing
- light oj-1282 Leading and Trailing
- light oj 1066Gathering Food (bfs 稍微有点小坑)
- linux那些命令
- 键盘输入缓存的清理
- Python: 消除print的自动换行
- 《剑指offer》——二维数组中的查找
- 【查找算法】——顺序查找、折半查找、分块查找(索引查找)
- Light OJ 1377 - Blade and Sword (BFS)
- 杭电2037-今年暑假不AC(贪心)
- Maven隐士变量
- 关于viewController中的tableview的cell在pop回来后仍有选中效果的解决方法
- 3:输入一个整数,求这个数的阶乘
- C# 多个Main()方法
- 面试题6:从尾到头打印链表(offer)
- Google的可用Hosts文件
- 【】