zoukankan      html  css  js  c++  java
  • HDU-1728 逃离迷宫

    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, x1, y1, x2, y2 (1 ≤ k ≤ 10, 1 ≤ x1, x2 ≤ n, 1 ≤ y1, y2 ≤ m),其中k表示gloria最多能转的弯数,(x1, y1), (x2, y2)表示两个位置,其中x1,x2对应列,y1, y2对应行。
     
    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

    AC代码:

    #include<queue>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<iostream>
    using namespace std;
    const int maxn = 110;
    char map[maxn][maxn];
    int vis[maxn][maxn];
    int n,m,x3,x4,y4,y3,k;
    int kk[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
    struct node{
    	int x,y,cost,pree;//x 横坐标 y纵坐标 cost转弯的最小数量 pree前面的方向 
    	node(int _x,int _y,int _cost,int _pree){
    		x = _x;
    		y = _y;
    		cost = _cost;
    		pree = _pree;
    	}
    };
    int bfs(){
    	queue<node>que;
    	memset(vis,0,sizeof(vis));
    	que.push(node(x3,y3,-1,-1));
    	vis[x3][y3] = 1;
    	while(!que.empty()){
    		node e = que.front();
    		que.pop();
    		for(int i = 0;i<4;i++){
    			node ee = e;
    			while(ee.x+kk[i][0]>=0&&ee.x+kk[i][0]<n&&ee.y+kk[i][1]>=0&&ee.y+kk[i][1]<m&&map[ee.x+kk[i][0]][ee.y+kk[i][1]]!='*'){
    				//printf("%c
    ",map[ee.x][ee.y]);
    				//printf("form %d(x) %d(y) to %d(x) %d(y)
    ",ee.x,ee.y,ee.x+kk[i][0],ee.y+kk[i][1]);
    				ee.x += kk[i][0];
    				ee.y += kk[i][1];
    				if(ee.pree!=i) ee.cost++;
    				ee.pree = i;
    				//这个判断语句不能放在while里****因为之前的路径可能挡在这个路径的前面, 
    				if(!vis[ee.x][ee.y]) que.push(ee);
    				if(ee.x == x4 && ee.y == y4) return ee.cost<=k;
    				vis[ee.x][ee.y] = 1;
    			}
    		}
    	}return 0;
    }
    int main()
    {
    	int t;
    	cin>>t;
    	while(t--)
    	{
    		scanf("%d %d",&n,&m);
    		getchar();
    		for(int i = 0;i<n;i++){
    			for(int j = 0;j<m;j++){
    				scanf("%c",&map[i][j]);
    			}
    			getchar();
    		}
    		//for(int i = 0;i<n;i++){
    		//	for(int j = 0;j<m;j++){
    		//		printf("%c ",map[i][j]);
    		//	}printf("
    ");
    		//}
    		scanf("%d %d %d %d %d",&k,&y3,&x3,&y4,&x4);
    		y3--;
    		x3--;
    		x4--;
    		y4--;
    		if(x3==x4&&y3==y4) {
    			printf("yes
    ");
    			continue;
    		}
    		int ans = bfs();
    		if(ans) printf("yes
    ");
    		else printf("no
    ");
    	}
    	
    	return 0;
     } 


  • 相关阅读:
    多选下拉框带搜索(aps.net)
    asp.net无刷新上传(带预览)
    http免费升级https 攻略(超简单)
    用JS获取地址栏参数的方法
    C#生成二维码
    update更新另一个字段
    自适应瀑布型布局(手机,PC全兼容)
    微信扫码支付.net版本
    常用css3技巧
    Repeater 嵌套,子级Repeater获取 父级Repeater 中的值
  • 原文地址:https://www.cnblogs.com/Nlifea/p/11746041.html
Copyright © 2011-2022 走看看