Problem D:Cross the maze google 2014

Problem

Edison, a robot, does not have a right hand or eyes. As a brave robot, he always puts his left hand on the wall no matter he walks or turns around. Because he thinks it is too dangerous, Edison does not walk backward.

Assume that Edison has found himself in a square-shaped maze of NxN square cells which is surrounded by walls from the outside. In the maze, some of the cells are also walls. Edison can only move between two empty cells in four directions, north, south, west and east. In order to get out of the maze, he drafts a plan. He uses his left hand to lean on the wall and goes by following the wall.

Here is the question, is Edison able to get out of the maze in at most 10,000 steps? If he can make it, output the path. By getting out of the maze, he only needs to be in the exit cell. If the starting cell is the same as the exit, Edison won't need to move and can directly get out of the maze.

Input

The first line of the input gives the number of test cases, T.T test cases follow. Each test case starts with an integer N. N is the size of the maze. The following N lines, each line containsN characters which may be '.' or '#'. '.' is an empty cell, '#' is a wall. Followed by a line which contains four integers:sx, sy, ex, ey. (sx,sy) means that Edison is standing on row sx and columnsy as his starting cell, (ex, ey) is the exit of the maze. (sx,sy) is guaranteed to be at one of the 4 corners of the maze, and Edison can only touch the wall on 4 adjacent cells(not 8) initially. (ex,ey) can be anywhere in the maze. Note that the top-left corner is at position (1,1).

Output

For each test case, output a line containing "Case #x: y", where x is the case number (starting from 1) and y is "Edison ran out of energy." (without the quotes) if Edison can't reach the exit of the maze in at most 10,000 steps, otherwise y should be the number of steps followed by another line which contains y characters to describe the path (each character should be E for east, S for south, W for west or N for north). There is no character to represent the turning around. We don't care about the turning around steps, please only output the path of how Edison will cross the maze.

Limits

1 ≤ T ≤ 30.
1 ≤ sx, sy, ex, eyN.
The starting cell and the exit of the maze will always be an empty cell. And the starting cell and the exit of the maze won't be the same.

Small dataset

2 ≤ N ≤ 10.

Large dataset

2 ≤ N ≤ 100.

Sample

Input
 

Output
 
3
2
.#
#.
1 1 2 2
5
.##.#
.....
...#.
.###.
...#.
1 1 5 3
3
...
.#.
...
1 1 3 3
Case #1: Edison ran out of energy.
Case #2: 22
SEEENSESSSNNNWWSWWSSEE
Case #3: 4
EESS

Input
 

Output
 
3
2
.#
#.
1 1 2 2
5
.##.#
.....
...#.
.###.
...#.
1 1 5 3
3
...
.#.
...
1 1 3 3
Case #1: Edison ran out of energy.
Case #2: 22
SEEENSESSSNNNWWSWWSSEE
Case #3: 4
EESS
 
Note:
In the 2nd test case after moving 1 cell down from his starting cell, Edison will still be able to lean on the wall at the cell (1,2) by his left hand.
In the third test case, due to Edison can't touch the wall at cell (2,2) initially, so he has to go east in his first step.
 
