描述
给一个20×20的迷宫、起点坐标和终点坐标,问从起点是否能到达终点。
 
输入
多个测例。输入的第一行是一个整数n,表示测例的个数。接下来是n个测例,每个测例占21行,第一行四个整数x1,y1,x2,y2是起止点的位置(坐标从零开始),(x1,y1)是起点,(x2,y2)是终点。下面20行每行20个字符,’.’表示空格;’X’表示墙。
 
输出
每个测例的输出占一行,输出Yes或No。
 
输入样例
2
0 0 19 19
....................
XXXXXXXXXXXXXXXXXXXX
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
0 0 19 19
....................
XXXXXXXXXXXXXXXXXXX.
....................
.XXXXXXXXXXXXXXXXXXX
....................
XXXXXXXXXXXXXXXXXXX.
....................
.XXXXXXXXXXXXXXXXXXX
....................
XXXXXXXXXXXXXXXXXXX.
....................
.XXXXXXXXXXXXXXXXXXX
....................
XXXXXXXXXXXXXXXXXXX.
XXXXXXXXXXXXXXXXXXX.
XXXXXXXXXXXXXXXXXXX.
XXXXXXXXXXXXXXXXXXX.
....................
.XXXXXXXXXXXXXXXXXXX
....................
 
输出样例
No
Yes
 
深搜,对当前项点按四个方向继续搜索即可。
 
/*
 * @author  Panoss
 */
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<ctime>
#include<stack>
#include<queue>
#include<list>
using namespace std;
#define DBG 1
#define fori(i,a,b) for(int i = (a); i < (b); i++)
#define forie(i,a,b) for(int i = (a); i <= (b); i++)
#define ford(i,a,b) for(int i = (a); i > (b); i--)
#define forde(i,a,b) for(int i = (a); i >= (b); i--)
#define forls(i,a,b,n) for(int i = (a); i != (b); i = n[i])
#define mset(a,v) memset(a, v, sizeof(a))
#define mcpy(a,b) memcpy(a, b, sizeof(a))
#define dout  DBG && cerr << __LINE__ << " >>| "
#define checkv(x) dout << #x"=" << (x) << " | "<<endl
#define checka(array,a,b) if(DBG) { \
    dout<<#array"[] | " << endl; \
    forie(i,a,b) cerr <<"["<<i<<"]="<<array[i]<<" |"<<((i-(a)+1)%5?" ":"\n"); \
    if(((b)-(a)+1)%5) cerr<<endl; \
}
#define redata(T, x) T x; cin >> x
#define MIN_LD -2147483648
#define MAX_LD  2147483647
#define MIN_LLD -9223372036854775808
#define MAX_LLD  9223372036854775807
#define MAX_INF 18446744073709551615
inline int  reint() { int d; scanf("%d",&d); return d; }
inline long relong() { long l; scanf("%ld",&l); return l; }
inline char rechar() { scanf(" "); return getchar(); }
inline double redouble() { double d; scanf("%lf", &d); return d; }
inline string restring() { string s; cin>>s; return s; }


char a[20][20];

int start_x,start_y;
int end_x,end_y;

const int dx[] = {1,0,-1,0};   //四个方向增量
const int dy[] = {0,1,0,-1};

bool IsCanplace(int row,int col)
{
    if(row<20&&row>=0&&col>=0&&col<20&&a[row][col]=='.'&&a[row][col]!='1')
        return true;
    else
        return false;
}
void DFS(int row,int col)
{
    a[row][col]='1';
    fori(i,0,4)
    {
        int nx = row + dx[i];
        int ny = col + dy[i];
        if(IsCanplace(nx,ny))
            DFS(nx,ny);
    }
}
int main()
{
    redata(int,T);
    while(T--)
    {
       cin>> start_x >> start_y >> end_x >> end_y;
       fori(i,0,20)
           scanf("%s",a[i]);
       DFS(start_x,start_y);
       if(a[end_x][end_y]=='1')
            cout<<"Yes"<<endl;
       else
            cout<<"No"<<endl;

    }
    return 0;
}

 

posted on 2014-05-21 12:52  Panoss  阅读(192)  评论(0编辑  收藏  举报