• POJ 3083 相对位置的DFS的变形和BFS


    Children of the Candy Corn
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 12755   Accepted: 5481

    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
    #include<cmath>
    #include<queue>
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    #define N 45
    struct P{
    	int x;
    	int y;
    	int step;
    };
    int t[N][N];
    int w,h,a,b,sum;
    char c[N][N];
    int d1[4]={-1,0,1,2};
    int d2[4]={1,0,-1,2};
    int dir[4][2]={{0,-1},{-1,0},{0,1},{1,0}};
    bool dfs(int x1,int y1,int t,int *d)
    {
    	for(int i=0;i<4;i++)
    	{
    		int t1=(t+4+d[i])%4;
    		int xx=x1+dir[t1][0];
    		int yy=y1+dir[t1][1];
    		if(xx==a&&yy==b)
    			return true;
    		if(xx>=0&&xx<h&&yy>=0&&yy<w&&c[xx][yy]=='.')
    		{
    			sum++;
    			if(dfs(xx,yy,t1,d))
    				return true;
    
    		}
    	}
    	return false;
    }
    void bfs(int x1,int y1)
    {
    	queue<P> q;
    	P p1,p2;
    	p1.x=x1;
    	p1.y=y1;
    	p1.step=1;
    	t[x1][y1]=1;
    	q.push(p1);
    	while(!q.empty())
    	{
    		p1=q.front();
    		q.pop();
    		for(int i=0;i<4;i++)
    		{
    			int xx=p1.x+dir[i][0];
    			int yy=p1.y+dir[i][1];
    			if(xx==a&&yy==b)
    			{
    				printf("%d
    ",p1.step+1);
    				break;
    			}
    			if(xx>=0&&xx<h&&yy>=0&&yy<w&&c[xx][yy]=='.'&&!t[xx][yy])
    			{
    				t[xx][yy]=1;
    				p2.x=xx;
    				p2.y=yy;
    				p2.step=p1.step+1;
    				q.push(p2);
    			}
    		}
    	}
    }
    int main()
    {
    	int t1,i,j,x,y;
    	cin>>t1;
    	while(t1--)
    	{
    		memset(t,0,sizeof(t));
    		scanf("%d%d",&w,&h);
    		for(i=0;i<h;i++)
    		{
    			getchar();
    			for(j=0;j<w;j++)
    			{
    				scanf("%c",&c[i][j]);
    				if(c[i][j]=='S')
    				{x=i;y=j;}
    				if(c[i][j]=='E')
    				{a=i;b=j;}
    			}
    		}
    		sum=1;
    		if(dfs(x,y,1,d1))
    			printf("%d ",sum+1);
    		sum=1;
    		if(dfs(x,y,1,d2))
    			printf("%d ",sum+1);
    		bfs(x,y);
    	}
    	return 0;
    }
    

    我觉得这道题需要极强的方向感,出门就忘记路的大路痴我哭晕在厕所,用两个数组来存储左边优先搜索和右边优先搜索的位置,因为下一个位置的方向和上一个位置的方向有关,相当于模拟一个人的走路,比如右拐后,则在二维数组上,此时人面向右边,左边即是二维地图的上方。

    #include<cmath>
    #include<queue>
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    #define N 45
    struct P{
    	int x;
    	int y;
    	int step;
    };
    int t[N][N];
    int w,h,a,b,sum;
    char c[N][N];
    int d1[4]={-1,0,1,2};
    int d2[4]={1,0,-1,2};
    int dir[4][2]={{0,-1},{-1,0},{0,1},{1,0}};
    bool dfs(int x1,int y1,int t,int *d)
    {
    	for(int i=0;i<4;i++)
    	{
    		int t1=(t+4+d[i])%4;
    		int xx=x1+dir[t1][0];
    		int yy=y1+dir[t1][1];
    		if(xx==a&&yy==b)
    			return true;
    		if(xx>=0&&xx<h&&yy>=0&&yy<w&&c[xx][yy]=='.')
    		{
    			sum++;
    			if(dfs(xx,yy,t1,d))
    				return true;
    
    		}
    	}
    	return false;
    }
    void bfs(int x1,int y1)
    {
    	queue<P> q;
    	P p1,p2;
    	p1.x=x1;
    	p1.y=y1;
    	p1.step=1;
    	t[x1][y1]=1;
    	q.push(p1);
    	while(!q.empty())
    	{
    		p1=q.front();
    		q.pop();
    		for(int i=0;i<4;i++)
    		{
    			int xx=p1.x+dir[i][0];
    			int yy=p1.y+dir[i][1];
    			if(xx==a&&yy==b)
    			{
    				printf("%d
    ",p1.step+1);
    				break;
    			}
    			if(xx>=0&&xx<h&&yy>=0&&yy<w&&c[xx][yy]=='.'&&!t[xx][yy])
    			{
    				t[xx][yy]=1;
    				p2.x=xx;
    				p2.y=yy;
    				p2.step=p1.step+1;
    				q.push(p2);
    			}
    		}
    	}
    }
    int main()
    {
    	int t1,i,j,x,y;
    	cin>>t1;
    	while(t1--)
    	{
    		memset(t,0,sizeof(t));
    		scanf("%d%d",&w,&h);
    		for(i=0;i<h;i++)
    		{
    			getchar();
    			for(j=0;j<w;j++)
    			{
    				scanf("%c",&c[i][j]);
    				if(c[i][j]=='S')
    				{x=i;y=j;}
    				if(c[i][j]=='E')
    				{a=i;b=j;}
    			}
    		}
    		sum=1;
    		if(dfs(x,y,1,d1))
    			printf("%d ",sum+1);
    		sum=1;
    		if(dfs(x,y,1,d2))
    			printf("%d ",sum+1);
    		bfs(x,y);
    	}
    	return 0;
    }
    

    最短路直接用广搜即可


  • 相关阅读:
    C#学习笔记之——矩形覆盖问题
    链表,栈,队列代码
    链表练习代码
    2012年全国计算机专业大学排名
    寄存器介绍
    win8 wifi开关显示关闭,且设置里面wifi开关显示灰色的解决办法
    360随身wifi无法使用临时解决方案大全
    锐捷客户端的校园网电脑如何转化成无线路由
    未完成数据结构题目
    数据结构代码1
  • 原文地址:https://www.cnblogs.com/zhangmingzhao/p/7256412.html
Copyright © 2020-2023  润新知