Description [title]
one day Extense adventure in the forest when accidentally walk into a maze, a labyrinth can be viewed as a grid of n * n, each grid point only two states,. # And the former represents The latter represents the passage impassable. When Extense at the same time a grid point, he can only move to the truck (or up and down) on the adjacent grid point one four directions, Extense want to go to point B from point A, do not ask the case out of the labyrinth can not do. If there is a starting point or end point can not pass (to #), is seen as unable to accomplish.
[Input]
Line 1 is the group number k of test data, followed by k sets of inputs. Each test line 1 is a positive integer n (1 ≤ n ≤ 100) , it represents the size of the maze of n * n. Next is a n * n matrix, matrix elements. Or #. The next line is the integer 4 ha, la, hb, lb, described in the first ha line A, the first column la, B hb in the first row, first column lb. Notes that ha, la, hb, lb are all counting from zero.
[Output]
k lines, each output corresponding to an input. Can do that then the output "YES", otherwise a "NO".
[Input] Sample
2
. 3
. ##
... #
# ...
0 2 0 2
. 5
...
### #
... # ...
### ...
. ... #
0040
[] sample output
YES
NO
Title Analysis:
This title tells us to do problems must be careful, it's time to start a written assignment of two equal signs, check for a long time, the second assignment is no problem, rewrite the results of local and la ha ha written two, and I was still silly to check my logic is not a problem, the online explanations read it several times, and he sent the code line by line, and when I checked to be such a small error that mentality on the spot collapse! !
Code:
#include<iostream>
#include<cstring>
using namespace std;
char s;
int ha,la,hb,lb,n;
int a[4][2]={1,0,-1,0,0,1,0,-1};
int b[101][101];//存储迷宫情况
int dao=0;//判断有没有到达
void search(int,int);
int main()
{
int k;
cin>>k;
while(k--)
{
dao=0;//重置值
memset(b,0,sizeof(b));
cin>>n;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{
cin>>s;
if(s=='#') b[i][j]=1;//遇到#则使该路不通
}
cin>>ha>>la>>hb>>lb;
if(b[ha][la]||b[hb][lb])//判断始末位置
{
cout<<"NO"<<endl;
continue;
}
else search(ha,la);
if(!dao)
cout<<"NO"<<endl;
}
return 0;
}
void search(int ha,int la)
{
if(ha==hb&&la==lb)//到达指定地点则输出并返回
{
cout<<"YES"<<endl;
dao=1;
return;
}
else for(int i=0;i<4;i++)
{
int nx=ha+a[i][0];
int ny=la+a[i][1];
if(nx>=0&&ny>=0&&nx<n&&ny<n&&(!b[nx][ny]))//判断下标是否合法以及该位置可以经过
{
b[nx][ny]=1;
search(nx,ny);//继续搜索下一步
}
}
}
result:
输入输出结果:
2
3
.##
..#
#..
0 0 2 2
YES
5
.....
###.#
..#..
###..
...#.
0 0 4 0
NO
Process returned 0 (0x0) execution time : 4.692 s
Press any key to continue.