这道题有点复杂:四个方向的顺序是:E,S,W,N
判断能不能向某个方向走需要判断三个条件:
一,左手是不是扶墙
二,前面是不是被墙挡住
三,这个方向是不是走过了
(一)对于左手扶墙,我是设置了一个坐标来表示当前左手的位置,然后再判断是否可以向某个方向走,判断方法是:看你的左手所在位置是不是左侧或左前侧,例如:
向E走,那么就看左手位置是不是当前结点的上方或前上方如果满足其中一个结点,则第一个条件满足(但是有一点是初始的时候不是这样,初始的时候只能看左侧,不存在左前侧问题)
(二)是否被挡住,则看前方是否为0,例如向E走则判断当前点的右侧是否为0
(三)是否访问过,我定义了一个二维数组来记录结点那些方向被访问过,二维数组的元素是一个四元组(e,s,w,n)初始化为0,访问完那个方向那个方向置1
这样三个问题都解决了。
代码:
#include<iostream>
#include<vector>
using namespace std;
#define MAX 103   
#define ez(a,y,x,h,l) (((y-1)==h&&(x+1)==l)||((y-1)==h&&(x)==l))&&(a[y][x+1]==0)
#define sz(a,y,x,h,l) (((y+1)==h&&(x+1)==l)||((y)==h&&(x+1)==l))&&(a[y+1][x]==0)
#define wz(a,y,x,h,l) (((y+1)==h&&(x)==l)||((y+1)==h&&(x-1)==l))&&(a[y][x-1]==0)
#define nz(a,y,x,h,l) (((y-1)==h&&(x-1)==l)||((y)==h&&(x-1)==l))&&(a[y-1][x]==0)
int temp_h[4]={-1,0,1,0};
int temp_l[4]={0,1,0,-1};
struct node
{
	int east,south,west,north;
};
int main()
{
	freopen("D:\\D-large-practice.in","r",stdin);
	freopen("D:\\D-large-practice.out","w",stdout);
	/****************************************/
	
	int T;
	cin>>T;
	int id=1;
	while(T--)
	{
		int n,i,j;
		cin>>n;
		int a[MAX][MAX];
		node b[MAX][MAX];
		for(i=0;i<n+2;i++)//初始化
			for(j=0;j<n+2;j++)
			{
				b[i][j].east=0;
                b[i][j].west=0;
				b[i][j].south=0;
				b[i][j].north=0;
			}
		for(i=0;i<n+2;i++)
			for(j=0;j<n+2;j++)
			{
				if(i==0||j==0||i==n+1||j==n+1)
					a[i][j]=1;
				else{
				char temp;
				cin>>temp;
				if(temp=='.')
				   a[i][j]=0;
				else a[i][j]=1;
				}
			}	
		int sx,sy,ex,ey;
		cin>>sx>>sy>>ex>>ey;
		vector<char> r;
		int c=0,hh,hl,ch,cl,stop=1;//hh,hl代表左手的位置,hh,hl的初始化有点难度,如果一个开始点周围全是0则直接判断结束点是不是它
		for(i=0;i<4;i++)//这是在初始左手位置
		{
			hh=sx+temp_h[i];
			hl=sy+temp_l[i];
			if(a[hh][hl]==1)
				break;
		}
		if(a[hh][hl]==0)//如果初始位置周围全是零
			stop=0;
        while(c<10000&&!(sx==ex&&sy==ey)&&stop==1)
		{
			int xh=0;//记录左手循环次数
			ch=hh-sx,cl=hl-sy;
			/*更新左手的位置,顺时针直到空*/
			while(a[sx+ch][sy+cl]==1&&xh<8)//直到左手位置为0
			{
				xh++;
				hh=sx+ch;
				hl=sy+cl;
//				cout<<"shou:"<<hh<<" "<<hl<<endl;
				if(ch==-1&&cl==0)//如果是正方向有两个方向可作为下一个左手位置
				{
					if(a[sx][sy+1]==0)
					cl=1;
					else ch=0,cl=1;
				}
				else if(ch==-1&&cl==1)//如果是正偏方向则只有一个方向作为下一个位置
					ch=0;
				else if(ch==0&&cl==1)
				{
					if(a[sx+1][sy]==0)
					ch=1;
					else ch=1,cl=0;
				}
				else if(ch==1&&cl==1)
					cl=0;
				else if(ch==1&&cl==0)
				{
					if(a[sx][sy-1]==0)
					cl=-1;
					else ch=0,cl=-1;
				}
				else if(ch==1&&cl==-1)
					ch=0;
				else if(ch==0&&cl==-1)
				{
					if(a[sx-1][sy]==0)
					ch=-1;
					else ch=-1,cl=0;
				}
				else if(ch==-1&&cl==-1)
					cl=0;
			}
			/*可以移动的条件:前方不为空,左手扶墙,没访问过*/
			if(ez(a,sx,sy,hh,hl)&&b[sx][sy].east!=1)
			{
				b[sx][sy].east=1;
				r.push_back('E');
//				cout<<r[c]<<endl;
				sy++;
				c++;
			}else if(sz(a,sx,sy,hh,hl)&&b[sx][sy].south!=1)
			{
				b[sx][sy].south=1;
				r.push_back('S');
//				cout<<r[c]<<endl;
				sx++;
				c++;
			}else if(wz(a,sx,sy,hh,hl)&&b[sx][sy].west!=1)
			{
				b[sx][sy].west=1;
				r.push_back('W');
//				cout<<r[c]<<endl;
				sy--;
				c++;
			}else if(nz(a,sx,sy,hh,hl)&&b[sx][sy].north!=1)
			{
				b[sx][sy].north=1;
				r.push_back('N');
//				cout<<r[c]<<endl;
				sx--;
				c++;
			}else{ 

//                cout<<sx<<" "<<sy<<endl;
//				cout<<a[sx+1][sy]<<" "<<sx+1<<" "<<sy+1<<"zouguoma :"<<b[sx][sy].south<<endl;
				break;
			}
		}
		cout<<"Case #"<<id<<": ";
		if(sx==ex&&sy==ey)
		{
			cout<<r.size()<<endl;
			for(i=0;i<r.size();i++)
				cout<<r[i];
			cout<<endl;
		}
		else
		{
			cout<<"Edison ran out of energy."<<endl;
		}
		id++;
		
	}
	fclose(stdin);
	fclose(stdout);
	return 0;
}

