zoukankan      html  css  js  c++  java
  • hdu 1180 诡异的楼梯

    点击打开链接

    复杂bfs...

    需要讨论开始为 | 还是 - 

    #include"stdio.h"
    #include"string.h"
    #include"queue"
    using namespace std;
    int dir[4][2]={1,0,0,1,-1,0,0,-1};
    int map[21][21];
    char str[21][21];
    int n,m,sx,sy;
    struct node
    {
    	int x,y,step;
    };
    int judge(int x,int y)
    {
    	if(x>=0&&x<n&&y>=0&&y<m&&map[x][y]==0)
    		return 1;
    	return 0;
    }
    int fun(int x,int y,int xx,int yy,int step)
    {
    	if(xx>=0&&xx<n&&yy>=0&&y<m&&map[xx][yy]==0)
    	{
    		if(str[x][y]=='|')//开始为|
    		{
    			if(y==yy)//竖直过来
    			{
    				if(step%2==0)
    					return 1;
    				else
    					return 0;
    			}
    			else//水平过来
    			{
    				if(step%2==1)
    					return 1;
    				else
    					return 0;
    			}
    		}
    		else//开始为-
    		{
    			if(yy==y)//竖直过来
    			{
    				if(step%2==1)
    					return 1;
    				else
    					return 0;
    			}
    			else//水平过来
    			{
    				if(step%2==0)
    					return 1;
    				else
    					return 0;
    			}
    		}
    	}
    	return 0;
    }
    
    void bfs()
    {
    	int x,xx,y,yy,i;
    	node q,p;
    	queue<node>Q;
    	p.x=sx;
    	p.y=sy;
    	p.step=0;
    	memset(map,0,sizeof(map));
    	map[p.x][p.y]=1;
    	Q.push(p);
    	while(!Q.empty())
    	{
    		p=Q.front();
    		Q.pop();
    		for(i=0;i<4;i++)
    		{
    			x=p.x+dir[i][0];
    			y=p.y+dir[i][1];
    			if(str[x][y]=='.'||str[x][y]=='T')
    			{
    				if(judge(x,y))
    				{
    					map[x][y]=1;
    					q.x=x;
    					q.y=y;
    					q.step=p.step+1;
    					if(str[q.x][q.y]=='T')
    					{
    						printf("%d\n",q.step);
    						return ;
    					}
    					Q.push(q);
    				}
    			}
    			else if(str[x][y]=='|'||str[x][y]=='-')
    			{
    				xx=x+dir[i][0];
    				yy=y+dir[i][1];
    				if(str[xx][yy]!='*')
    				{
    					if(fun(x,y,xx,yy,p.step))
    					{
    						q.x=xx;
    						q.y=yy;
    						q.step=p.step+1;
    						map[q.x][q.y]=1;
    						if(str[xx][yy]=='T')
    						{
    							printf("%d\n",q.step);
    							return ;
    						}
    						Q.push(q);
    					}
    					else if(judge(xx,yy))
    					{
    						if(map[xx][yy]==0)
    						{
    							q.x=p.x;
    							q.y=p.y;
    							q.step=p.step+1;
    							map[q.x][q.y]=1;
    							Q.push(q);
    						}
    					}
    				}
    			}
    		}
    	}
    	printf("-1\n");
    	return ;
    }
    int main()
    {
    	int i,j;
    	while(scanf("%d%d",&n,&m)!=EOF)
    	{
    		for(i=0;i<n;i++)
    			scanf("%s",str[i]);
    		for(i=0;i<n;i++)
    		{
    			for(j=0;j<m;j++)
    				if(str[i][j]=='S')
    				{
    					sx=i;sy=j;
    				}
    		}
    		bfs();
    	}
    	return 0;
    }
    					
    


  • 相关阅读:
    Python进程池multiprocessing.Pool的用法
    基于opencv的车牌提取项目
    Srapy 爬取知乎用户信息
    Scrapy框架简介及小项目应用
    豆瓣爬取图书标签
    CSS选择器使用
    关于 urlencode 的使用和 json 模块的介绍
    urllib库使用方法
    猫眼电影的各种爬取方法
    淘宝商品信息爬取
  • 原文地址:https://www.cnblogs.com/yyf573462811/p/6365396.html
Copyright © 2011-2022 走看看