zoukankan      html  css  js  c++  java
  • HDOJ-三部曲一(搜索、数学)-1002-Children of the Candy Corn

    Children of the Candy Corn

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
    Total Submission(s) : 20   Accepted Submission(s) : 10
    Problem Description
    The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through the maze facing zombies, chainsaw-wielding psychopaths, hippies, and other terrors on their quest to find the exit. 
    One popular maze-walking strategy guarantees that the visitor will eventually find the exit. Simply choose either the right or left wall, and follow it. Of course, there's no guarantee which strategy (left or right) will be better, and the path taken is seldom the most efficient. (It also doesn't work on mazes with exits that are not on the edge; those types of mazes are not represented in this problem.) 
    As the proprieter of a cornfield that is about to be converted into a maze, you'd like to have a computer program that can determine the left and right-hand paths along with the shortest path so that you can figure out which layout has the best chance of confounding visitors.
     
    Input
    Input to this problem will begin with a line containing a single integer n indicating the number of mazes. Each maze will consist of one line with a width, w, and height, h (3 <= w, h <= 40), followed by h lines of w characters each that represent the maze layout. Walls are represented by hash marks ('#'), empty space by periods ('.'), the start by an 'S' and the exit by an 'E'. 
    Exactly one 'S' and one 'E' will be present in the maze, and they will always be located along one of the maze edges and never in a corner. The maze will be fully enclosed by walls ('#'), with the only openings being the 'S' and 'E'. The 'S' and 'E' will also be separated by at least one wall ('#'). 
    You may assume that the maze exit is always reachable from the start point.
     
    Output
    For each maze in the input, output on a single line the number of (not necessarily unique) squares that a person would visit (including the 'S' and 'E') for (in order) the left, right, and shortest paths, separated by a single space each. Movement from one square to another is only allowed in the horizontal or vertical direction; movement along the diagonals is not allowed.
     
    Sample Input
    2
    8 8
    ########
    #......#
    #.####.#
    #.####.#
    #.####.#
    #.####.#
    #...#..#
    #S#E####
    9 5
    #########
    #.#.#.#.#
    S.......E
    #.#.#.#.#
    #########
     
    Sample Output
    37 5 5
     
    17 17 9
     
    Source
    PKU
     
     
     
    BFS,感觉这道题真挺麻烦的,写得很长....
     
     
    #include<iostream>
    #include<cstring>
    using namespace std;
    
    int w,h,stepl=1,stepr=1,step[40][40],r,c;
    char maze[40][41];
    
    void forward(int &i,int &j,int k)
    {
    	if(k==1)
    		i--;
    	else if(k==2)
    		j++;
    	else if(k==3)
    		i++;
    	else if(k==4)
    		j--;
    }
    
    bool judge(int k)
    {
    	int i=r,j=c;
    	forward(i,j,k);
    	if(maze[i][j]=='#')
    		return false;
    	else
    		return true;
    }
    
    int leftside(int k)
    {
    	k--;
    	if(k==0)
    		k=4;
    	while(!judge(k))
    	{
    		k++;
    		if(k==5)
    			k=1;
    	}
    	forward(r,c,k);
    	stepl++;
    	if(maze[r][c]=='E')
    		return stepl;
    	return leftside(k);
    }
    
    
    int rightside(int k)
    {
    	k++;
    	if(k==5)
    		k=1;
    	while(!judge(k))
    	{
    		k--;
    		if(k==0)
    			k=4;
    	}
    	forward(r,c,k);
    	stepr++;
    	if(maze[r][c]=='E')
    		return stepr;
    	return rightside(k);
    }
    
    
    
    int BFS()
    {
    	int que[1600*2][2];
    	int front=0,rear=1;
    	bool f[40][41]={false};
    	que[0][0]=r;
    	que[0][1]=c;
    	f[r][c]=true;
    	while(front<rear)
    	{
    		int t=step[que[front][0]][que[front][1]];
    		if(que[front][0]-1>=0&&maze[que[front][0]-1][que[front][1]]!='#'&&!f[que[front][0]-1][que[front][1]])
    		{
    			que[rear][0]=que[front][0]-1;
    			que[rear][1]=que[front][1];
    			step[que[rear][0]][que[rear][1]]=t+1;
    			if(maze[que[rear][0]][que[rear][1]]=='E')
    				return step[que[rear][0]][que[rear][1]];
    			f[que[rear][0]][que[rear][1]]=true;
    			rear++;
    		}
    		if(que[front][1]+1<w&&maze[que[front][0]][que[front][1]+1]!='#'&&!f[que[front][0]][que[front][1]+1])
    		{
    			que[rear][0]=que[front][0];
    			que[rear][1]=que[front][1]+1;
    			step[que[rear][0]][que[rear][1]]=t+1;
    			if(maze[que[rear][0]][que[rear][1]]=='E')
    				return step[que[rear][0]][que[rear][1]];
    			f[que[rear][0]][que[rear][1]]=true;
    			rear++;
    		}
    		if(que[front][0]+1<h&&maze[que[front][0]+1][que[front][1]]!='#'&&!f[que[front][0]+1][que[front][1]])
    		{
    			que[rear][0]=que[front][0]+1;
    			que[rear][1]=que[front][1];
    			step[que[rear][0]][que[rear][1]]=t+1;
    			if(maze[que[rear][0]][que[rear][1]]=='E')
    				return step[que[rear][0]][que[rear][1]];
    			f[que[rear][0]][que[rear][1]]=true;
    			rear++;
    		}
    		if(que[front][1]-1>=0&&maze[que[front][0]][que[front][1]-1]!='#'&&!f[que[front][0]][que[front][1]-1])
    		{
    			que[rear][0]=que[front][0];			
    			que[rear][1]=que[front][1]-1;
    			step[que[rear][0]][que[rear][1]]=t+1;
    			if(maze[que[rear][0]][que[rear][1]]=='E')
    				return step[que[rear][0]][que[rear][1]];
    			f[que[rear][0]][que[rear][1]]=true;
    			rear++;
    		}
    		front++;
    	}
    }
    
    
    int main()
    {
    	int T;
    	cin>>T;
    	while(T--)
    	{
    		int i,j,si,sj,k;
    		stepl=1;
    		stepr=1;
    		memset(step,0,sizeof(step));
    		cin>>w>>h;
    		for(i=0;i<h;i++)
    		{
    			for(j=0;j<w;j++)
    			{
    				cin>>maze[i][j];
    				if(maze[i][j]=='S')
    				{
    					si=i;
    					sj=j;
    				} 
    			}
    		}
    		r=si;
    		c=sj;
    		if(r-1>=0&&maze[r-1][c]=='.')
    			k=1;
    		else if(c+1<w&&maze[r][c+1]=='.')
    			k=2;
    		else if(r+1<h&&maze[r+1][c]=='.')
    			k=3;
    		else if(c-1>=0&&maze[c-1][r]=='.')
    			k=4;
    		cout<<leftside(k)<<' ';
    		r=si,c=sj;
    		cout<<rightside(k)<<' ';
    		r=si,c=sj;
    		step[r][c]=1;
    		cout<<BFS()<<endl;
    	}
    }
    
  • 相关阅读:
    element-ui el-tree竖向滚动条和横向滚动条问题
    菜单加滚动条相关样式
    echarts tree 树图总结,点击父节点动态生成子节点,树图数据过多高度自适应,点击子节点跳转页面。
    基于Mininet测量路径的损耗率
    RyuBook1.0案例一:Switching Hub项目源码分析
    深度学习之稠密连接⽹络(DENSENET)
    图形学之图像信号处理
    图形学之卷积滤波器
    图形学之信号处理
    Lecture13_光线追踪1(Whitted-Style Ray Tracing)_GAMES101 课堂笔记
  • 原文地址:https://www.cnblogs.com/aljxy/p/3332185.html
Copyright © 2011-2022 走看看