#include<iostream>
#include<vector>
using namespace std;
#define MAX 103  
#define ez(a,y,x,h,l) (((y-1)==h&&(x+1)==l)||((y-1)==h&&(x)==l))&&(a[y][x+1]==0)
#define sz(a,y,x,h,l) (((y+1)==h&&(x+1)==l)||((y)==h&&(x+1)==l))&&(a[y+1][x]==0)
#define wz(a,y,x,h,l) (((y+1)==h&&(x)==l)||((y+1)==h&&(x-1)==l))&&(a[y][x-1]==0)
#define nz(a,y,x,h,l) (((y-1)==h&&(x-1)==l)||((y)==h&&(x-1)==l))&&(a[y-1][x]==0)
int temp_h[4]={-1,0,1,0};
int temp_l[4]={0,1,0,-1};
struct node
{
 int east,south,west,north;
};
int main()
{
 freopen("D:\\D-large-practice.in","r",stdin);
 freopen("D:\\D-large-practice.out","w",stdout);
 /****************************************/
 
 int T;
 cin>>T;
 int id=1;
 while(T--)
 {
  int n,i,j;
  cin>>n;
  int a[MAX][MAX];
  node b[MAX][MAX];
  for(i=0;i<n+2;i++)//初始化
   for(j=0;j<n+2;j++)
   {
    b[i][j].east=0;
                b[i][j].west=0;
    b[i][j].south=0;
    b[i][j].north=0;
   }
  for(i=0;i<n+2;i++)
   for(j=0;j<n+2;j++)
   {
    if(i==0||j==0||i==n+1||j==n+1)
     a[i][j]=1;
    else{
    char temp;
    cin>>temp;
    if(temp=='.')
       a[i][j]=0;
    else a[i][j]=1;
    }
   } 
  int sx,sy,ex,ey;
  cin>>sx>>sy>>ex>>ey;
  vector<char> r;
  int c=0,hh,hl,ch,cl,stop=1;//hh,hl代表左手的位置,hh,hl的初始化有点难度,如果一个开始点周围全是0则直接判断结束点是不是它
  for(i=0;i<4;i++)//这是在初始左手位置
  {
   hh=sx+temp_h[i];
   hl=sy+temp_l[i];
   if(a[hh][hl]==1)
    break;
  }
  if(a[hh][hl]==0)//如果初始位置周围全是零
   stop=0;
        while(c<10000&&!(sx==ex&&sy==ey)&&stop==1)
  {
   int xh=0;//记录左手循环次数
   ch=hh-sx,cl=hl-sy;
   /*更新左手的位置,顺时针直到空*/
   while(a[sx+ch][sy+cl]==1&&xh<8)//直到左手位置为0
   {
    xh++;
    hh=sx+ch;
    hl=sy+cl;
//    cout<<"shou:"<<hh<<" "<<hl<<endl;
    if(ch==-1&&cl==0)//如果是正方向有两个方向可作为下一个左手位置
    {
     if(a[sx][sy+1]==0)
     cl=1;
     else ch=0,cl=1;
    }
    else if(ch==-1&&cl==1)//如果是正偏方向则只有一个方向作为下一个位置
     ch=0;
    else if(ch==0&&cl==1)
    {
     if(a[sx+1][sy]==0)
     ch=1;
     else ch=1,cl=0;
    }
    else if(ch==1&&cl==1)
     cl=0;
    else if(ch==1&&cl==0)
    {
     if(a[sx][sy-1]==0)
     cl=-1;
     else ch=0,cl=-1;
    }
    else if(ch==1&&cl==-1)
     ch=0;
    else if(ch==0&&cl==-1)
    {
     if(a[sx-1][sy]==0)
     ch=-1;
     else ch=-1,cl=0;
    }
    else if(ch==-1&&cl==-1)
     cl=0;
   }
   /*可以移动的条件:前方不为空,左手扶墙,没访问过*/
   if(ez(a,sx,sy,hh,hl)&&b[sx][sy].east!=1)
   {
    b[sx][sy].east=1;
    r.push_back('E');
//    cout<<r[c]<<endl;
    sy++;
    c++;
   }else if(sz(a,sx,sy,hh,hl)&&b[sx][sy].south!=1)
   {
    b[sx][sy].south=1;
    r.push_back('S');
//    cout<<r[c]<<endl;
    sx++;
    c++;
   }else if(wz(a,sx,sy,hh,hl)&&b[sx][sy].west!=1)
   {
    b[sx][sy].west=1;
    r.push_back('W');
//    cout<<r[c]<<endl;
    sy--;
    c++;
   }else if(nz(a,sx,sy,hh,hl)&&b[sx][sy].north!=1)
   {
    b[sx][sy].north=1;
    r.push_back('N');
//    cout<<r[c]<<endl;
    sx--;
    c++;
   }else{

//                cout<<sx<<" "<<sy<<endl;
//    cout<<a[sx+1][sy]<<" "<<sx+1<<" "<<sy+1<<"zouguoma :"<<b[sx][sy].south<<endl;
    break;
   }
  }
  cout<<"Case #"<<id<<": ";
  if(sx==ex&&sy==ey)
  {
   cout<<r.size()<<endl;
   for(i=0;i<r.size();i++)
    cout<<r[i];
   cout<<endl;
  }
  else
  {
   cout<<"Edison ran out of energy."<<endl;
  }
  id++;
  
 }
 fclose(stdin);
 fclose(stdout);
 return 0;
}

 

你可能感兴趣的:(数据结构,程序设计)